例如:有個文件test的內容以下: ert fff ** [abcfd] 123 324 444 [rty] ** fgfgf
怎麼能截取 [abcfd] 123 324 444 [rty] 這一部分出來呢?
[root@yong-02 sed]# sed -n '/\[abcfd\]/,/\[rty\]/p' 1.txt [abcfd] 123 324 444 [rty]
[root@yong-02 sed]# sed -n '/\[abcfd\]/p' 1.txt [abcfd]
sed轉換大小寫 http://ask.apelearn.com/question/7758linux
[root@yong-02 sed]# sed 's/\b[a-z]/\u&/g' 1.txt Ert Fff ** [Abcfd] 123 324 444 [Rty] ** Fgfgf
注意:\b:表示\b就近匹配,好比\bAB 匹配A,AB\b 匹配B;\b只能匹配字母、數字、漢字、下劃線。spa
&表示前面匹配的內容code
[root@yong-02 sed]# sed 's/[a-z]/\u&/g' 1.txt ERT FFF ** [ABCFD] 123 324 444 [RTY] ** FGFGF
[root@yong-02 sed]# cat 2.txt ERT FFF ** [ABCFD] 123 324 444 [RTY] ** FGFGF [root@yong-02 sed]# sed 's/[A-Z]/\l&/g' 2.txt ert fff ** [abcfd] 123 324 444 [rty] ** fgfgf
[root@linux-128 test]# cat 3.txt askdj aslkd aslkdjf3e skdjfsdfj sdkfjk fsdkfjksdjfkjsdf 12sdfesdf aslkdjfkasdjf asdlfkjaskdfj
[root@yong-02 sed]# sed 's/\(.*\)/\1 123/' 3.txt skdj 123 aslkd aslkdjf3e 123 skdjfsdfj 123 sdkfjk 123 fsdkfjksdjfkjsdf 123 12sdfesdf 123 aslkdjfkasdjf asdlfkjaskdfj 123
[root@yong-02 sed]# sed 's/\(^a.*\)/&123/' 3.txt skdj aslkd aslkdjf3e123 skdjfsdfj sdkfjk fsdkfjksdjfkjsdf 12sdfesdf aslkdjfkasdjf asdlfkjaskdfj123
[root@yong-02 sed]# sed 's/\(^a.*\)/\1123/'g 3.txt skdj aslkd aslkdjf3e123 skdjfsdfj sdkfjk fsdkfjksdjfkjsdf 12sdfesdf aslkdjfkasdjf asdlfkjaskdfj123
[root@linux-128 test]# cat 3.txt askdj aslkd aslkdjf3e skdjfsdfj sdkfjk fsdkfjksdjfkjsdf 12sdfesdf aslkdjfkasdjf asdlfkjaskdfj
[root@yong-02 sed]# sed '/sdkfjk/,$'d 3.txt skdj aslkd aslkdjf3e skdjfsdfj
[root@yong-02 sed]# cat 2.txt ERT FFF ** [ABCFD] 123 324 444 [RTY] ** FGFGF [root@yong-02 sed]# sed '/123/{p;:a;N;$!ba;d}' 2.txt ERT FFF ** [ABCFD] 123
定義一個標籤a,匹配c,而後N把下一行加到模式空間裏,匹配最後一行時,才退出標籤循環,而後命令d,把這個模式空間裏的內容所有清除。字符串
格式:sed -n '1,100{/abc/p}' 1.txtget
[root@yong-02 sed]# cat 2.txt ERT FFF ** [ABCFD] 123 324 444 [RTY] ** FGFGF
方法一:io
[root@yong-02 sed]# sed -n '1,10{/324/p}' 2.txt 324
方法二:test
[root@yong-02 sed]# sed -n '1,10'p 2.txt|sed -n '/324/'p 324