首页 > 代码库 > sed附加命令
sed附加命令
追加命令(命令a)
sed ‘[address] a the-line-to-append’ input-file
在第二行后面追加一行(原文这里可能有问题,没有写名行号)
[root@sishen ~]# sed ‘2 a 203,Jack Johnson,Engineer‘ employee.txt
101,Johnnynynyny Doe,CEO
102,Jason Smith,IT Manager
203,Jack Johnson,Engineer
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
#106,Jane Miller,Sales Manager
#107,Jane Miller,Sales Manager
在employee.txt文件结尾追加一行:
[root@sishen ~]# sed ‘$ a 106,Jack Johnny,Engineer‘ employee.txt
101,Johnnynynyny Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
#106,Jane Miller,Sales Manager
#107,Jane Miller,Sales Manager
106,Jack Johnny,Engineer
在匹配Jason的行后面追加两行
[root@sishen ~]# sed ‘/Jason/a\
> 203,Jack Johnson,Engineer\
> 204,Mark Smith,Slaes Engineer‘ employee.txt
101,Johnnynynyny Doe,CEO
102,Jason Smith,IT Manager
203,Jack Johnson,Engineer
204,Mark Smith,Slaes Engineer
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
#106,Jane Miller,Sales Manager
#107,Jane Miller,Sales Manager
追加多行之间可以用\n来换行,这样就不用折行了,上面的命令等价于:
[root@sishen ~]# sed ‘/Jason/a 203,Jack Johnson,Engineer\n204,Mark Smith,Slaes Engineer‘ employee.txt
101,Johnnynynyny Doe,CEO
102,Jason Smith,IT Manager
203,Jack Johnson,Engineer
204,Mark Smith,Slaes Engineer
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
#106,Jane Miller,Sales Manager
#107,Jane Miller,Sales Manager
修改命令(命令c)
sed ‘[address] c the-line-to-insert’ input-file
用新数据取代第2行
[root@sishen ~]# sed ‘2 c 202,Jack,Johnson,Engineer‘ employee1.txt
101,John Doe,CEO
202,Jack,Johnson,Engineer
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
#106,Jane Miller,Sales Manager
#107,Jane Miller,Sales Manager
这里命令c等价于替换:sed ‘2s/.*/202,,Jack,Johnson,Engineer/’employee.txt
sed也可以用多行来取代一行
用两行数据取代匹配Raj的行
[root@sishen ~]# sed ‘/Raj/c \
> 203,Jack,Johnson,Engineer \
> 204,Mark Smith,Slaes Engineer‘ employee.txt
101,Johnnynynyny Doe,CEO
102,Jason Smith,IT Manager
203,Jack,Johnson,Engineer
204,Mark Smith,Slaes Engineer
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
#106,Jane Miller,Sales Manager
#107,Jane Miller,Sales Manager
命令a、i、和c组合使用
l a在“Jason“后面追加”Jack Johnson“
l i在“Jason”前面插入”Mark Smith“
l c用“Joe Mason“代替”Jason“
[root@sishen ~]# sed ‘/Jason/ {
a\
204,Jack Johnson,Engineer
i\
202,Mark Smith,Slaes Engineer
c\
203,Joe Mason,Sysadmin
}‘ employee.txt
101,Johnnynynyny Doe,CEO
202,Mark Smith,Slaes Engineer
203,Joe Mason,Sysadmin
204,Jack Johnson,Engineer
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
#106,Jane Miller,Sales Manager
#107,Jane Miller,Sales Manager
打印不可见字符(命令l)
首先建立测试文件
[root@sishen ~]# vim tab.txt
fname First Name
lname Last Name
Mname Middle Name
[root@sishen ~]# sed -n ‘l‘ tab.txt
fname First Name$
lname Last Name$
Mname Middle Name$
$
如果在l后面指定了数字,那么会在第N个字符处使用一个不可见自动折行
[root@sishen ~]# sed -n ‘l 20‘ employee.txt
101,Johnnynynyny Do\
e,CEO$
102,Jason Smith,IT \
Manager$
103,Raj Reddy,Sysad\
min$
104,Anand Ram,Devel\
oper$
105,Jane Miller,Sal\
es Manager$
#106,Jane Miller,Sa\
les Manager$
#107,Jane Miller,Sa\
les Manager$
这个功能只有GNU sed才有
打印行号(命令=)
打印所有行的行号
[root@sishen ~]# sed ‘=‘ employee.txt
1
101,Johnnynynyny Doe,CEO
2
102,Jason Smith,IT Manager
3
103,Raj Reddy,Sysadmin
4
104,Anand Ram,Developer
5
105,Jane Miller,Sales Manager
6
#106,Jane Miller,Sales Manager
7
#107,Jane Miller,Sales Manager
提示:把命令=和命令N配合使用,可以把行号和内容显示在同一行上
只打印1,2,3行的行号
[root@sishen ~]# sed ‘1,3=‘ employee.txt
1
101,Johnnynynyny Doe,CEO
2
102,Jason Smith,IT Manager
3
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
#106,Jane Miller,Sales Manager
#107,Jane Miller,Sales Manager
打印包含关键字“Jane“的行哈,同时打印输入文件中的内容:
[root@sishen ~]# sed ‘/Jane/=‘ employee.txt
101,Johnnynynyny Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
5
105,Jane Miller,Sales Manager
6
#106,Jane Miller,Sales Manager
7
#107,Jane Miller,Sales Manager
如果你想只是显示行号但不显示行的内容,那么使用-n选项来配合命令=:
[root@sishen ~]# sed -n ‘/Raj/=‘ employee.txt
3
打印文件的总行数
[root@sishen ~]# sed -n ‘$=‘ employee.txt
7
转换字符(命令y)
命令y根据对应位置转换字符,好处之一便是把大写字符转换为小写,反之亦然
如下例子
[root@sishen ~]# sed ‘y/abcde/ABCDE/‘ employee.txt
101,Johnnynynyny DoE,CEO
102,JAson Smith,IT MAnAgEr
103,RAj REDDy,SysADmin
104,AnAnD RAm,DEvElopEr
105,JAnE MillEr,SAlEs MAnAgEr
#106,JAnE MillEr,SAlEs MAnAgEr
#107,JAnE MillEr,SAlEs ManAgEr
把所有的小写字母转换为大写
[root@sishen ~]# sed ‘y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/‘ employee.txt
101,JOHNNYNYNYNY DOE,CEO
102,JASON SMITH,IT MANAGER
103,RAJ REDDY,SYSADMIN
104,ANAND RAM,DEVELOPER
105,JANE MILLER,SALES MANAGER
#106,JANE MILLER,SALES MANAGER
#107,JANE MILLER,SALES MANAGER
操作多个文件
在/etc/passwd中搜索root并打印出来
[root@sishen ~]# sed -n ‘/root/ p‘ /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
在/etc/group中搜索root并打印出来
[root@sishen ~]# sed -n ‘/root/ p‘ /etc/group
root:x:0:
同时在/etc/passwd和/etc/group中搜索root
[root@sishen ~]# sed -n ‘/root/ p‘ /etc/passwd /etc/group
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:
退出sed (命令q)
当sed遇到命令q,便立刻退出,当前循环中的后续命令不会被执行,也不会继续循环
打印第一行后退出
[root@sishen ~]# sed ‘1 q ‘ employee.txt
101,Johnnynynyny Doe,CEO
打印5行后退出
[root@sishen ~]# sed ‘5 q ‘ employee.txt
101,Johnnynynyny Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
打印所有行直到遇到包含关键字Manager的行
[root@sishen ~]# sed ‘/Manager/ q‘ employee.txt
101,Johnnynynyny Doe,CEO
102,Jason Smith,IT Manager
注意:q命令不能指定地址范围(或模式范围),只能用于单个文件(或单个模式)
从文件中读取数据(命令r)
[root@sishen ~]# sed ‘$ r log.txt‘ employee.txt
101,Johnnynynyny Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
#106,Jane Miller,Sales Manager
#107,Jane Miller,Sales Manager
log: input.txt
log:
log: testing resumed
log:
log:output created
[root@sishen ~]# sed ‘ /Raj/ r ../sed8awk/log.txt‘ employee.txt
101,Johnnynynyny Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
#106,Jane Miller,Sales Manager
#107,Jane Miller,Sales Manager
sed附加命令