#cat passwd root:x:0:0:root:/root:/bin/sh proxy:x:13:13:proxy:/bin:/bin/sh operator:x:37:37:Operator:/var:/bin/sh ftp:x:83:83:ftp:/home/ftp:/bin/sh nobody:x:99:99:nobody:/home:/bin/sh sshd:x:103:99:Operator:/var:/bin/sh #1. p命令
#sed 'p' passwd root:x:0:0:root:/root:/bin/sh root:x:0:0:root:/root:/bin/sh proxy:x:13:13:proxy:/bin:/bin/sh proxy:x:13:13:proxy:/bin:/bin/sh operator:x:37:37:Operator:/var:/bin/sh operator:x:37:37:Operator:/var:/bin/sh ftp:x:83:83:ftp:/home/ftp:/bin/sh ftp:x:83:83:ftp:/home/ftp:/bin/sh nobody:x:99:99:nobody:/home:/bin/sh nobody:x:99:99:nobody:/home:/bin/sh sshd:x:103:99:Operator:/var:/bin/sh sshd:x:103:99:Operator:/var:/bin/sh #发现每一行打印了两次,我们使用-n选项关闭默认的输出:
#sed -n 'p' passwd root:x:0:0:root:/root:/bin/sh proxy:x:13:13:proxy:/bin:/bin/sh operator:x:37:37:Operator:/var:/bin/sh ftp:x:83:83:ftp:/home/ftp:/bin/sh nobody:x:99:99:nobody:/home:/bin/sh sshd:x:103:99:Operator:/var:/bin/sh# awk实现对比:
#awk '{print}' passwd root:x:0:0:root:/root:/bin/sh proxy:x:13:13:proxy:/bin:/bin/sh operator:x:37:37:Operator:/var:/bin/sh ftp:x:83:83:ftp:/home/ftp:/bin/sh nobody:x:99:99:nobody:/home:/bin/sh sshd:x:103:99:Operator:/var:/bin/sh #使用sed不一定要打印所有行出来,我们可以定位到某一行对其进行操作,使用s e d在文件中定位文本的方式:
#sed -n '3p' passwd operator:x:37:37:Operator:/var:/bin/sh #awk实现对比:
#awk '{if(NR==3)print}' passwd operator:x:37:37:Operator:/var:/bin/sh #3) 打印2到4行:
#sed -n '2,4p' passwd proxy:x:13:13:proxy:/bin:/bin/sh operator:x:37:37:Operator:/var:/bin/sh ftp:x:83:83:ftp:/home/ftp:/bin/sh #awk实现对比:
#awk '{if(NR<=4 && NR>=2)print}' passwd proxy:x:13:13:proxy:/bin:/bin/sh operator:x:37:37:Operator:/var:/bin/sh ftp:x:83:83:ftp:/home/ftp:/bin/sh #4) 打印匹配行
#sed -n '/var/p' passwd operator:x:37:37:Operator:/var:/bin/sh sshd:x:103:99:Operator:/var:/bin/sh #awk实现对比:
#awk '/var/{print}' passwd operator:x:37:37:Operator:/var:/bin/sh sshd:x:103:99:Operator:/var:/bin/sh #5) 打印最后一行
#sed -n '$p' passwd sshd:x:103:99:Operator:/var:/bin/sh
#sed -n '/var/=' passwd 3 6awk实现对比:
#awk '/var/{print NR}' passwd 3 6如果打印行号及匹配行,必须使用两个sed命令,并使用 e选项。第一个命令打印模式匹配行,第二个使用 =选项打印行号,格式为 sed -n -e /pattern/p -e /pattern/=。
#sed -n -e '/var/p' -e '/var/=' passwd operator:x:37:37:Operator:/var:/bin/sh 3 sshd:x:103:99:Operator:/var:/bin/sh 6 #
#awk '/var/{print $0 "\n" NR}' passwd operator:x:37:37:Operator:/var:/bin/sh 3 sshd:x:103:99:Operator:/var:/bin/sh 6 #3. \a命令,在指定行后附加新文本
#sed '/root/a\this line is add by sed\haha' passwd root:x:0:0:root:/root:/bin/sh this line is add by sedhaha proxy:x:13:13:proxy:/bin:/bin/sh operator:x:37:37:Operator:/var:/bin/sh ftp:x:83:83:ftp:/home/ftp:/bin/sh nobody:x:99:99:nobody:/home:/bin/sh sshd:x:103:99:Operator:/var:/bin/sh #
#awk '{print;if(/root/)print"this line is add by awk"}' passwd root:x:0:0:root:/root:/bin/sh this line is add by awk proxy:x:13:13:proxy:/bin:/bin/sh operator:x:37:37:Operator:/var:/bin/sh ftp:x:83:83:ftp:/home/ftp:/bin/sh nobody:x:99:99:nobody:/home:/bin/sh sshd:x:103:99:Operator:/var:/bin/sh #4. i\命令,和\a命令类似,只是插入文本在匹配行前一行
#sed '/root/c\this line is modify by sed\h' passwd this line is modify by sedh proxy:x:13:13:proxy:/bin:/bin/sh operator:x:37:37:Operator:/var:/bin/sh ftp:x:83:83:ftp:/home/ftp:/bin/sh nobody:x:99:99:nobody:/home:/bin/sh sshd:x:103:99:Operator:/var:/bin/sh
#awk '{if(/root/){print"this line is modify by awk"}else {print}}' passwd this line is modify by awk proxy:x:13:13:proxy:/bin:/bin/sh operator:x:37:37:Operator:/var:/bin/sh ftp:x:83:83:ftp:/home/ftp:/bin/sh nobody:x:99:99:nobody:/home:/bin/sh sshd:x:103:99:Operator:/var:/bin/sh #6. d命令,删除匹配行
#sed '1,4d' passwd nobody:x:99:99:nobody:/home:/bin/sh sshd:x:103:99:Operator:/var:/bin/sh #
#awk '{if(NR>4)print}' passwd nobody:x:99:99:nobody:/home:/bin/sh sshd:x:103:99:Operator:/var:/bin/sh #7. s命令,替换行文本
# sed '1,2s/bin/sedtest/' passwd root:x:0:0:root:/root:/sedtest/sh proxy:x:13:13:proxy:/sedtest:/bin/sh operator:x:37:37:Operator:/var:/bin/sh ftp:x:83:83:ftp:/home/ftp:/bin/sh nobody:x:99:99:nobody:/home:/bin/sh sshd:x:103:99:Operator:/var:/bin/sh我们可以使用sed的-n选项以及s命令的p选项来仅仅打印被替换的行:
# sed -n '1,2s/bin/sedtest/p' passwd root:x:0:0:root:/root:/sedtest/sh proxy:x:13:13:proxy:/sedtest:/bin/sh使用s命令的g选项可以替换当前行所有符合模式的地方:
# sed -n '1,2s/bin/sedtest/pg' passwd root:x:0:0:root:/root:/sedtest/sh proxy:x:13:13:proxy:/sedtest:/sedtest/sh该命令使用awk实现较复杂。
# sed '3r new.txt' passwd root:x:0:0:root:/root:/bin/sh proxy:x:13:13:proxy:/bin:/bin/sh operator:x:37:37:Operator:/var:/bin/sh this line from new.txt ftp:x:83:83:ftp:/home/ftp:/bin/sh nobody:x:99:99:nobody:/home:/bin/sh sshd:x:103:99:Operator:/var:/bin/sh此操作使用awk较复杂。
# sed '/Operator/q' passwd root:x:0:0:root:/root:/bin/sh proxy:x:13:13:proxy:/bin:/bin/sh operator:x:37:37:Operator:/var:/bin/sh
# awk '{print;if (NR >= 3)exit}' passwd root:x:0:0:root:/root:/bin/sh proxy:x:13:13:proxy:/bin:/bin/sh operator:x:37:37:Operator:/var:/bin/sh10. l命令,显示文件控制字符
# cat new.txt this line from new.txt kkh #使用l命令显示如下:
# sed -n 'l' new.txt this line from new.txt\033[B$ kkh$ \r$ #Awk未发现有此功能。
# sed 's/^[a-z]*//g' passwd在匹配文本后面增加文本:
# sed 's/[0-9][0-9]:/&8902:/g' passwd root:x:0:0:root:/root:/bin/sh proxy:x:13:8902:13:8902:proxy:/bin:/bin/sh operator:x:37:8902:37:8902:Operator:/var:/bin/sh ftp:x:83:8902:83:8902:ftp:/home/ftp:/bin/sh nobody:x:99:8902:99:8902:nobody:/home:/bin/sh sshd:x:103:8902:99:8902:Operator:/var:/bin/sh对password文件做如下操作:
# sed 's/^[a-z]*://g' passwd | sed 's/:/ /g' | sed 's/sh$//g' x 0 0 root /root /bin/ x 13 13 proxy /bin /bin/ x 37 37 Operator /var /bin/ x 83 83 ftp /home/ftp /bin/ x 99 99 nobody /home /bin/ x 103 99 Operator /var /bin/删除行首两字符:
# echo "hello sed." | sed 's/^..//g' llo sed.删除行尾两字符:
# echo "hello sed." | sed 's/..$//g' hello se在sed字符串后面添加字符串” world”:
# echo "hello sed." | sed 's/sed/& world/g' hello sed world.awk和sed的比较,我目前片面看来,sed更适合修改编辑文件,尤其是要修改行中某一字符串的时候很方便;而awk更适合用来取出每一行按指定分割符号分割出来的信息。
原文地址:http://blog.csdn.net/shallnet/article/details/38925653