linux命令---sed
来源:互联网 发布:setup.exe mac 打不开 编辑:程序博客网 时间:2024/06/05 02:39
概述
sed是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。
可以通过定址来定位你所希望编辑的行,该地址用数字构成,用逗号分隔的两个行数表示以这两行为起止的行的范围。范围可以通过数据,正则表达式或者二者结合的方式确定。它可删除(delete)、改变(change)、添加(append)、插入(insert)、合、交换文件中的资料行,或读入其它档的资料到文 >件中,也可替换(substuite)它们其中的字串、或转换(tranfer)其中的字母……
注意
1、sed总是以行对输入进行处理
2、sed处理的不是原文件而是原文件的拷贝
3、要灵活使用地址加指令:熟练运用指令中位址参数和指令与指令间的配合,可以完成非凡任务。
sed GUN 首页 本文基于sed版本:sed version 4.2.1
工作原理
sed不是在原输入上直接进行处理的,而是先将读入的行放到缓冲区中,对缓冲区里的内容进行处理,处理完毕后也不会写回原文件(除非用shell的输出重定向来保存结果),而是直接输出到屏幕上。sed运行过程中维护着两个缓冲区,一个是活动的“模式空间(pattern space)”,另一个是起辅助作用的“暂存缓冲区(holding space)”。一般情况下,每当运行sed,sed首先把第一行装入模式空间,进行处理后输出到屏幕,然后将第二行装入模式空间替换掉模式空间里原来的内容,然后进行处理,以此类推。一般情况下暂存缓冲区是用不到的,但有特殊的命令可以在模式空间与暂存缓冲区之间交换数据,后文将有介绍。由于sed对文本的所有操作都是在缓冲区里进行的,所以不会对原文件造成任何破坏
简单表达:
sed执行模板=sed ‘模式{命令1;命令2}’
即逐行读入模式空间,执行命令,最后输出打印出来
语法
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
通俗地讲sed编辑指令格式如下
[add1[,add2]]fun[arg]
其中 , 位址参数 add1 、add2 为行数或 regular expression 字串 , 表示所执行编辑的资料行; 函数参数 fun[arg] 为 sed 的内定函数 , 表示执行的编辑动作。
选项
如果没有显式提供-e,–expresssion,-f或者–file选项,第一个非选项的参数将被当成sed脚本,其余的所有参数都是输入文件名称;如果没有指定输入文件,sed将会从标准输入读取。
命令说明
sed的命令的已知的主要摘要。
0个地址+cmd
0个地址或者一个地址+cmd
地址区间+cmd
地址
sed命令可以省略地址,此时sed命令会对所有在所有输入行上执行;若有一个地址,sed命令会在匹配的行上执行命令;再或者有两个地址,sed命令会在两个地址之间的所有行上执行,包括两个边界行,这两个地址需要用逗号隔开。第一个地址被接受,即使第二个地址在第一个之前也没有关系;若第二个地址是正则表达,则不会执行第一个地址匹配的行。
在地址(或者地址区间)之后,在命令之前,可以插入一个”!”,表示地址(或者地址区间)不匹配才执行命令。
地址类型支持
实例
准备文件(每个实例是独立的,修改数据后会还原数据为以下内容)
lingyue@lingyue-vm:~/test$vim testSed.txtthis is a sed test1 a is 1this is a sed test2 b is 2this is a sed test3 c is 3this is a sed test4 d is 4this is a sed test5 e is 5
1、-i选项,直接修改目标文件,有风险建议重新定向
lingyue@lingyue-vm:~/test$sed -i 's/test/final/g' testSed.txtlingyue@lingyue-vm:~/test$cat testSed.txtthis is a sed final1 a is 1this is a sed final2 b is 2this is a sed final3 c is 3this is a sed final4 d is 4this is a sed final5 e is 5
2、-e选项,允许多个编辑
lingyue@lingyue-vm:~/test$sed -e 's/test/final/g' -e 's/sed/S/g' testSed.txtthis is a S final1 a is 1this is a S final2 b is 2this is a S final3 c is 3this is a S final4 d is 4this is a S final5 e is 5
3、-n取消默认打印
lingyue@lingyue-vm:~/test$sed -n -e 's/test/final/g' -e 's/sed/S/g' testSed.txt
4、-f选项,制定sed文件
lingyue@lingyue-vm:~/test$vim s.seds/test/final/glingyue@lingyue-vm:~/test$sed -f s.sed testSed.txtthis is a sed final1 a is 1this is a sed final2 b is 2this is a sed final3 c is 3this is a sed final4 d is 4this is a sed final5 e is 5
5、s命令
sed ‘s/src/des/g’ file
lingyue@lingyue-vm:~/test$sed 's/test/final/g' testSed.txtthis is a sed final1 a is 1this is a sed final2 b is 2this is a sed final3 c is 3this is a sed final4 d is 4this is a sed final5 e is 5
配合-i选项可以直接修改文件或者使用重定向将修改后的结果放入新的文件
sed -i 's/test/final/g' filesed 's/test/final/g' file >outFileName
更多示例(输出省略):
$ sed -n 's/test/final/p' testSed.txt //(-n)选项和p标志一起使用表示只打印那些发生替换的行。也就是说,如果某一行开头的test被替换成final,就打印它。$ sed 's/test/&add/' testSed.txt //&符号表示替换换字符串中被找到的部份。所有匹配test的都会被替换成它自已加add,变成testadd。$ sed -n 's/\(test\)/\1er/p' testSed.txt //test被标记为1,所有test会被替换成tester,而且替换的行会被打印出来。$ sed 's#test#final#g' testSed.txt //不论什么字符,紧跟着s命令的都被认为是新的分隔符,所以,"#"在这里是分隔符,代替了默认的"/"分隔符。表示把所有test替换成final。
6、d命令
输出我给省略了~~
lingyue@lingyue-vm:~/test$sed '2d' testSed.txt //删除testSed.txt文件的第二行。lingyue@lingyue-vm:~/test$sed '2,$d' testSed.txt //删除testSed.txt文件的第二行到末尾所有行。lingyue@lingyue-vm:~/test$sed '$d' testSed.txt //删除testSed.txt文件的最后一行。lingyue@lingyue-vm:~/test$sed '/test/'d testSed.txt //删除testSed.txt文件所有包含test的行。
7、r命令,将文件中的内容读出来显示在匹配行下面
s.sed里的内容被读进来,显示在与test匹配的所有匹配行的下面
lingyue@lingyue-vm:~/test$sed '/test/r s.sed' testSed.txtthis is a sed test1 a is 1s/test/final/gthis is a sed test2 b is 2s/test/final/gthis is a sed test3 c is 3s/test/final/gthis is a sed test4 d is 4s/test/final/gthis is a sed test5 e is 5s/test/final/g
8、w命令
在testSed.txt中所有包含test的行都被写入output.txt里
lingyue@lingyue-vm:~/test$sed -n '/test/w output.txt' testSed.txtlingyue@lingyue-vm:~/test$cat output.txtthis is a sed test1 a is 1this is a sed test2 b is 2this is a sed test3 c is 3this is a sed test4 d is 4this is a sed test5 e is 5
9、选定行的范围
所有在模板test1和test3所确定的范围内的行都被打印。lingyue@lingyue-vm:~/test$sed -n '/test1/,/test3/p' testSed.txt this is a sed test1 a is 1this is a sed test2 b is 2this is a sed test3 c is 3打印从第3行开始到第一个包含以test5开始的行之间的所有行。lingyue@lingyue-vm:~/test$sed -n '3,/test5/p' testSed.txtthis is a sed test3 c is 3this is a sed test4 d is 4this is a sed test5 e is 5对于模板test1和test3之间的行,每行的末尾用字符串end替换。lingyue@lingyue-vm:~/test$sed '/test1/,/test3/s/$/end/' testSed.txtthis is a sed test1 a is 1endthis is a sed test2 b is 2endthis is a sed test3 c is 3endthis is a sed test4 d is 4this is a sed test5 e is 5
9、i命令
在匹配的行上面插入新的文本(斜杠也可以省略)
lingyue@lingyue-vm:~/test$sed '/test1/i\insertnewchar' testSed.txtinsertnewcharthis is a sed test1 a is 1this is a sed test2 b is 2this is a sed test3 c is 3this is a sed test4 d is 4this is a sed test5 e is 5
10、a命令
在匹配的行下面插入新的文本(斜杠也可以省略)
lingyue@lingyue-vm:~/test$sed '/test1/a\insertnewchar' testSed.txtthis is a sed test1 a is 1insertnewcharthis is a sed test2 b is 2this is a sed test3 c is 3this is a sed test4 d is 4this is a sed test5 e is 5
11、c命令
行替换lingyue@lingyue-vm:~/test$sed -e '1c\changed' testSed.txtchangedthis is a sed test2 b is 2this is a sed test3 c is 3this is a sed test4 d is 4this is a sed test5 e is 5字符替换lingyue@lingyue-vm:~/test$sed -e 's/test1/&changed/' testSed.txtthis is a sed testchanged1 a is 1this is a sed test2 b is 2this is a sed test3 c is 3this is a sed test4 d is 4this is a sed test5 e is 5
12、n命令读取偶数行
lingyue@lingyue-vm:~/test$sed -n 'n;p' testSed.txt this is a sed test2 b is 2 this is a sed test4 d is 4
解释:-n选项表示静默输出,即隐藏默认输出内容;读取第1行,执行n命令,此时模式空间为第2行,执行p,打印模式空间内容第2行内容,之后读取第3行,执行n命令,此时模式空间为第4行,执行p,打印模式空间内容第4行内容,之后读取第5行,执行n 命令,因为没有了,所以退出,并放弃p命令。
注意:还记不记得地址有一种写法:start~step,不清楚就往前翻找解释。
sed -n '2~2p' testSed.txt
13、N命令打印奇数行
lingyue@lingyue-vm:~/test$sed -n '$!N;P' testSed.txt this is a sed test1 a is 1this is a sed test3 c is 3 this is a sed test5 e is 5
注释:读取第1行,$!条件满足(不是尾行),执行N命令,得出”第1行内容\n第2行内容”,执行P,打印得第1行内容,读取第3行,$!条件满足(不是尾行),执行N命令,得出”第3行内容\n第4行内容”,执行P,打印得第3行内容,读取第5行,$!条件不满足,跳过N,执行P,打印得第5行内容
注意:同例12
sed -n '1~2p' testSed.txt
14、d命令
lingyue@lingyue-vm:~/test$sed 'n;d' testSed.txt this is a sed test1 a is 1this is a sed test3 c is 3 this is a sed test5 e is 5
注释:读取第1行,执行n,得出第2行内容,执行d,删除第2行内容,得空,以此类推,读取第3行,执行n,得出第4行内容,执行d,删除第4行内容,得空,但是读取第5行时,因为n无法执行,所以d不执行。因无-n参数,故输出”第1行内容\n第3行内容\n第5行内容”
15、D命令
lingyue@lingyue-vm:~/test$sed 'N;D' testSed.txt this is a sed test5 e is 5
注释:读取第1行,执行N,得出”第1行内容\n第2行内容”,执行D,得出第2行,执行N,得出”第2行内容\n第3行内容”,执行D,得出第3行,依此类推,得出第5行,执行N,条件失败退出,因无-n参数,故输出第5行
16、y命令替换字符
lingyue@lingyue-vm:~/test$sed 'y/es/ES/' testSed.txt thiS iS a SEd tESt1 a iS 1thiS iS a SEd tESt2 b iS 2thiS iS a SEd tESt3 c iS 3thiS iS a SEd tESt4 d iS 4thiS iS a SEd tESt5 E iS 5
lingyue@lingyue-vm:~/test$sed 's/\b[a-z]\b/\u&/g' testSed.txt this is A sed test1 A is 1this is A sed test2 B is 2this is A sed test3 C is 3this is A sed test4 D is 4this is A sed test5 E is 5
17、h命令,H命令,g命令,G命令
将testSed.txt文件中and前后数字和字母互换,并将字母大写
lingyue@lingyue-vm:~/test$cat sedScript.sedh { s/.* test[0-9] \(.*\) is.*/\1/ y/abcde/ABCDE/G s/\(.*\)\n\(.*test[0-9] \).*\(is \)\(.*\)/\2 \4 \3 \1/ } lingyue@lingyue-vm:~/test$sed -f sedScript.sed testSed.txtthis is a sed test1 1 is Athis is a sed test2 2 is Bthis is a sed test3 3 is Cthis is a sed test4 4 is Dthis is a sed test5 5 is E
我简单解释下这个例子:s/.* test[0-9] (.*) is.*/\1/ 将字母列取出;然后使用y执行元字符替换;执行G,追加保持空间内容到模式空间,得A\nthis is a sed test1 a is 1;执行s,重新排列,得出this is a sed test1 1 is A;逐行处理。
保持和互换:h命令和x命令
互换模式空间和保持缓冲区的内容lingyue@lingyue-vm:~/test$sed -e '/test1/h' -e '/test3/x' testSed.txtthis is a sed test1 a is 1this is a sed test2 b is 2this is a sed test1 a is 1this is a sed test4 d is 4this is a sed test5 e is 5
看匹配的test3的变化……
附录
sed替换标记
sed元字符集
原文链接
欢迎拍砖,未完待续……
- sed linux 命令
- linux sed命令详解
- linux sed命令详解
- linux sed命令详解
- linux sed命令详解
- linux sed命令详解
- linux sed 命令详解
- linux sed命令详解
- linux sed命令
- linux sed命令
- linux sed命令详解
- linux sed 命令详解
- linux sed命令详解
- linux sed命令详解
- linux sed命令详解
- linux sed命令详解
- linux sed命令
- linux sed命令详解
- Java 结构体之 JavaStruct 使用教程<三> JavaStruct 数组进阶
- 中国标准时间转换成DateTime
- POJ 3468 A Simple Problem with Integers (线段树 区间共加)
- 数据立方体----维度与OLAP
- lhgdialog个人整理
- linux命令---sed
- CUDA Samples: Dot Product
- Android开发控件-VarietyImageView(百变ImageView,可以根据要求指定哪个角是圆角,哪个角是直角)
- 《Python核心编程习题》---chapter9
- occi的基本操作
- 【二分图】 poj 1422 Air Raid
- 实现serializable序列化接口
- SAS篇-数据清洗基本技巧
- centos7语言更改