您的位置:首页 > 其它

sed基本使用

2014-09-15 18:33 267 查看
sed [options] sed-commands input-fileoptions:-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-fileflags:g: 替换一行中所有匹配的模式[1|2|3|..n]: 规划换一行中第n次匹配的模式p: 打印替换后的模式w output.txt: 把替换后的文本写入文件output.txti: 忽略搜索的模式的大小写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.txt102,Jason Smith,IT Manager
打印第一行到第四行
# sed -n '1,4 p' employee.txt101,John Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer
打印第二行到最后一行
# sed -n '2,$ p' employee.txt102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager
打印从第一行开始的2行
# sed -n '1,+2 p' employee.txt101,John Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin
打印奇数行
# sed -n '1~2 p' employee.txt101,John Doe,CEO103,Raj Reddy,Sysadmin105,Jane Miller,Sales Manager
打印偶数行
# sed -n '2~2 p' employee.txt102,Jason Smith,IT Manager104,Anand Ram,Developer
删除第二行
# sed '2 d' employee.txt101,John Doe,CEO103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager
删除第三行到最后一行
# sed '3,$ d' employee.txt101,John Doe,CEO102,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.txt102,Jason Smith,IT Manager
把Manager改成Director
# sed 's/Manager/Director/' employee.txt101,John Doe,CEO102,Jason Smith,IT Director103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Director
搜索Sales所在行的Manager为Director
# sed '/Sales/s/Manager/Director/' employee.txt101,John Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Director
搜索替换一行中所有的小写字母a为大写字母A
# sed 's/a/A/g' employee.txt101,John Doe,CEO102,JAson Smith,IT MAnAger103,RAj Reddy,SysAdmin104,AnAnd RAm,Developer105,JAne Miller,SAles MAnAger
搜索替换一行中第二个出现的小写字母a为大写字母A
[root@unp ~]# sed 's/a/A/2' employee.txt101,John Doe,CEO102,Jason Smith,IT MAnager103,Raj Reddy,SysAdmin104,Anand RAm,Developer105,Jane Miller,SAles Manager
搜索包含John的行并替换为Jonny,仅打印此行
# sed -n 's/John/Jonny/p' employee.txt101,Jonny Doe,CEO
搜索包含John的行并替换为Jonny,写入到文件output.txt
# sed -n 's/John/Jonny/w output.txt' employee.txt# cat output.txt101,Jonny Doe,CEO
搜索包含John的行(忽略大小写)并替换为Jonny,写入到文件output.txt
# sed -n 's/john/Jonny/iw output.txt' employee.txt# cat output.txt101,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-filesed 's^/usr/local/bin^/usr/bin^' input-filesed 's@/usr/local/bin@/usr/bin@' input-filesed '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.txt101102103104105
多分组引用
# sed 's/\([^,]*\),\([^,]*\),\([^,]*\).*/\1,\3/g' employee.txt101,CEO102,IT Manager103,Sysadmin104,Developer105,Sales Manager
\l表示把后面的单个字符N视为小写n,\L表示将后面的所有字符HNNY视为小写hnny
# sed -n 's/John/JO\LHNNY/p' employee.txt101,JOhnny Doe,CEO# sed -n 's/John/JO\lHNNY/p' employee.txt101,JOhNNY Doe,CEO
\u表示把后面的单个字符n视为大写N,\U表示将后面的所有字符hnny视为大写HNNY
# sed -n 's/John/JO\uhnny/p' employee.txt101,JOHnny Doe,CEO# sed -n 's/John/JO\Uhnny/p' employee.txt101,JOHNNY Doe,CEO
\E和\L \U配合使用,作为定界符
# sed -n 's/John/JO\Uhnn\Ey/p' employee.txt101,JOHNNy Doe,CEO
把\3改为大写
# sed 's/\([^,]*\),\([^,]*\),\([^,]*\).*/\1,\U\3\E/g' employee.txt101,CEO102,IT MANAGER103,SYSADMIN104,DEVELOPER105,SALES MANAGER
把sed作为一个解释器来用
# cat script.sed#!/bin/sed -fs/\([^,]*\),\([^,]*\),\([^,]*\).*/\2,\1,\3/gs/^.*/<&>/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.txt101,Johnny Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,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.txtStaff Information-------------101,Johnny Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales ManagerEND-------------
替换c
# sed '/Sales/ c \105,Jane Miller,Sales Director' employee.txt101,Johnny Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Director
打印隐藏字符
# sed -n 'l' employee.txt101,Johnny Doe,CEO$102,Jason Smith,IT Manager$103,Raj Reddy,Sysadmin$104,Anand Ram,Developer$105,Jane Miller,Sales Manager$
打印行号
# sed '=' employee.txt1101,Johnny Doe,CEO2102,Jason Smith,IT Manager3103,Raj Reddy,Sysadmin4104,Anand Ram,Developer5105,Jane Miller,Sales Manager
转换大小写字母
# sed  'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' employee.txt101,JOHNNY DOE,CEO102,JASON SMITH,IT MANAGER103,RAJ REDDY,SYSADMIN104,ANAND RAM,DEVELOPER105,JANE MILLER,SALES MANAGER
多个input-file
# sed -n '/^root\>/ p' /etc/{passwd,group}root:x:0:0:root:/root:/bin/bashroot:x:0:
退出命令q
# sed 'q' employee.txt101,Johnny Doe,CEO# sed '2q' employee.txt101,Johnny Doe,CEO102,Jason Smith,IT Manager
命令n:用于打印当前模式空间,并从input-file中读入下一行
# sed  'n' employee.txt101,Johnny Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sed 基本使用