1. 追加 a 命令
### 语法 # 在指定行后添加一行,第2行后添加一行203 [root@tyler temp]# sed '2 a 203,Jack,Engineer' employee.txt 101,John Doe,CEO 102,Jason Smith,IT Manager 203,Jack,Engineer 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Manager [root@tyler temp]# # 在匹配行后添加多行 [root@tyler temp]# sed '/Jason/a 203,Jack Johnson,Engineer\n204,Mark Smith,Sales Engineer' employee.txt 101,John Doe,CEO 102,Jason Smith,IT Manager 203,Jack Johnson,Engineer 204,Mark Smith,Sales Engineer 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Manager [root@tyler temp]# |
2. 插入 i 命令
### 语法 [root@tyler temp]# sed '2 i 203,Jack,Engineer' employee.txt 101,John Doe,CEO 203,Jack,Engineer 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Manager [root@tyler temp]# # 插入多行,用法同上 |
3. 修改 c 命令
### 语法 [root@tyler temp]# sed '2 c 202,Jack,Engineer' employee.txt [root@tyler temp]# sed '2s/.*/202,Jack,Engineer/' employee.txt # 可以用多行取代一行,用法同上 |
4. 打印不可见字符 l 命令
[root@tyler temp]# sed -n 'l' tmp.txt hndisjdljdisj$ nmamlka$ doiskdois$ dsdlkpsldp$ [root@tyler temp]# sed -n 'l' dos.txt hndisjdljdisj\r$ nmamlka\r$ doiskdois\r$ dsdlkpsldp\r$ [root@tyler temp]# ### l命令后指定数字,会在第n个字符使用一个不可见的自动折行 [root@tyler temp]# sed -n 'l 20' employee.txt 101,John Doe,CEO$ 102,Jason Smith,IT \ Manager$ 103,Raj Reddy,Sysad\ min$ 104,Anand Ram,Devel\ oper$ 105,Jane Miller,Sal\ es Manager$ |
5. 打印行号 = 命令
### 打印所有行的行号 1 101,John Doe,CEO 2 102,Jason Smith,IT Manager 3 103,Raj Reddy,Sysadmin 4 104,Anand Ram,Developer 5 105,Jane Miller,Sales Manager [root@tyler temp]# sed '1,3=' employee.txt 1 101,John Doe,CEO 2 102,Jason Smith,IT Manager 3 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Manager [root@tyler temp]#
[root@tyler temp]# sed '/Jane/=' employee.txt 101,John Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 5 105,Jane Miller,Sales Manager
[root@tyler temp]# sed -n '$=' employee.txt 5 [root@tyler temp]# |
6. 转换字符 y 命令
### 命令y可以根据对应位置转换字符 # 常用于,大小写转换 [root@tyler temp]# sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' employee.txt 101,JOHN DOE,CEO 102,JASON SMITH,IT MANAGER 103,RAJ REDDY,SYSADMIN 104,ANAND RAM,DEVELOPER 105,JANE MILLER,SALES MANAGER [root@tyler temp]# |
7. 操作多个文件
[root@tyler temp]# sed -n '/root/p' /etc/passwd /etc/group root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin dockerroot:x:496:491:Docker User:/var/lib/docker:/sbin/nologin root:x:0: dockerroot:x:491: [root@tyler temp]# |
8. sed退出 q 命令
### 在指定行,退出 [root@tyler temp]# sed '3 q' employee.txt 101,John Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin ### 匹配到行,退出 [root@tyler temp]# sed '/Manager/q' employee.txt 101,John Doe,CEO 102,Jason Smith,IT Manager [root@tyler temp]# |
9. 读取文件数据 r 命令
### r命令会读取另一个文件的内容,并在指定的位置打印出来 [root@tyler temp]# sed '$rlog.txt' employee.txt 101,John Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Manager log: input.txt log: log: testing resumed log: log:output created
[root@tyler temp]# sed '/Raj/r log.txt' employee.txt 101,John Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin log: input.txt log: log: testing resumed log: log:output created 104,Anand Ram,Developer 105,Jane Miller,Sales Manager [root@tyler temp]# |
10. 选项和命令
### -l选项用于指定行的长度(需要配合l命令使用) 101,John Doe,CEO$ 102,Jason Smith,IT \ Manager$ 103,Raj Reddy,Sysad\ min$ 104,Anand Ram,Devel\ oper$ 105,Jane Miller,Sal\ es Manager$ ### l命令也可以单独使用指定行长度 [root@tyler temp]# sed -n 'l20' employee.txt 101,John Doe,CEO$ 102,Jason Smith,IT \ Manager$ 103,Raj Reddy,Sysad\ min$ 104,Anand Ram,Devel\ oper$ 105,Jane Miller,Sal\ es Manager$ ### 容易混淆的还有 # -i 选项,直接修改原文件 <> i命令用于插入数据 # -n 选项,屏蔽默认输出 <> n命令打印模式空间 |