首页 > 代码库 > sed基本使用
sed基本使用
sed [options] sed-commands input-file
options:
-n: 静默模式,不输出模式空间中的内容
-e script –e script: 指定多个脚本命令
-f script_file: 指定脚本文件
-i: 直接编辑原文件
-r: 使用扩展的正则表达式
sed-commands:
d: 删除
p: 打印
i \text: 在模式匹配行的前面插入
a \text: 在模式匹配行的后面追加
r /path/to/somefile: 在指定位置读取另外一个文件插入
w /path/to/somefile: 把符合条件的所有行保存至指定的文件中
=: 打印行号
s/查找条件/替换文本/: 查找替换
地址定界:
1,3: 匹配第一行到第三行
2,$: 匹配第二行到最后一行
/he/,$: 匹配he所在的行到最后一行
/he/,/yy/:匹配he所在的行到yy所在的行
/li/: 匹配所有li的行
sed查找替换命令格式:
sed ‘[address-range | pattern-range] s/original-string/replacement-string/[flags]‘ input-file
flags:
g: 替换一行中所有匹配的模式
[1|2|3|..n]: 规划换一行中第n次匹配的模式
p: 打印替换后的模式
w output.txt: 把替换后的文本写入文件output.txt
i: 忽略搜索的模式的大小写
sed的工作模式:
1.读取一行到模式空间(模式空间是一个sed命令的buffer空间)
2.在此模式空间中执行sed 命令
3.打印模式空间中的行
4.重复以上步骤
实例:
以employee.txt为input-file
# vim 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 -n ‘p‘ 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-commands
# vim script.sed /admin/ p /IT/ p # sed -n -f script.sed employee.txt 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin
使用-e指定多个sed-commands(方法一)
# sed -n -e ‘/admin/p‘ -e ‘/IT/p‘ employee.txt 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin
使用-e指定多个sed-commands(方法二)
# sed -n > -e ‘/admin/p‘ > -e ‘/IT/p‘ > employee.txt 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin
使用-e指定多个sed-commands(方法二)
打印第二行
# sed -n ‘2 p‘ employee.txt 102,Jason Smith,IT Manager
打印第一行到第四行
# sed -n ‘1,4 p‘ employee.txt 101,John Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer
打印第二行到最后一行
# sed -n ‘2,$ p‘ employee.txt 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Manager
打印从第一行开始的2行
# sed -n ‘1,+2 p‘ employee.txt 101,John Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin
打印奇数行
# sed -n ‘1~2 p‘ employee.txt 101,John Doe,CEO 103,Raj Reddy,Sysadmin 105,Jane Miller,Sales Manager
打印偶数行
# sed -n ‘2~2 p‘ employee.txt 102,Jason Smith,IT Manager 104,Anand Ram,Developer
删除第二行
# sed ‘2 d‘ employee.txt 101,John Doe,CEO 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Manager
删除第三行到最后一行
# sed ‘3,$ d‘ employee.txt 101,John Doe,CEO 102,Jason Smith,IT Manager
删除空行
# sed ‘/^$/ d‘ employee.txt
删除以#开始的注释行
sed ‘/^#/ d‘ employee.txt
把employee.txt的偶数行写入到output.txt
# sed -n ‘2 w output.txt‘ employee.txt # cat output.txt 102,Jason Smith,IT Manager
把Manager改成Director
# sed ‘s/Manager/Director/‘ employee.txt 101,John Doe,CEO 102,Jason Smith,IT Director 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Director
搜索Sales所在行的Manager为Director
# sed ‘/Sales/s/Manager/Director/‘ employee.txt 101,John Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Director
搜索替换一行中所有的小写字母a为大写字母A
# 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
搜索替换一行中第二个出现的小写字母a为大写字母A
[root@unp ~]# 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
搜索包含John的行并替换为Jonny,仅打印此行
# sed -n ‘s/John/Jonny/p‘ employee.txt 101,Jonny Doe,CEO
搜索包含John的行并替换为Jonny,写入到文件output.txt
# sed -n ‘s/John/Jonny/w output.txt‘ employee.txt # cat output.txt 101,Jonny Doe,CEO
搜索包含John的行(忽略大小写)并替换为Jonny,写入到文件output.txt
# sed -n ‘s/john/Jonny/iw output.txt‘ employee.txt # cat output.txt 101,Jonny Doe,CEO
在第1到2行的前面的行首插入<<,行尾插入>>
# sed -e ‘1,2s/^/<< /‘ -e ‘1,2s/$/ >>/‘ employee.txt << 101,John Doe,CEO >> << 102,Jason Smith,IT Manager >>
sed搜索定界符也可使用以下几种形式
sed ‘s|/usr/local/bin|/usr/bin|‘ input-file sed ‘s^/usr/local/bin^/usr/bin^‘ input-file sed ‘s@/usr/local/bin@/usr/bin@‘ input-file sed ‘s!/usr/local/bin!/usr/bin!‘ input-file
&用于替换匹配的oritinal-string
# sed ‘s/^[0-9]\{3\}/[&]/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 # sed -r ‘s/^[0-9]{3}/[&]/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
单分组引用
# sed ‘s/\([^,]*\).*/\1/g‘ employee.txt 101 102 103 104 105
多分组引用
# sed ‘s/\([^,]*\),\([^,]*\),\([^,]*\).*/\1,\3/g‘ employee.txt 101,CEO 102,IT Manager 103,Sysadmin 104,Developer 105,Sales Manager
\l表示把后面的单个字符N视为小写n,\L表示将后面的所有字符HNNY视为小写hnny
# sed -n ‘s/John/JO\LHNNY/p‘ employee.txt 101,JOhnny Doe,CEO # sed -n ‘s/John/JO\lHNNY/p‘ employee.txt 101,JOhNNY Doe,CEO
\u表示把后面的单个字符n视为大写N,\U表示将后面的所有字符hnny视为大写HNNY
# sed -n ‘s/John/JO\uhnny/p‘ employee.txt 101,JOHnny Doe,CEO # sed -n ‘s/John/JO\Uhnny/p‘ employee.txt 101,JOHNNY Doe,CEO
\E和\L \U配合使用,作为定界符
# sed -n ‘s/John/JO\Uhnn\Ey/p‘ employee.txt 101,JOHNNy Doe,CEO
把\3改为大写
# sed ‘s/\([^,]*\),\([^,]*\),\([^,]*\).*/\1,\U\3\E/g‘ employee.txt 101,CEO 102,IT MANAGER 103,SYSADMIN 104,DEVELOPER 105,SALES MANAGER
把sed作为一个解释器来用
# cat script.sed #!/bin/sed -f s/\([^,]*\),\([^,]*\),\([^,]*\).*/\2,\1,\3/g s/^.*/<&>/ s/Developer/IT Manager/ s/Manager/Director/ # ./script.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>
直接修改原文件
# sed -i ‘s/John/Johnny/‘ employee.txt # 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
直接修改原文件,并备份原文件
# sed -ibak ‘s/John/Johnny/‘ employee.txt # ll employee.txt* -rw-r--r-- 1 root root 123 Sep 15 18:05 employee.txt -rw-r--r-- 1 root root 121 Sep 15 18:05 employee.txtbak
直接修改原文件,并指定文件后缀名
# sed --in-place=.bak ‘s/John/Johnny/‘ employee.txt # ll employee.txt* -rw-r--r-- 1 root root 123 Sep 15 18:08 employee.txt -rw-r--r-- 1 root root 121 Sep 15 18:07 employee.txt.bak
追加a和插入i
# sed -e ‘1 i \Staff Information\n-------------‘ -e ‘$ a \END\n-------------‘ employee.txt Staff Information ------------- 101,Johnny Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Manager END -------------
替换c
# sed ‘/Sales/ c \105,Jane Miller,Sales Director‘ employee.txt 101,Johnny Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Director
打印隐藏字符
# sed -n ‘l‘ employee.txt 101,Johnny Doe,CEO$ 102,Jason Smith,IT Manager$ 103,Raj Reddy,Sysadmin$ 104,Anand Ram,Developer$ 105,Jane Miller,Sales Manager$
打印行号
# sed ‘=‘ employee.txt 1 101,Johnny Doe,CEO 2 102,Jason Smith,IT Manager 3 103,Raj Reddy,Sysadmin 4 104,Anand Ram,Developer 5 105,Jane Miller,Sales Manager
转换大小写字母
# sed ‘y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/‘ employee.txt 101,JOHNNY DOE,CEO 102,JASON SMITH,IT MANAGER 103,RAJ REDDY,SYSADMIN 104,ANAND RAM,DEVELOPER 105,JANE MILLER,SALES MANAGER
多个input-file
# sed -n ‘/^root\>/ p‘ /etc/{passwd,group} root:x:0:0:root:/root:/bin/bash root:x:0:
退出命令q
# sed ‘q‘ employee.txt 101,Johnny Doe,CEO # sed ‘2q‘ employee.txt 101,Johnny Doe,CEO 102,Jason Smith,IT Manager
命令n:用于打印当前模式空间,并从input-file中读入下一行
# sed ‘n‘ employee.txt 101,Johnny Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Manager
本文出自 “虎虎生威” 博客,请务必保留此出处http://tobeone.blog.51cto.com/817917/1553001
sed基本使用