sed详解+实例
2009-10-15 18:53
691 查看
代码:
s e d从文件的一个文本行或从标准输入的几种格式中读取数据,将之拷贝到一个编辑缓冲区,然后读命令行或脚本的第一条命令,并使用这些命令查找模式或定位行号编辑它。重复此过程直到命令结束。
(2)调用sed
调用s e d有三种方式:
a.在命令行键入命令; sed [选项] s e d命令输入文件
b.将s e d命令插入脚本文件,然后调用s e d; sed [选项] -f sed脚本文件输入文件
c.将s e d命令插入脚本文件,并使s e d脚本可执行。 sed脚本文件 [选项] 输入文件
2.sed选项
s e d选项如下:
-n 不打印;s e d不写编辑行到标准输出,缺省为打印所有行(编辑和未编辑)。p命令可以用来打印编辑行。
-f 如果正在调用s e d脚本文件,使用此选项。此选项通知s e d一个脚本文件支持所有的s e d命令,例如:sed -f myscript.sed input_file,这里m y s c r i p t . s e d即为支持s e d命令的文件。
-c 下一命令是编辑命令。使用多项编辑时加入此选项。如果只用到一条s e d命令,此选项无用,但指定它也没有关系。
-i 编辑原文件(此选项慎用,如果使用则原文件就会被修改,无法恢复)。
3.保存sed输出
a.重定向(下面将sed命令的所有输出至文件 output-file 中)
代码:
r 从另一个文件中读文本代码:
代码:
代码:
代码:
要用sed把字符“\”转化成“'”该怎么写?
错误解法:
代码:
题目
文件内容file.txt:
123456 345678 2005-05-06 123456
123456 234567 2003-5-6 234567
345555 987644 2003-4-23 543333
555555 999999 2004-11-5 999999
要将第四列数据变成正常的年月日,将2003-5-6 变成2003-05-0;
2003-4-23变成2003-04-23; 2004-11-5变成 2004-11-05
解答
首先将需要改变的部分分域代码:
首先
创建sed脚本文件-->赋予执行权限-->执行文件
sed [-n] [-e] 'command' file(s)sed [-n] -f scriptfile file(s)(1)sed怎样读取数据
s e d从文件的一个文本行或从标准输入的几种格式中读取数据,将之拷贝到一个编辑缓冲区,然后读命令行或脚本的第一条命令,并使用这些命令查找模式或定位行号编辑它。重复此过程直到命令结束。
(2)调用sed
调用s e d有三种方式:
a.在命令行键入命令; sed [选项] s e d命令输入文件
b.将s e d命令插入脚本文件,然后调用s e d; sed [选项] -f sed脚本文件输入文件
c.将s e d命令插入脚本文件,并使s e d脚本可执行。 sed脚本文件 [选项] 输入文件
2.sed选项
s e d选项如下:
-n 不打印;s e d不写编辑行到标准输出,缺省为打印所有行(编辑和未编辑)。p命令可以用来打印编辑行。
-f 如果正在调用s e d脚本文件,使用此选项。此选项通知s e d一个脚本文件支持所有的s e d命令,例如:sed -f myscript.sed input_file,这里m y s c r i p t . s e d即为支持s e d命令的文件。
-c 下一命令是编辑命令。使用多项编辑时加入此选项。如果只用到一条s e d命令,此选项无用,但指定它也没有关系。
-i 编辑原文件(此选项慎用,如果使用则原文件就会被修改,无法恢复)。
3.保存sed输出
a.重定向(下面将sed命令的所有输出至文件 output-file 中)
代码:
zhyfly@zhyfly:~/bash$ cat test.txtThe honeysuckle band played all night long for only $90.It was an evening of splendid music and company.Too bad the disco floor fell through at 23:00.The local nurse Miss P.Neave was in attendance.zhyfly@zhyfly:~/bash$ sed -e '1,2w test.bak' test.txtThe honeysuckle band played all night long for only $90.It was an evening of splendid music and company.Too bad the disco floor fell through at 23:00.The local nurse Miss P.Neave was in attendance.zhyfly@zhyfly:~/bash$ cat test.bakThe honeysuckle band played all night long for only $90.It was an evening of splendid music and company.另外,同样
r 从另一个文件中读文本代码:
zhyfly@zhyfly:~/bash$ sed -e '1p' test.txt The honeysuckle band played all night long for only $90.The honeysuckle band played all night long for only $90.It was an evening of splendid music and company.Too bad the disco floor fell through at 23:00.The local nurse Miss P.Neave was in attendance.zhyfly@zhyfly:~/bash$ sed -n -e '1p' test.txtThe honeysuckle band played all night long for only $90.代码:
zhyfly@zhyfly:~/bash$ sed -n -e '/company/p' test.txtIt was an evening of splendid music and company.代码:
zhyfly@zhyfly:~/bash$ sed -e '=' test.txt1The honeysuckle band played all night long for only $90.2It was an evening of splendid music and company.3Too bad the disco floor fell through at 23:00.4The local nurse Miss P.Neave was in attendance.zhyfly@zhyfly:~/bash$ sed -n -e '=' test.txt1234zhyfly@zhyfly:~/bash$ sed -n -e '/music/p' test.txtIt was an evening of splendid music and company.zhyfly@zhyfly:~/bash$ sed -n -e '/music/=' test.txt2zhyfly@zhyfly:~/bash$ sed -n -e '/music/p' -e '/music/=' test.txtIt was an evening of splendid music and company.2zhyfly@zhyfly:~/bash$ sed -e '=;p' test.txt1The honeysuckle band played all night long for only $90.The honeysuckle band played all night long for only $90.2It was an evening of splendid music and company.It was an evening of splendid music and company.3Too bad the disco floor fell through at 23:00.Too bad the disco floor fell through at 23:00.4The local nurse Miss P.Neave was in attendance.The local nurse Miss P.Neave was in attendance.zhyfly@zhyfly:~/bash$ sed -n -e '=;p' test.txt1The honeysuckle band played all night long for only $90.2It was an evening of splendid music and company.3Too bad the disco floor fell through at 23:00.4The local nurse Miss P.Neave was in attendance.zhyfly@zhyfly:~/bash$ sed -n -e '=' -e 'p' test.txt1The honeysuckle band played all night long for only $90.2It was an evening of splendid music and company.3Too bad the disco floor fell through at 23:00.4The local nurse Miss P.Neave was in attendance.zhyfly@zhyfly:~/bash$附加 [address]a\附加内容 #缺省放在每一行后面
代码:
zhyfly@zhyfly:~/bash$ sed -e 'c\this line will be modified to the each line!oooooooooo' test.txtthis line will be modified to the each line!oooooooooothis line will be modified to the each line!oooooooooothis line will be modified to the each line!oooooooooothis line will be modified to the each line!oooooooooozhyfly@zhyfly:~/bash$ sed -e '/music/c\this line will be modified to the matching line!oooooooooo' test.txtThe honeysuckle band played all night long for only $90.this line will be modified to the matching line!ooooooooooToo bad the disco floor fell through at 23:00.The local nurse Miss P.Neave was in attendance.删除定位行
代码:
zhyfly@zhyfly:~/bash$ cat test.txtThe honeysuckle band played all night long for only $90.It was an evening of splendid music and company.Too bad the disco floor fell through at 23:00.The local nurse Miss P.Neave was in attendance.zhyfly@zhyfly:~/bash$ sed -e 's/$90/&230/g' test.txtThe honeysuckle band played all night long for only $90230.It was an evening of splendid music and company.Too bad the disco floor fell through at 23:00.The local nurse Miss P.Neave was in attendance.zhyfly@zhyfly:~/bash$ sed -e 's/90/120,&/g' test.txtThe honeysuckle band played all night long for only $120,90.It was an evening of splendid music and company.Too bad the disco floor fell through at 23:00.The local nurse Miss P.Neave was in attendance.分隔符变换(避免产生歧义)
代码:
zhyfly@zhyfly:~/bash$ sed -e 's/<[^>]*>//g' testThis is what I meant.题目
要用sed把字符“\”转化成“'”该怎么写?
错误解法:
代码:
/^\([0-9]*\)\([A-Z]*\)\([0-9]*\)/\1 \2 \3/代码:
zhyfly@zhyfly:~/bash$ sed -e 's/^\([0-9]*\)\([A-Z]*\)\([0-9]*\)/\1 \2 \3 /g' file1.txt|sort +1 -2 +2n +0 -11 C 21 C 32 C 31 C 42 C 41 C 311 C 321 D 11 D 21 D 31 D 101 D 121 D 311 RC 21 RC 31 RC 201 RC 211 RC 311 WR 11 WR 21 WR 201 WR 211 WR 23下面的例子比较复杂:
题目
文件内容file.txt:
123456 345678 2005-05-06 123456
123456 234567 2003-5-6 234567
345555 987644 2003-4-23 543333
555555 999999 2004-11-5 999999
要将第四列数据变成正常的年月日,将2003-5-6 变成2003-05-0;
2003-4-23变成2003-04-23; 2004-11-5变成 2004-11-05
解答
首先将需要改变的部分分域代码:
zhyfly@zhyfly:~/bash$ sed -e '2,3s/a/ooooo/g' -e '2,3s/d/ddddd/g' test.txtThe honeysuckle band played all night long for only $90.It wooooos ooooon evening of splendddddiddddd music ooooonddddd compooooony.Too boooooddddd the dddddisco floor fell through ooooot 23:00.The local nurse Miss P.Neave was in attendance.zhyfly@zhyfly:~/bash$ sed -e '2,3s/a/ooooo/g;2,3s/d/ddddd/g' test.txtThe honeysuckle band played all night long for only $90.It wooooos ooooon evening of splendddddiddddd music ooooonddddd compooooony.Too boooooddddd the dddddisco floor fell through ooooot 23:00.The local nurse Miss P.Neave was in attendance.前两种方法比较简单,下面重点讲一下第三种方法
首先
创建sed脚本文件-->赋予执行权限-->执行文件
相关文章推荐
- sed命令详解和实例
- Linux sed命令实例详解
- Linux sed命令实例详解
- sed命令详解及应用实例
- shell sed实例详解
- sed命令详解及实例
- shell sed实例详解
- Sed命令详解及应用实例
- sed命令详解及应用实例
- 瑞士军刀:文本处理工具Sed用法与实例详解
- 详解java 中Spring jsonp 跨域请求的实例
- 一个应用实例详解卡尔曼滤波及其算法实现
- Linux Rootkit系列三:实例详解 Rootkit 必备的基本功能
- 转:OpenCV学习之CvMat的用法详解及实例
- Java的位运算符详解实例——与(&)、非(~)、或(|)、异或(^)
- maven多模块web项目完整实例详解(附项目源码)
- Oracle Merge Into 的用法详解实例
- Angular.js之作用域scope'@','=','&'实例详解
- sed命令使用详解
- Spring事务管理(详解+实例)