Shell基础之-sed命令
2015-09-07 01:06
579 查看
Shell基础之-sed命令
首先,大家如果看到有什么不懂的地方,欢迎吐槽!!!我会在当天或者第二天及时回复,并且改进~~
sed 是一个非交互式文本编辑器,它可对文本文件和标准输入进行编辑
sed只是对缓冲区中原始文件的副本进行编辑,并不编辑原始的文件
sed命令调用的三种方式
1、在shell命令行输出命令调用sed sed [option] 'sed cmd' file 2、将sed命令插入脚本 sed [option] -f 'sed.sh' file vi sed.sh sed要调用的脚本为sed.sh /file:/a\This is a test line!!! 调用脚本 sed -f sed.sh test 3、将sed命令插入脚本,直接执行 ./sed.sh file vi sed.sh #!/bin/sed -f /file:/a\This is a test line!!! ./sed.sh test
sed命令选项及意义
-n #不打印所有行到标准输出 -e #表示将下一个字符串解析为sed编辑命令,如果只传递一个编辑命令给sed,-e选项可以省略 -f #调用sed脚本文件
sed命令定位文本的方法示例
x #x为指定行号 x,y #指定从x到y的行号范围 /pattern/ #查询包含模式的行 /pattern/pattern/ #查询包含两个模式的行 /pattern/,x #从与pattern的匹配行到x号行之间的行 x,/pattern/ #从x号行到与pattern的匹配行之间的行 x,y! #查询不包括x和y行号的行
sed编辑命令
p #打印匹配行 = #打印文件行号 a\ #在定位行号之后追加文本信息 i\ #在定位行号之前插入文本信息 d #删除定位行 c\ #用新文本替换定位文本 s #使用替换模式替换相应模式 r #从另一个文件中读文本 w #将文本写入到一个文件 y #变换字符 q #第一个模式匹配完成后退出 l #显示与八进制ASCII码等价的控制字符 {} #在定位行执行的命令组 n #读取下一个输入行,用下一个命令处理新的行 h #将模式缓冲区的文本复制到保持缓冲区 H #将模式缓冲区的文本追加到保持缓冲区 x #互换模式缓冲区和保持缓冲区的内容 g #将保持缓冲区的内容复制到模式缓冲区 G #将保持缓冲区的内容追加到模式缓冲区
sed编辑命令示例:
打印第一行 sed -n '1p' test 打印全部内容,并且有两个第一行 sed '1p' test 打印文本中的3-6行 sed -n '3,6p' test 模式匹配,打印出包含certificate所在的行 sed -n '/certificate/p' test sed [-e]:表示将下一个字符串解析为sed编辑命令 (1)、打印certificate匹配模式的内容 (2)、打印certificate匹配模式的行号 (3)、打印certificate匹配模式的内容及行号 sed -n '/certificate/p' test sed -n '/certificate/=' test sed -n -e '/certificate/p' -e '/certificate/=' test 带多个编辑命令sed的一般格式为 sed [option] -e cmd1 -e cmd2 -e cmdn inputfile 在文本字符串file:后面追加内容为:This is a test file!!! sed '/file:/a\This is a test file !!!' test 将wanglei用户直接追加到/etc/passwd文本的root下面 sed -i '/root:x:0:0/a\wanglei:x:0:0:wanglei:/wanglei:/bin/bash' /etc/passwd 打印出文本test中包含.字符的行 sed -n '/\./p' test 打印出文本test中包含$字符的行 sed -n '/\$/p' test 打印出文本test中的最后一行 sed -n '$p' test 打印出以bus结尾字符串的行 #sed -n '/.*bus$/p' test 打印出不在2-10之间的行 #sed -n '2,10!p' test 打印出除了字符串certificate以外的所有行 sed -n '/certificate/!p' test 在文本test中above字符串前面的行插入内容"This is a test line!!!",并保存于文件之中 sed -i '/above/i\@This is a test line!!!' test 打印在文本test中above字符串到13行之间的内容 sed -n '/above/,13p' test 打印在文本test中3行到字符串/home/globus/.的行 sed -n '3,/\/home\/globus\/\./p' test 在test文本$88字符串前面插入一行"This is a test \i line!!!" sed '/\$88/i\This is a test \\i line!!!' test
把文中匹配的字符串的文本行用新文本替代\c sed '/指定地址/c\test' file 将文本中匹配above字符串的行替换为新行"This is a new line!!!" sed '/above/c\This is a new line!!!' test 将文本中1,5,13行替换为"This is a 1 line !!!"(1,5,13) sed -e '1c\This is a 1 line!!!' -e '5c\This is a 5 line!!!' -e '13c\This is a 13 line!!!' test 删除test文本中的第3行 sed '3d' test 删除test文本中的1-3行 sed '1,3d' test 删除test文本中字符串above到13行的内容 sed '/above/,13d' test 删除最后一行文本 #sed '$d' test 删除5行到最后一行 sed '5,$d' test 删除文本中不区分大小写的字符串certificate的行 sed '/[Cc][Ee][Rr][Tt][Ii][Ff][Ii][Cc][Aa][Tt][Ee]/d' test
替换文本-替换一个或多个字符串
s/被替换的字符串/新字符串/[替换选项] g #表示替换文本中所有出现被替换字符串之处 p #与-n选项结合,只打印替换行 w 文件名 #表示将输出定向到一个文件 sed -n 's/被替换的字符串/新字符串/p' 输入文件 将test文件中的字符串Certificate替换成CERTIFICATE(同行中有多个则只替换前面的一个) sed -n 's/certificate/CERTIFICATE/p' test 将test文件中所有的字符串above替换成shabi,并保存于/root/char文件中 sed -n 's/above/shabi/pg w /root/char' test 替换test文本中的字符串,将above替换为char,替换文中每行第二个出现的char sed -n 's/above/char/2p' test 替换test文本中的字符串,将above替换为charset,替换文中每行第6个出现的above,并且保存到/root/save sed -n 's/above/charset/6p w /root/save' xx 替换test文中的字符串CERTIFICATE,替换为oye,并保存为oye sed -n 's/CERTIFICATE/oye/pg w oye' test & 可以用来保存被替换的字符串以供调用,&符号等价于替换前字符串 sed -n 's/seu/(&)/pg' test sed -n 's/seu/(seu)/pg' test 打印出test文件的1-5行,并写入到output sed -n '1,5 w output' test 将文件xx的内容读入文件test文件字符串above的后面 sed '/above/r xx' test 将文件xx的内容读入文件test文件第5行后面 sed '5r xx' test 将文件xx的内容读入文件test文件第5-7行后面 sed '5,7r xx' test 将文件xx的内容读入文件test文件第5,7,12行后面 sed -e '5r xx' -e '7r xx' -e '12r xx' test 打印出test文件中.r.*匹配的文件 sed -n '/.r.*/p' test 打印出test文件中.r.*匹配的文件,并且只打印出匹配的第一个行 sed '/.r.*/q' test
变换命令 y
表示字符变换,将一系列的字符变换为相应的字符,对字符逐个处理,1处理1 2处理2 … sed 'y/被变换的字符序列/变换的字符序列' 输入文件 将test文件中的所有a、b、c、d、e、f、g、h、i分别变换为数字1、2、3、4、5、6、7、8、9 sed 'y/abcdefghi/123456789/' test 将test文件中的a、b、o、v、e、字符转换为A、B、O、V、E sed 'y/above/ABOVE/' test 将test文件中27个英文字母替换为1234567890-=`~!@#$%^&*()_+/ sed 'y/abcdefghijklmnopqrstuvwxyz/1234567890-=`~!@#$%^&*()_+/' test
在定位行执行命令组{}
sed -n '/Certificate/{p;=}' test 等价于: sed -n -e '/Certificate/p' -e '/Certificate/=' test 在test文本中与Certificate关键字匹配的行全部的i替换为I,将第一个le替换为99 sed '/Certificate/{s/i/I/g;s/le/99/;}' test
n #处理匹配行的下一行
将test文本中字符串certificate下面一行的user字符替换为99 sed '/certificate/{n;s/user/99/;}' test 将test文本中字符串certificate下面一行的\字符替换为gang sed '/certificate/{n;s/\\/gang/;}' test
sed缓冲区处理
sed -e '/file1/h' -e '/file2/x' -e '$G' xxx 1、h #将匹配file1的行复制到保持缓冲区 2、x #将匹配file2的行和保持缓冲区的行互换(file1行) 3、G #将保持缓冲区的内容追加到模式缓冲区 $为最后一行 利用分号分隔多个编辑命令 -e {} ; 利用分号打印出test文件中字符串Ceritificate的行和行号 sed -n '/Certificate/p;/Certificate/=' test 把xxx文件中匹配file1的字符替换为wl,匹配file2替换为fx,匹配file3替换为ywc,匹配file4替换为ly sed 's/file1/wl/; s/file2/fx/; s/file3/ywc/; s/file4/ly/;' xxx
文本样式1
1---This is a read file1 2---This is a read file2 3---This is a read file3 4---This is a read file4 5---This is a read file5 6---This is a read file6 7---This is a read file7
文本样式2
1\This is a Certificate Request file: 2\ 3\It should be meailed to zawu@seu.edu.cn 4\ 5\=========================================================== 6\Certificate Subject: 7\ 8\ /O=Grid/OU=GlobusTest/OU=simpleCA-seugridl.seu.edu.cn/OU=seu.edu.cn/CN=globus 9\ 10\the above string is known as your user certificate subject , and it uniquely identifies 11\this user . $88 12\To install this user certificate, please save this e-mail message into the following file. 13\ 14\/home/globus/.globus/usercert.pem 15\fawnbusgfqa 16\fwafawfbus
相关文章推荐
- Shell基础之-awk命令
- Shell基础之-正则表达式
- Shell基础之-变量、比较、测试
- Shell基础之-grep命令
- 【排序算法之】Shell排序和插入排序
- Shell之-Tomcat资源检测
- 马哥linux学习笔记(bash变量配置)
- Shell之-Tomcat日志检索
- Bash 中的 _ 是不是环境变量
- Shell之-备份系统重要文件
- Shell之-定期检测数据库大小
- Shell之-开机自启所有服务
- 常见linux命令释义(第五天)——shell变量学习
- shell脚本那点事儿8-9
- shell学习
- shell编程入门
- shell 输入输出
- bash 环境变量
- Bash环境配置和文件的特殊权限
- Asp.Net 之 通过调用 WScript.Shell 启动本地 exe 程序时产生“ automation服务器不能创建对象 ”的错误