内容:
1、选项与形式
2、举例
二、举例
2.1.1、建立test.txt文件
- [admin@localhost sed]$ cat -n test.txt
-
- 1 ---------------------------------
-
- 2 abcc
-
- 3 CCCC@sohu.com
-
- 4 Test u:mondey
-
- 5 OctoberD
-
- 6 TTTT@gmail.com
-
- 7 October
-
- 8 r-truth
-
- 9 5456fdsgdfg889g
-
- 10 bigshow
-
- 11 EEEEE@sohu.com
-
- 12 magic per
-
- 13 DDDDD@gmail.com
-
- 14 perhaps
-
- 15 AAAAAA@sohu.com
-
- 16 AABBAA@gmail.com
-
- 17 AAAbbbCCC
-
- 18 END
-
- 19 ---------------------------------
-
- 20 Others
-
- [admin@localhost sed]$
2.1.2、格式二查找,且存在模式(1.1.3\1.1.4):
- [admin@localhost sed]$
-
- [admin@localhost sed]$ sed -n '2,/sohu.com/p' test.txt
-
- abcc
-
- CCCC@sohu.com
-
- [admin@localhost sed]$ sed -n '3,/sohu.com/p' test.txt
-
- CCCC@sohu.com
-
- Test u:mondey
-
- OctoberD
-
- TTTT@gmail.com
-
- October
-
- r-truth
-
- 5456fdsgdfg889g
-
- bigshow
-
- EEEEE@sohu.com
-
- [admin@localhost sed]$ sed -n '4,/sohu.com/p' test.txt
-
- Test u:mondey
-
- OctoberD
-
- TTTT@gmail.com
-
- October
-
- r-truth
-
- 5456fdsgdfg889g
-
- bigshow
-
- EEEEE@sohu.com
-
- [admin@localhost sed]$ sed -n '16,/sohu.com/p' test.txt
-
- AABBAA@gmail.com
-
- AAAbbbCCC
-
- END
-
- ---------------------------------
-
- Others
-
- [admin@localhost sed]$
2.1.3、格式二查找,且不存在模式(1.1.3\1.1.4):
- [admin@localhost sed]$
-
- [admin@localhost sed]$ sed -n '2,/UUUUUU/p' test.txt
-
- abcc
-
- CCCC@sohu.com
-
- Test u:mondey
-
- OctoberD
-
- TTTT@gmail.com
-
- October
-
- r-truth
-
- 5456fdsgdfg889g
-
- bigshow
-
- EEEEE@sohu.com
-
- magic per
-
- DDDDD@gmail.com
-
- perhaps
-
- AAAAAA@sohu.com
-
- AABBAA@gmail.com
-
- AAAbbbCCC
-
- END
-
- ---------------------------------
-
- Others
-
- [admin@localhost sed]$ sed -n '5,/UUUUUU/p' test.txt
-
- OctoberD
-
- TTTT@gmail.com
-
- October
-
- r-truth
-
- 5456fdsgdfg889g
-
- bigshow
-
- EEEEE@sohu.com
-
- magic per
-
- DDDDD@gmail.com
-
- perhaps
-
- AAAAAA@sohu.com
-
- AABBAA@gmail.com
-
- AAAbbbCCC
-
- END
-
- ---------------------------------
-
- Others
-
- [admin@localhost sed]$ sed -n '16,/UUUUUU/p' test.txt
-
- AABBAA@gmail.com
-
- AAAbbbCCC
-
- END
-
- ---------------------------------
-
- Others
-
- [admin@localhost sed]$
-
- [admin@localhost sed]$
-
- [admin@localhost sed]$ sed -n '30,/UUUUUU/p' test.txt
-
- [admin@localhost sed]$
2.1.4、格式一查找,且存在模式(1.1.1\1.1.2):
- [admin@localhost sed]$
-
- [admin@localhost sed]$ sed -n '/sohu.com/,2p' test.txt
-
- CCCC@sohu.com
-
- EEEEE@sohu.com
-
- AAAAAA@sohu.com
-
- [admin@localhost sed]$
-
- [admin@localhost sed]$ sed -n '/sohu.com/,3p' test.txt
-
- CCCC@sohu.com
-
- EEEEE@sohu.com
-
- AAAAAA@sohu.com
-
- [admin@localhost sed]$ sed -n '/sohu.com/,4p' test.txt
-
- CCCC@sohu.com
-
- Test u:mondey
-
- EEEEE@sohu.com
-
- AAAAAA@sohu.com
-
- [admin@localhost sed]$
-
- [admin@localhost sed]$
-
- [admin@localhost sed]$ sed -n '/sohu.com/,16p' test.txt
-
- CCCC@sohu.com
-
- Test u:mondey
-
- OctoberD
-
- TTTT@gmail.com
-
- October
-
- r-truth
-
- 5456fdsgdfg889g
-
- bigshow
-
- EEEEE@sohu.com
-
- magic per
-
- DDDDD@gmail.com
-
- perhaps
-
- AAAAAA@sohu.com
-
- AABBAA@gmail.com
-
- [admin@localhost sed]$
2.1.5、格式一查找,不存在模式(1.1.1\1.1.2)::
- [admin@localhost sed]$
-
- [admin@localhost sed]$ sed -n '/UUUUUU/,2p' test.txt
-
- [admin@localhost sed]$ sed -n '/UUUUUU/,3p' test.txt
-
- [admin@localhost sed]$ sed -n '/UUUUUU/,4p' test.txt
-
- [admin@localhost sed]$ sed -n '/UUUUUU/,16p' test.txt
-
- [admin@localhost sed]$
2.1.6、将sed处理结果写入文件:
- [admin@localhost sed]$
-
- [admin@localhost sed]$ sed -n '1,6 w 1_6_test' test.txt
-
- [admin@localhost sed]$ cat 1_6_test
-
- ---------------------------------
-
- abcc
-
- CCCC@sohu.com
-
- Test u:mondey
-
- OctoberD
-
- TTTT@gmail.com
-
- [admin@localhost sed]$
2.1.7、显示行号以及匹配项:
- [admin@localhost sed]$
-
- [admin@localhost sed]$
-
- [admin@localhost sed]$ sed -n -e '/sohu/p' -e '/sohu/=' test.txt
-
- CCCC@sohu.com
-
- 3
-
- EEEEE@sohu.com
-
- 11
-
- AAAAAA@sohu.com
-
- 15
-
- [admin@localhost sed]$
-
- [admin@localhost sed]$
-
- [admin@localhost sed]$
-
- [admin@localhost sed]$ sed -n -e '/sohu/p' test.txt
-
- CCCC@sohu.com
-
- EEEEE@sohu.com
-
- AAAAAA@sohu.com
-
- [admin@localhost sed]$
2.1.8、在指定文本行进行字符串替换:
- [admin@localhost sed]$
-
- [admin@localhost sed]$ cat test.txt
-
- ---------------------------------
-
- abcc
-
- CCCC@sohu.com
-
- Test u:mondey
-
- OctoberD
-
- TTTT@gmail.com
-
- October
-
- r-truth
-
- 5456fdsgdfg889g
-
- bigshow
-
- EEEEE@sohu.com
-
- magic per
-
- DDDDD@gmail.com
-
- perhaps
-
- AAAAAA@sohu.com
-
- AABBAA@gmail.com
-
- AAAbbbCCC
-
- END
-
- ---------------------------------
-
- Others
-
- [admin@localhost sed]$ sed -n -e '1,7s/sohu/yahoo/gp' test.txt
-
- CCCC@yahoo.com
-
- [admin@localhost sed]$
-------------这里指明了在1--8行之间进行文本替换,将sohu替换为yahoo
2.1.9、通过指定模式间接在匹配的文本行间进行字符串替换:
- [admin@localhost sed]$ cat -n test.txt
-
- 1 ---------------------------------
-
- 2 abcc
-
- 3 CCCC@sohu.com
-
- 4 Test u:mondey
-
- 5 OctoberD
-
- 6 TTTT@gmail.com
-
- 7 October
-
- 8 r-truth
-
- 9 5456fdsgdfg889g
-
- 10 bigshow
-
- 11 EEEEE@sohu.com
-
- 12 magic per
-
- 13 DDDDD@gmail.com
-
- 14 perhaps
-
- 15 AAAAAA@sohu.com
-
- 16 AABBAA@gmail.com
-
- 17 AAAbbbCCC
-
- 18 END
-
- 19 ---------------------------------
-
- 20 Others
-
- [admin@localhost sed]$
-
- [admin@localhost sed]$ sed -n -e '/^O/,/^END/s/\.com/\DOTCOM/p' test.txt
-
- TTTT@gmailDOTCOM
-
- EEEEE@sohuDOTCOM
-
- DDDDD@gmailDOTCOM
-
- AAAAAA@sohuDOTCOM
-
- AABBAA@gmailDOTCOM
-
- [admin@localhost sed]$
------------------如果指定的模式存在多处的话,情况就比较复杂,这个需要进一步测试验证……
2.1.10、可以通过其他间隔符执行s///
如果模式中存在'/',一方面可以通过转移字符'\'进行处理,另外i可以通过":"进行代替:
- [admin@localhost sed]$ sed -e 's:/usr/local:/usr:g' test.txt
2.1.11、去除贪婪
- [admin@localhost sed]$ cat perhaps.txt
- This is what I want!
- [admin@localhost sed]$
- [admin@localhost sed]$ sed -n 's/^<.*>//gp' perhaps.txt
- want!
- [admin@localhost sed]$
- [admin@localhost sed]$ sed -n 's/<[^>]*>//gp' perhaps.txt
- This is what I want!
- [admin@localhost sed]$
2.1.12、运用转义字符+()
- [admin@localhost sed]$ cat just.txt
- aa;bb;cc;dd
- [admin@localhost sed]$
- [admin@localhost sed]$
- [admin@localhost sed]$ sed -n -e 's/\(.*\);\(.*\);\(.*\);\(.*\)/AA\1,\2,CC\3,\4/gp' just.txt
- AAaa,bb,CCcc,dd
- [admin@localhost sed]$
2.1.13、应用大括号{}
- [admin@localhost sed]$
- [admin@localhost sed]$ cat -n test.txt
- 1 ---------------------------------
- 2 abcc
- 3 CCCC@sohu.com
- 4 Test u:mondey
- 5 OctoberD
- 6 TTTT@gmail.com
- 7 October
- 8 r-truth
- 9 5456fdsgdfg889g
- 10 bigshow
- 11 EEEEE@sohu.com
- 12 magic per
- 13 DDDDD@gmail.com
- 14 perhaps
- 15 AAAAAA@sohu.com
- 16 AABBAA@gmail.com
- 17 AAAbbbCCC
- 18 END
- 19 ---------------------------------
- 20 Others
- [admin@localhost sed]$
- [admin@localhost sed]$
- [admin@localhost sed]$ sed -n -e '1,12{s/sohu/yahoo/g p;s/gmail/hotmail/g p}' test.txt
- CCCC@yahoo.com
- TTTT@hotmail.com
- EEEEE@yahoo.com
- [admin@localhost sed]$
阅读(639) | 评论(0) | 转发(0) |