AIX下的sed命令介绍



AIX下的sed命令介绍
 
[plain]    www.2cto.com  
sed:是一个非交互性文本流编辑器,可编辑大或小的文件,sed命令定时编辑,删除文件;  
一次性处理所有改变。  
sed不与原文件打交道,只是操作源文件的一个拷贝,然后所有的改动输出到一个文件,并输出到屏幕。
  
调用sed的三种方式:  
1.命令行键入命令  
2.将sed命令插入脚本文件,然后调用sed  
3.将sed命令插入脚本文件,并使sed脚本可执行?自动执行?  
  
调用sed  
1.命令格式  
sed [选项] sed命令 输入文件  
2.使用sed脚本  
sed [选项] -f sed脚本文件 输入文件  
3.使用第一行具有sed命令解释器的sed脚本文件  
sed脚本 [选项] 输入文件  
  
选项:  
n   不打印  
c   下一命令是编辑命令[无用]  
f   正在调用sed脚本文件  
  
保存sed命令,可以使用重定向方式:sed 'sed_cmd' input_file > outfile  
  
sed浏览文件时,默认从第一行开始浏览,若想定位文本,有2种方式:  
1.使用行号,单行或多行(范围)  
2.正则表达式  
  
使用sed在文件中定位文本的方式  
——————————————————————————————————————————————————————————————————  
x           x为行号,如1  
x,y         表示行号范围从x到y  
/pattern/       查询包含模式的行。例如/disk/或/[a-z]/  
/pattern/pattern/   查询包含两个模式的行,例如/disk/disks/  
pattern/,x      在给定行号上查询包含模式的行,例如/ribbon/,3  
x,/pattern/     通过行号和模式查询匹配行,3,/vdu/  
x,y!            查询不包含指定行号x和y的行,1,2!  
——————————————————————————————————————————————————————————————————  
sed编辑命令  
————————————————————————————————————————————————————————  
p       打印匹配行  
=       显示文件行号  
a\      在定位行号后附加新文本信息  
i\      在定位行号后插入新文本信息  
d       删除定位行  
c\      在新文本替换定位文本  
s       使用替换模式替换相应模式  
r       从另一个文本中读取文本  
w       写文本到一个文件  
q       第一个模式匹配完成后推出或立即推出  
l       显示与八进制ASCII代码等价的控制字符  
{}      在定位行执行的命令组  
n       从另一个文本中读取下一行,并附加在下一行  
g       将模式2粘贴到/pattern n/  
y       传递字符  
n       延续到下一输入行;允许跨行的模式匹配语句  
——————————————————————————————————————————————————————————  
建立一个文件:  
vi quote.txt  
pg quote.txt  
the honeysuckle band played all night long for noly $90.  
It was an evening of splendid music and company.  
Too bad the disco floor fell through at 23:10.  
The local nurse Miss P.Neave was in attendance.  
  
打印首行:  
sed -n '1p' quote.txt  
The honeysuckle band played all night long for noly $90.  
  
打印最后一行:  
sed -n '$p' quote.txt  
The local nurse Miss P.Neave was in attendance.  
  
打印整个文件,格式:1,$p  
sed -n '1,$p' quote.txt  
The honeysuckle band played all night long for noly $90.  
It was an evening of splendid music and company.  
Too bad the disco floor fell through at 23:10.  
The local nurse Miss P.Neave was in attendance.  
  
打印指定行  
sed -n '4p' quote.txt  
The local nurse Miss P.Neave was in attendance.  
  
使用正则表达式  
打印包含was单词的行;  
sed -n '/was/'p  quote.txt[or sed -n '/was/p'  quote.txt]  
It was an evening of splendid music and company.  
The local nurse Miss P.Neave was in attendance.  
  
检索数据:打印出现以ing结尾单词的行  
sed -n '/.*ing/'p quote.txt  
It was an evening of splendid music and company.  
  
查找包含$符号的行:  
sed -n '/\$/'p quote.txt  
The honeysuckle band played all night long for noly $90.  
  
筛选出一行,格式:line_num,/pattern/  
sed -n '4,/was/'p quote.txt  
The local nurse Miss P.Neave was in attendance.  
  
打印每行的行号:  
sed -e '=' quote.txt  
1  
The honeysuckle band played all night long for noly $90.  
2  
It was an evening of splendid music and company.  
3  
Too bad the disco floor fell through at 23:10.  
4  
The local nurse Miss P.Neave was in attendance.  
  
打印包含was单词的行的行号:  
sed -n '/was/=' quote.txt  
2  
4  
  
包含was的行:  
sed -n '/was/'p quote.txt  
It was an evening of splendid music and company.  
The local nurse Miss P.Neave was in attendance.  
  
打印包含music的行号,和行数据:  
sed -e '/music/=' -n -e '/music/p' quote.txt  
2  
It was an evening of splendid music and company.  
--the end--
 

相关内容

    暂无相关文章