AIX下的sed介绍
sed:是一个非交互性文本流编辑器,可编辑大或小的文件,sed命令定时编辑,删除文件;一次性处理所有改变。sed不与原文件打交道,只是操作源文件的一个拷贝,然后所有的改动输出到一个文件,并输出到屏幕。调用sed的三种方式:1.命令行键入命令2.将sed命令插入脚本文件,然后调用sed3.将sed命令插入脚本文件,并使sed脚本可执行?自动执行?调用sed1.命令格式sed [选项] sed命令 输入文件2.使用sed脚本sed [选项] -f sed脚本文件 输入文件3.使用第一行具有sed命令解释器的sed脚本文件sed脚本 [选项] 输入文件选项:n不打印c下一命令是编辑命令[无用]f正在调用sed脚本文件保存sed命令,可以使用重定向方式:sed 'sed_cmd' input_file > outfilesed浏览文件时,默认从第一行开始浏览,若想定位文本,有2种方式:1.使用行号,单行或多行(范围)2.正则表达式使用sed在文件中定位文本的方式——————————————————————————————————————————————————————————————————xx为行号,如1x,y表示行号范围从x到y/pattern/查询包含模式的行。例如/disk/或/[a-z]//pattern/pattern/查询包含两个模式的行,例如/disk/disks/pattern/,x在给定行号上查询包含模式的行,例如/ribbon/,3x,/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.txtpg quote.txtthe 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.txtThe honeysuckle band played all night long for noly $90.打印最后一行:sed -n '$p' quote.txtThe local nurse Miss P.Neave was in attendance.打印整个文件,格式:1,$psed -n '1,$p' quote.txtThe 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.txtThe 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.txtIt was an evening of splendid music and company.查找包含$符号的行:sed -n '/\$/'p quote.txtThe honeysuckle band played all night long for noly $90.筛选出一行,格式:line_num,/pattern/sed -n '4,/was/'p quote.txtThe local nurse Miss P.Neave was in attendance.打印每行的行号:sed -e '=' quote.txt1The honeysuckle band played all night long for noly $90.2It was an evening of splendid music and company.3Too bad the disco floor fell through at 23:10.4The local nurse Miss P.Neave was in attendance.打印包含was单词的行的行号:sed -n '/was/=' quote.txt24包含was的行:sed -n '/was/'p quote.txtIt 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.txt2It was an evening of splendid music and company.--the end--