cut 選項 文件名
-f 列號 提取第幾列
-d 分隔符 指定分隔符把行分成多列
不能以空格為分隔符。
[root@localhost ~]# cat testfile no. name sex score 1 zhangsan m 88 2 lisi f 89 3 wangwu m 87 [root@localhost ~]# cut -f 2 testfile 注釋:默認分隔符是制表符\t name zhangsan lisi wangwu [root@localhost ~]# cut -f 2,4 testfile name score zhangsan 88 lisi 89 wangwu 87
[root@localhost ~]# cat testfile no. na:me sex sco:re 1 zhang:san m 8:8 2 li:si f 8:9 3 wang:wu m 8:7 [root@localhost ~]# cut -f 1,3 -d : testfile no. na:re 1 zhang:8 2 li:9 3 wang:7
root@localhost ~]# cut -f 1 -d : /etc/passwd|grep xiong xiongjiawei [root@localhost ~]# grep xiong /etc/passwd|cut -d : -f 1 xiongjiawei [root@localhost ~]# grep xiong /etc/passwd|cut -d ":" -f 1 xiongjiawei [root@localhost ~]# grep xiong /etc/passwd|cut -f 1 -d : xiongjiawei [root@localhost ~]# grep xiong /etc/passwd|cut -f 1 -d ":" xiongjiawei
printf '格式' 輸出內容
%ns 輸出n個字符串
%ni 輸出n個數字
%n.mf 輸出共n位數字,m位小數,如%5.2f表示共3位整數,2位小數
\a 警告聲
\b Backspace鍵
\f 清屏
\n 換行,常用
\r Enter,常用
\t Tab,常用
\v 垂直Tab
awk支持printf和print(linux默認無此命令),print輸出會自動加換行,printf是標准格式輸出命令,不會自動加換行,如果要換行需要手動加。
[root@localhost ~]# printf %s a b cd ef g 1 2 abcdefg12[root@localhost ~]# printf %s %s a b cd ef g 1 2 %sabcdefg12[root@localhost ~]# printf '%s %s' a b cd ef g 1 2 a bcd efg 12 [root@localhost ~]# printf '%s\n%s' a b cd ef g 1 2 a bcd efg 12 [root@localhost ~]# printf '%s %s\n' a b cd ef g 1 2 a b cd ef g 1 2 [root@localhost ~]# printf %s $(cat testfile) no.na:mesexsco:re1zhang:sanm8:82li:sif8:93wang:wum8:7[root@localhost ~]# [root@localhost ~]# cat testfile no. na:me sex sco:re 1 zhang:san m 8:8 2 li:si f 8:9 3 wang:wu m 8:7
awk '條件1{動作1} 條件2{運行2}...' 文件名
默認以空格為分隔符,執行命令時首先讀取文件一行。
[root@localhost ~]# cat testfile no. name sex score age comment 1 zhansan m 88 18 student 2 li:si f 89 20 member 3 wangwu m 87 22 teacher [root@localhost ~]# awk {printf $2 $4} testfile awk: cmd. line:1: {printf awk: cmd. line:1: ^ unexpected newline or end of string 注釋:報錯的原因是awk命令后的條件動作未加單引號 [root@localhost ~]# awk '{printf $2 $4}' testfile namescorezhansan88li:si89wangwu87[root@localhost ~]# awk '{printf $2\t$4\n}' testfile awk: cmd. line:1: {printf $2\t$4\n} awk: cmd. line:1: ^ backslash not last character on line awk: cmd. line:1: {printf $2\t$4\n} awk: cmd. line:1: ^ syntax error 注釋:報錯是因為制表符\t未加雙引號 [root@localhost ~]# awk '{printf $2 "\t" $4 \n}' testfile awk: cmd. line:1: {printf $2 "\t" $4 \n} awk: cmd. line:1: ^ backslash not last character on line awk: cmd. line:1: {printf $2 "\t" $4 \n} awk: cmd. line:1: ^ syntax error 注釋:報錯是因為換行符\n未加雙引號 [root@localhost ~]# awk '{printf $2 "\t" $4 "\n"}' testfile name score zhansan 88 li:si 89 wangwu 87 [root@localhost ~]# awk '{printf $2"\t"$4"\n"}' testfile name score zhansan 88 li:si 89 wangwu 87 [root@localhost ~]# df -h|awk '{printf $1}' 文件系統/dev/sda5devtmpfstmpfstmpfstmpfs/dev/sda2/dev/sdb5/dev/sdb1/dev/sda1tmpfs[root@localhost ~]# df -h|awk '{printf $1"\n"}' 文件系統 /dev/sda5 devtmpfs tmpfs tmpfs tmpfs /dev/sda2 /dev/sdb5 /dev/sdb1 /dev/sda1 tmpfs [root@localhost ~]# df -h|awk '{print $1}' 文件系統 /dev/sda5 devtmpfs tmpfs tmpfs tmpfs /dev/sda2 /dev/sdb5 /dev/sdb1 /dev/sda1 tmpfs [root@localhost ~]# df 文件系統 1K-塊 已用 可用 已用% 掛載點 /dev/sda5 16558080 1443112 15114968 9% / devtmpfs 490168 0 490168 0% /dev tmpfs 499968 0 499968 0% /dev/shm tmpfs 499968 6872 493096 2% /run tmpfs 499968 0 499968 0% /sys/fs/cgroup /dev/sda2 2086912 33160 2053752 2% /home /dev/sdb5 1998672 6144 1871288 1% /disk5 /dev/sdb1 999320 2564 927944 1% /disk1 /dev/sda1 201380 116576 84804 58% /boot tmpfs 99996 0 99996 0% /run/user/0 [root@localhost ~]# df -h|grep sda5 /dev/sda5 16G 1.4G 15G 9% / [root@localhost ~]# df -h|grep sda5|awk '{print $5}' 9% [root@localhost ~]# df -h|grep sda5|awk '{print $5}'|cut -d % -f1 注釋:cut命令的-f選項后的列序號和f之間可以無空格 9 [root@localhost ~]# df -h|grep sda5|awk '{print $5}'|cut -d % -f 1 9 [root@localhost ~]# df -h|grep sda5|awk 'BEGIN{print "This is a title:"}{print $5}'|cut -d % -f 1 This is a title: 9 [root@localhost ~]# df -h|grep sda5|awk '{print "This is a title:"}{print $5}'|cut -d % -f1 This is a title: 9 [root@localhost ~]# awk '{print $1}' /etc/passwd 注釋:awk的默認分隔符是空格 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP nobody:x:99:99:Nobody:/:/sbin/nologin systemd-bus-proxy:x:999:997:systemd systemd-network:x:192:192:systemd dbus:x:81:81:System polkitd:x:998:996:User libstoragemgmt:x:997:995:daemon abrt:x:173:173::/etc/abrt:/sbin/nologin rpc:x:32:32:Rpcbind tss:x:59:59:Account postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated ntp:x:38:38::/etc/ntp:/sbin/nologin chrony:x:996:994::/var/lib/chrony:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin xiongjiawei:x:1000:1000:xiongjiawei:/home/xiongjiawei:/bin/bash qiaofeng:x:1001:1001::/home/qiaofeng:/bin/bash yangguo:x:1002:1002::/home/yangguo:/bin/bash st:x:1003:1004::/home/st:/bin/bash user1:x:1004:1005::/home/user1:/bin/bash [root@localhost ~]# awk '{FS=":"}{print $1}' /etc/passwd root:x:0:0:root:/root:/bin/bash 注釋:設置分隔符為冒號:后第一行並未處理,因為awk命令的執行是首先讀取第一行 bin daemon adm lp sync shutdown halt mail operator games ftp nobody systemd-bus-proxy systemd-network dbus polkitd libstoragemgmt abrt rpc tss postfix sshd ntp chrony tcpdump xiongjiawei qiaofeng yangguo st user1 [root@localhost ~]# awk 'BEGIN{FS=":"}{print $1}' /etc/passwd 注釋:加BEGIN后就不會首先讀取第一行,而是首先設置分隔符 root bin daemon adm lp sync shutdown halt mail operator games ftp nobody systemd-bus-proxy systemd-network dbus polkitd libstoragemgmt abrt rpc tss postfix sshd ntp chrony tcpdump xiongjiawei qiaofeng yangguo st user1 [root@localhost ~]# awk 'END{print "The end!"}BEGIN{FS=":"}{print $1}' /etc/passwd 注釋:有BEGIN就有END,在命令動作中的順序無所謂 root bin daemon adm lp sync shutdown halt mail operator games ftp nobody systemd-bus-proxy systemd-network dbus polkitd libstoragemgmt abrt rpc tss postfix sshd ntp chrony tcpdump xiongjiawei qiaofeng yangguo st user1 The end! [root@localhost ~]# cat testfile no. name sex score age comment 1 zhansan m 88 18 student 2 li:si f 89 20 member 3 wangwu m 87 22 teacher [root@localhost ~]# awk '$4>88{print $2}' testfile 注釋:根據條件執行動作 name li:si
sed
sed [選項] '[動作]' 文件名
選項:
-n 只輸出sed處理過的行
-i 修改原文件
-e 執行多條sed動作
動作:
p 打印,例2p打印第2行,2,3p打印第2、3行
d 刪除,例‘3,4d',不輸出第3、4行
a 追加,例'2a Welcome to China',在第2行后換行追加Welcom to China
i 插入,例'2i Welcom to China',在第2行前插入Welcom to China,即在第1行后換行插入Welcom to China
c 行替換
s 字符串替換
[root@localhost ~]# cat testfile no. name sex score age comment 1 zhansan m 88 18 student 2 li:si f 89 20 member 3 wangwu m 87 22 teacher [root@localhost ~]# sed '2p' testfile 注釋:不加選項-n時即輸出原文件又輸出經過處理的結果 no. name sex score age comment 1 zhansan m 88 18 student 1 zhansan m 88 18 student 2 li:si f 89 20 member 3 wangwu m 87 22 teacher [root@localhost ~]# sed -n '2p' testfile 1 zhansan m 88 18 student [root@localhost ~]# sed -n '2,3p' testfile 1 zhansan m 88 18 student 2 li:si f 89 20 member [root@localhost ~]# df -h|sed -n '2p' /dev/sda5 16G 1.4G 15G 9% / [root@localhost ~]# sed '3,4d' testfile no. name sex score age comment 1 zhansan m 88 18 student [root@localhost ~]# df -h|sed '2a This is added words' 文件系統 容量 已用 可用 已用% 掛載點 /dev/sda5 16G 1.4G 15G 9% / This is added words devtmpfs 479M 0 479M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.8M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda2 2.0G 33M 2.0G 2% /home /dev/sdb5 2.0G 6.0M 1.8G 1% /disk5 /dev/sdb1 976M 2.6M 907M 1% /disk1 /dev/sda1 197M 114M 83M 58% /boot tmpfs 98M 0 98M 0% /run/user/0 [root@localhost ~]# df -h|sed '2i This is added words' 文件系統 容量 已用 可用 已用% 掛載點 This is added words /dev/sda5 16G 1.4G 15G 9% / devtmpfs 479M 0 479M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.8M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda2 2.0G 33M 2.0G 2% /home /dev/sdb5 2.0G 6.0M 1.8G 1% /disk5 /dev/sdb1 976M 2.6M 907M 1% /disk1 /dev/sda1 197M 114M 83M 58% /boot tmpfs 98M 0 98M 0% /run/user/0 [root@localhost ~]# df -h|sed '2i This is \added words' 文件系統 容量 已用 可用 已用% 掛載點 This is dded words /dev/sda5 16G 1.4G 15G 9% / devtmpfs 479M 0 479M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.8M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda2 2.0G 33M 2.0G 2% /home /dev/sdb5 2.0G 6.0M 1.8G 1% /disk5 /dev/sdb1 976M 2.6M 907M 1% /disk1 /dev/sda1 197M 114M 83M 58% /boot tmpfs 98M 0 98M 0% /run/user/0 [root@localhost ~]# df -h|sed '2i This is \ > added words' 文件系統 容量 已用 可用 已用% 掛載點 This is added words /dev/sda5 16G 1.4G 15G 9% / devtmpfs 479M 0 479M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.8M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda2 2.0G 33M 2.0G 2% /home /dev/sdb5 2.0G 6.0M 1.8G 1% /disk5 /dev/sdb1 976M 2.6M 907M 1% /disk1 /dev/sda1 197M 114M 83M 58% /boot tmpfs 98M 0 98M 0% /run/user/0 [root@localhost ~]# sed '2c deleted...' testfile no. name sex score age comment deleted... 2 li:si f 89 20 member 3 wangwu m 87 22 teacher [root@localhost ~]# sed '2c deleted...\ > new lines!!!' testfile no. name sex score age comment deleted... new lines!!! 2 li:si f 89 20 member 3 wangwu m 87 22 teacher [root@localhost ~]# cat testfile no. name sex score age comment 1 zhansan m 88 18 student 2 li:si f 89 20 member 3 wangwu m 87 22 teacher [root@localhost ~]# sed '3s/li:si/lisi/g' testfile 注釋:2表示行序號,不加數字表示替換整個文檔 no. name sex score age comment 1 zhansan m 88 18 student 2 lisi f 89 20 member 3 wangwu m 87 22 teacher [root@localhost ~]# cat testfile no. name sex score age comment 1 zhansan m 88 18 student 2 li:si f 89 20 member 3 wangwu m 87 22 teacher [root@localhost ~]# sed -i '3s/li:si/lisi/g' testfile 注釋:-i選項表示修改原文件,此選項有一定風險性,慎用,如果需要修改文件內容建議使用vim [root@localhost ~]# cat testfile no. name sex score age comment 1 zhansan m 88 18 student 2 lisi f 89 20 member 3 wangwu m 87 22 teacher [root@localhost ~]# sed -e '2s/zhansan/sanzhan/g;3s/li/Li/g' testfile no. name sex score age comment 1 sanzhan m 88 18 student 2 Lisi f 89 20 member 3 wangwu m 87 22 teacher