s :取代,可以直接进行取代的工作!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!
让我们来尝试一下吧
创建sed.txt,内容如下:
1 2 3
Authentication improvements when using an HTTP proxy server. Support for POSIX-style filesystem extended attributes. YARN's REST APIs now support write/modify operations.
第一行下插入一行 sed "1a\hello world" sed.txt(没有修改源文件):
1 2 3 4 5 6 7 8 9
[root@layne laydir]# cat sed.txt Authentication improvements when using an HTTP proxy server. Support for POSIX-style filesystem extended attributes. YARN's REST APIs now support write/modify operations. [root@layne laydir]# sed "1a\hello world" sed.txt Authentication improvements when using an HTTP proxy server. hello world Support for POSIX-style filesystem extended attributes. YARN's REST APIs now support write/modify operations.
加上-i直接修改源文件:sed -i "1a\hello world" sed.txt
删除第2行sed -i "2d" sed.txt :
1 2 3 4 5 6 7 8 9 10
[root@layne laydir]# cat sed.txt Authentication improvements when using an HTTP proxy server. hello world Support for POSIX-style filesystem extended attributes. YARN's REST APIs now support write/modify operations. [root@layne laydir]# sed -i "2d" sed.txt [root@layne laydir]# cat sed.txt Authentication improvements when using an HTTP proxy server. Support for POSIX-style filesystem extended attributes. YARN's REST APIs now support write/modify operations.
[root@layne laydir]# cat sed.txt uthentication improvements when using an HTTP proxy server. Support for POSIX-style filesystem extended attributes. hello2world YARN's REST APIs now support write/modify operations. [root@layne laydir]# sed "/[0-9]/p" sed.txt uthentication improvements when using an HTTP proxy server. Support for POSIX-style filesystem extended attributes. hello2world hello2world YARN's REST APIs now support write/modify operations.
[root@layne laydir]# cat sed.txt uthentication improvements when using an HTTP proxy server. Support for POSIX-style filesystem extended attributes. hello2world YARN's REST APIs now support write/modify operations. [root@layne laydir]# sed -n "/[0-9]/p" sed.txt hello2world
[root@layne laydir]# cat sed.txt uthentication improvements when using an HTTP proxy server. Support FILESysTem for POSIX-style filesystem extended filesystem attributes. YARN's REST APIs now filesystem support write/modify operations. [root@layne laydir]# sed "s/filesystem/FS/" sed.txt uthentication improvements when using an HTTP proxy server. Support FILESysTem for POSIX-style FS extended filesystem attributes. YARN's REST APIs now FS support write/modify operations.
[root@layne laydir]# cat sed.txt uthentication improvements when using an HTTP proxy server. Support FILESysTem for POSIX-style filesystem extended filesystem attributes. YARN's REST APIs now filesystem support write/modify operations. [root@layne laydir]# sed "s/filesystem/FS/i" sed.txt uthentication improvements when using an HTTP proxy server. Support FS for POSIX-style filesystem extended filesystem attributes. YARN's REST APIs now FS support write/modify operations.
[root@layne laydir]# cat sed.txt uthentication improvements when using an HTTP proxy server. Support FILESysTem for POSIX-style filesystem extended filesystem attributes. YARN's REST APIs now filesystem support write/modify operations. [root@layne laydir]# sed "s/filesystem/FS/gi" sed.txt uthentication improvements when using an HTTP proxy server. Support FS for POSIX-style FS extended FS attributes. YARN's REST APIs now FS support write/modify operations.
新建一个文件sed1.txt,输入以下内容:
1 2
hello world,hahha,This is hiaccy2 ideas I am 24 years old,lalala
将sed1.txt的所有数字替换为5,sed "s/[0-9]/5/g" sed1.txt
1 2 3
[root@layne laydir]# sed "s/[0-9]/5/g" sed1.txt hello world,hahha,This is hiaccy5 ideas I am 55 years old,lalala
将hiaccy2改为hiaccy9,先看下面的命令:
1 2 3
[root@layne laydir]# sed "s/2/9/" sed1.txt hello world,hahha,This is hiaccy9 ideas I am 94 years old,lalala
会发现,发现将所有匹配的都修改了(注意并未修改原文件),第二行的24被替换为了94。
更精确匹配方案的写法应该为如下命令:
1 2 3
[root@layne laydir]# sed "s/hiaccy[0-4]/9/" sed1.txt hello world,hahha,This is 9 ideas I am 24 years old,lalala
**但是还存在问题,匹配后被修改内容问匹配出的部分,范围过大。**解决办法:反向引用
用命令sed "s/\(hiaccy\)[0-6]/\19/" sed1.txt
1 2 3
[root@layne laydir]# sed "s/\(hiaccy\)[0-6]/\19/" sed1.txt hello world,hahha,This is hiaccy9 ideas I am 24 years old,lalala
分析:
sed "s/\(hiaccy\)[0-6]/\19/" sed1.txt 的结构划分为:
sed 命令
s/替换的前缀(第一个分割标志)
\(hiaccy\) 其实就是hiaccy,括号用\(和\),就是转义字符,查找的还是hiaccy
[0-6] 通配符数字1~6
/替换的第二个分割标志
\1 表示前面的第一个转义内容,即\(hiaccy\)
9 把在第一个分割标志和第二个分割标志中间 ,除了\1表示的之外的内容,替换为9
/第三个分割标志
再看一个例子,把sed1.txt的中的world替换为woABCld:
1 2 3
[root@layne laydir]# sed "s/\(wo\)r\(ld\)/\1ABC\2/" sed1.txt hello woABCld,hahha,This is hiaccy2 ideas I am 24 years old,lalala
如果加上-r,就不用转义字符了:
1 2 3
[root@layne laydir]# sed -r "s/(wo)r(ld)/\1ABC\2/" sed1.txt hello woABCld,hahha,This is hiaccy2 ideas I am 24 years old,lalala