1. 执行多条指令
sed -n -e'/^root/p' -e'/^nobody/p' /etc/passwd | |
sed -n \ | |
sed -n '{ |
|
2. sed的执行流程
Sed 脚本执行遵从下面简单易记的顺序:Read,Execute,Print,Repeat(读取,执行,打印,重复),
简称 REPR
分析脚本执行顺序:
l 读取一行到模式空间(sed 内部的一个临时缓存,用于存放读取到的内容)
l 在模式空间中执行命令。如果使用了{ } 或 –e 指定了多个命令,sed 将依次执行每
个命令
l 打印模式空间的内容,然后清空模式空间
l 重复上述过程,直到文件结束
3. 打印模式空间
# 打印所有 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]# sed -n '2 p' employee.txt 102,Jason Smith,IT Manager # 打印指定几行 [root@tyler temp]# sed -n '1,3 p' employee.txt 101,John Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin # 打印指定行到结尾 [root@tyler temp]# sed -n '4,$ p' employee.txt 104,Anand Ram,Developer 105,Jane Miller,Sales Manager |
# 加号配合逗号使用,打印第n行到n+m行 [root@tyler temp]# sed -n '1,+2 p' employee.txt 101,John Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin
| # 波浪号,表示第n行开始每次跳过m行 [root@tyler temp]# sed -n '1~2 p' employee.txt 101,John Doe,CEO 103,Raj Reddy,Sysadmin 105,Jane Miller,Sales Manager |
### 模式匹配 # 打印关键字行 [root@tyler temp]# sed -n '/Jane/p' employee.txt 105,Jane Miller,Sales Manager # 打印关键字行到第4行 [root@tyler temp]# sed -n '/Jason/,4 p' employee.txt 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer | # 匹配关键字及之间行 [root@tyler temp]# sed -n '/Raj/,/Jane/ p' employee.txt 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Manager [root@tyler temp]# sed -n '/Jason/,+2 p' employee.txt 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer |
4. 删除行
#
用法和打印行基本一致,匹配需要删除的行,使用删除命令
"d"
常用的删除命令示例: 删除所有空行 sed ‘/^$/ d’ employee.txt 删除所有注释行( 假定注释行以# 开头) sed ‘/^#/ ‘ employee.txt 注意:如果有多个命令,sed 遇到命令 d 时,会删除匹配到的整行数据,其余的命令将无法 操作被删除的行。 |
5. 模式空间输出到文件
# 用法和打印行基本一致,匹配需要删除的行,使用删除命令"w"
[root@tyler temp]# sed -n '2,3 w output.tmp' employee.txt [root@tyler temp]# cat output.tmp 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin |
6. 替换
# 替换所有行 101,John Doe,CEO 102,Jason Smith,IT Diractor 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Diractor # 替换匹配到Sales的行 [root@tyler temp]# sed '/Sales/s/Manager/Diractor/' employee.txt 101,John Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Diractor |
# 默认情况下,sed会替换第一次匹配到的字符串 [root@tyler temp]# sed 's/a/A/' 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]# sed 's/a/A/g' employee.txt 101,John Doe,CEO 102,JAson Smith,IT MAnAger 103,RAj Reddy,SysAdmin 104,AnAnd RAm,Developer 105,JAne Miller,SAles MAnAger # 使用数字标志,可以只替换每行第n次字符串的出现 [root@tyler temp]# sed 's/a/A/2' employee.txt 101,John Doe,CEO 102,Jason Smith,IT MAnager 103,Raj Reddy,SysAdmin 104,Anand RAm,Developer 105,Jane Miller,SAles Manager |
### 打印标志p,配合-n s一起使用,只显示被替换的行 [root@tyler temp]# sed -n 's/locate/find/2 p' sl-test.txt locate command is used to find files locate command uses database to find files ### 写标志w [root@tyler temp]# sed 's/locate/find/2 w output.tmp' sl-test.txt locate command is used to find files locate command uses database to find files locate command can also use regex for searching [root@tyler temp]# cat output.tmp locate command is used to find files locate command uses database to find files ### 忽略大小写标志i [root@tyler temp]# sed -n 's/john/Johnny/i p' employee.txt 101,Johnny Doe,CEO ### 执行命令标志e # 可以将模式空间的内容作为shell命令执行彬返回结果到模式空间 [root@tyler temp]# cat files.txt /etc/passwd /etc/group [root@tyler temp]# sed 's/^/ls -l /e' files.txt -rw-r--r--. 1 root root 1775 6月 12 16:39 /etc/passwd -rw-r--r--. 1 root root 873 6月 12 16:39 /etc/group [root@tyler temp]# ### 标志组合 [root@tyler temp]# sed -n 's/manager/Director/igpw output.tmp' employee.txt 102,Jason Smith,IT Director 105,Jane Miller,Sales Director |
### 替换模式的分隔符 |
### &的作用——获取匹配到模式 [root@tyler temp]# sed 's/^.*$/{&}/' 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]# sed 's/^[0-9][0-9][0-9]/[&]/' employee.txt [101],John Doe,CEO [102],Jason Smith,IT Manager [103],Raj Reddy,Sysadmin [104],Anand Ram,Developer [105],Jane Miller,Sales Manager |
分组以\( 开始,以\) 结束 $ sed 's/\([^,]*\).*/\1/g' employee.txt 101 102 103 104 105 上面例子中: l 正则表达式\([^,]*\)匹配字符串从开头到第一个逗号之间的所有字符(并将其放入第一个分组中) l replacement-string 中的\1 将替代匹配到的分组 l g 即是全局标志
### 分组替换(多个分组) $ sed 's/^\([^,]*\),\([^,]*\),\([^,]*\)/\1,\3/' employee.txt 101,CEO 102,IT Manager 103,Sysadmin 104,Developer 105,Sales Manager
|
使用 sed 可以把 DOS 的换行符(CR/LF)替换为 Unix 格式。当把 DOS 格式的文件拷到 Unix 上,你会发现,每行结尾都有\r\n . 使用 sed 把 DOS 格式的文件转换为 Unix 格式: sed ‘s/.$//’ filename |
7. sed脚本
### 可以重复使用一组sed语句 [root@tyler temp]# cat run.sed s/\([^,]*\),\([^,]*\),\(.*\).*/\2,\1, \3/g s/^.*/<&>/ s/Developer/IT Manager/ s/Manager/Director/ [root@tyler temp]# sed -f run.sed employee.txt <John Doe,101, CEO> <Jason Smith,102, IT Director> <Raj Reddy,103, Sysadmin> <Anand Ram,104, IT Director> <Jane Miller,105, Sales Director> [root@tyler temp]# ### sed注释 [root@tyler temp]# cat run.sed #交换第一列和第二列 s/\([^,]*\),\([^,]*\),\(.*\).*/\2,\1, \3/g #把整行内容放入<>中 s/^.*/<&>/ #把 Developer 替换为 IT Manager s/Developer/IT Manager/ #把 Manager 替换为 Director s/Manager/Director/ ### 用做命令解释器 [root@tyler temp]# cat run.sed #!/bin/sed -f #交换第一列和第二列 s/\([^,]*\),\([^,]*\),\(.*\).*/\2,\1, \3/g #把整行内容放入<>中 s/^.*/<&>/ #把 Developer 替换为 IT Manager s/Developer/IT Manager/ #把 Manager 替换为 Director s/Manager/Director/ [root@tyler temp]# ./run.sed employee.txt <John Doe,101, CEO> <Jason Smith,102, IT Director> <Raj Reddy,103, Sysadmin> <Anand Ram,104, IT Director> <Jane Miller,105, Sales Director> [root@tyler temp]#
[root@tyler temp]# cat test.sed #!/bin/sed -nf /root/p /nobody/p /mail/p |
8. 修改文件
### 常用重定向和w命令 [root@tyler temp]# sed 's/John/Johnny/' employee.txt > new-employee.txt [root@tyler temp]# cat new-employee.txt 101,Johnny Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Manager
[root@tyler temp]# cat new-employee.txt 101,Johnny Doe,CEO
[root@tyler temp]# sed -i.bak 's/John/Johnny/' employee.txt [root@tyler temp]# cat employee.txt.bak 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]# cat employee.txt 101,Johnny Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Manager [root@tyler temp]# # 上边语句等价于-i的全名–in-place sed –in-place=.bak 's/John/Johnny/' employee.txt
|