您好,欢迎访问三七文档
当前位置:首页 > 医学/心理学 > 药学 > Shell 基础十二篇 第五部分
Utterconfusionfollowed.执行结果CODE:[sam@chenwysam]$chmodu+xinsert.sed[sam@chenwysam]$./insert.sedquote.txtThehoneysucklebandplayedallnightlongforonly$90.Itwasaneveningofsplendidmusicandcompany.Toobadthediscofloorfellthroughat23:00.Utterconfusionfollowed.ThelocalnurseMissP.Neavewasinattendance.修改文本修改文本修改文本修改文本修改命令将在匹配模式空间的指定行用新文本加以替代,格式如下:将第一行Thehoneysucklebandplayedallnightlongforonly$90替换为TheofficeDibblebandplayedwell。首先要匹配第一行的任何部分,可使用模式‘/Honeysuckle/’。sed脚本文件为change.sed。内容如下:CODE:[sam@chenwysam]$catchange.sed#!/bin/sed-f3c\TheofficeDibblebandplayedwell.CODE:[sam@chenwysam]$chmodu+xchange.sed[sam@chenwysam]$./change.sedquote.txtThehoneysucklebandplayedallnightlongforonly$90.Itwasaneveningofsplendidmusicandcompany.TheofficeDibblebandplayedwell.ThelocalnurseMissP.Neavewasinattendance.或命令行:CODE:[sam@chenwysam]$sed/honeysuck/c\TheOfficeDibblebandplayedwell.quote.txtTheOfficeDibblebandplayedwell.Itwasaneveningofsplendidmusicandcompany.Toobadthediscofloorfellthroughat23:00.ThelocalnurseMissP.Neavewasinattendance.可以对同一个脚本中的相同文件进行修改、附加、插入三种动作匹配和混合操作。CODE:[sam@chenwysam]$catmix.sed#!/bin/sed-f1c\TheDibblebandweregrooving./evening/i\Theyplayedsomegreattunes.3a\Wherewasthenursetohelp?CODE:[sam@chenwysam]$chmodu+xmix.sed[sam@chenwysam]$./mix.sedquote.txtTheDibblebandweregrooving.Theyplayedsomegreattunes.Itwasaneveningofsplendidmusicandcompany.Toobadthediscofloorfellthroughat23:00.Wherewasthenursetohelp?ThelocalnurseMissP.Neavewasinattendance.删除文本删除文本删除文本删除文本sed删除文本格式:CODE:[address[,address]]d删除第一行;1d意为删除第一行。CODE:[sam@chenwysam]$sed'1d'quote.txtItwasaneveningofsplendidmusicandcompany.Toobadthediscofloorfellthroughat23:00.ThelocalnurseMissP.Neavewasinattendance.删除第一到第三行:CODE:[sam@chenwysam]$sed'1,3d'quote.txtThelocalnurseMissP.Neavewasinattendance.删除最后一行:CODE:[sam@chenwysam]$sed'$d'quote.txtThehoneysucklebandplayedallnightlongforonly$90.Itwasaneveningofsplendidmusicandcompany.Toobadthediscofloorfellthroughat23:00.也可以使用正则表达式进行删除操作。下面的例子删除包含文本‘Neave’的行。CODE:[sam@chenwysam]$sed'/Neave/d'quote.txtThehoneysucklebandplayedallnightlongforonly$90.Itwasaneveningofsplendidmusicandcompany.Toobadthediscofloorfellthroughat23:00.替换文本替换文本替换文本替换文本替换命令用替换模式替换指定模式,格式为:CODE:[address[,address]]s/pattern-to-find/replacement-pattern/[gpwn]s选项通知sed这是一个替换操作,并查询pattern-to-find,成功后用replacement-pattern替换它。替换选项如下:QUOTE:g缺省情况下只替换第一次出现模式,使用g选项替换全局所有出现模式。p缺省sed将所有被替换行写入标准输出,加p选项将使-n选项无效。-n选项不打印输出结果。w文件名使用此选项将输出定向到一个文件。如替换night为NIGHT,首先查询模式night,然后用文本NIGHT替换它。CODE:[sam@chenwysam]$sed's/night/NIGHT/'quote.txtThehoneysucklebandplayedallNIGHTlongforonly$90.Itwasaneveningofsplendidmusicandcompany.Toobadthediscofloorfellthroughat23:00.ThelocalnurseMissP.Neavewasinattendance.要从$90中删除$符号(记住这是一个特殊符号,必须用\屏蔽其特殊含义),在replacement-pattern部分不写任何东西,保留空白,但仍需要用斜线括起来。在sed中也可以这样删除一个字符串。CODE:[sam@chenwysam]$sed's/\$//'quote.txtThehoneysucklebandplayedallnightlongforonly90.Itwasaneveningofsplendidmusicandcompany.Toobadthediscofloorfellthroughat23:00.ThelocalnurseMissP.Neavewasinattendance.要进行全局替换,即替换所有出现模式,只需在命令后加g选项。下面的例子将所有The替换成Wow!。CODE:[sam@chenwysam]$sed's/The/Wow!/g'quote.txtWow!honeysucklebandplayedallnightlongforonly$90.Itwasaneveningofsplendidmusicandcompany.Toobadthediscofloorfellthroughat23:00.Wow!localnurseMissP.Neavewasinattendance.将替换结果写入一个文件用w选项,下面的例子将splendid替换为SPLENDID的替换结果写入文件sed.out:CODE:[sam@chenwysam]$sed's/splendid/SPLENDID/wsed.out'quote.txtThehoneysucklebandplayedallnightlongforonly$90.ItwasaneveningofSPLENDIDmusicandcompany.Toobadthediscofloorfellthroughat23:00.ThelocalnurseMissP.Neavewasinattendance.注意要将文件名括在sed的单引号里。文件结果如下:CODE:[sam@chenwysam]$catsed.outItwasaneveningofSPLENDIDmusicandcompany.使用替换修改字符串使用替换修改字符串使用替换修改字符串使用替换修改字符串如果要附加或修改一个字符串,可以使用(&)命令,&命令保存发现模式以便重新调用它,然后把它放在替换字符串里面。先给出一个被替换模式,然后是一个准备附加在第一个模式后的另一个模式,并且后面带有&,这样修改模式将放在匹配模式之前。例如,sed语句s/nurse/Hello&/p的结果如下CODE:[sam@chenwysam]$sed-n's/nurse/hello&/p'quote.txtThelocalhellonurseMissP.Neavewasinattendance.原句是文本行ThelocalnurseMissP.Neavewasinattendance。记住模式中要使用空格,因为输出结果表明应加入空格。还有一个例子:CODE:[sam@chenwysam]$sed-n's/played/fromHockering&/p'quote.txtThehoneysucklebandfromHockeringplayedallnightlongforonly$90.原句是Thehoneysucklebandplayedallnightlongforonly$90。将将将将sed结果写入文件命令结果写入文件命令结果写入文件命令结果写入文件命令像使用;文件重定向发送输出到一个文件一样,在sed命令中也可以将结果输入文件。格式有点像使用替换命令:CODE:[address[,address]]wfilename‘w’选项通知sed将结果写入文件。filename是自解释文件名。下面有两个例子。CODE:[sam@chenwysam]$sed'1,2wfiledt'quote.txtThehoneysucklebandplayedallnightlongforonly$90.Itwasaneveningofsplendidmusicandcompany.Toobadthediscofloorfellthroughat23:00.ThelocalnurseMissP.Neavewasinattendance.文件quote.txt输出到屏幕。模式范围即1,2行输出到文件filedt。CODE:[sam@chenwysam]$catfiledtThehoneysucklebandplayedallnightlongforonly$90.Itwasaneveningofsplendidmusicandcompany.下面例子中查询模式Neave,匹配结果行写入文件filedht。CODE:[sam@chenwysam]$sed'/Neave/wdht'quote.txtThehoneysucklebandplayedallnightlongforonly$90.Itwasaneveningofsplendidmusicandcompany.Toobadthediscofloorfellthroughat23:00.ThelocalnurseMissP.Neavewasinattendance.CODE:[sam@chenwysam]$catdhtThelocalnurseMissP.Neavewasinattendance.从文件中读文本从文件中读文本从
本文标题:Shell 基础十二篇 第五部分
链接地址:https://www.777doc.com/doc-6340920 .html