shell基础编程sed(编辑修改稿)内容摘要:

ough at 23:00. The local nurse Miss was in attendance. 删除第一到第三行: [sam@chenwy sam]$ sed 39。 1,3d39。 The local nurse Miss was in attendance. 删除最后一行: [sam@chenwy sam]$ sed 39。 $d39。 The honeysuckle band played all night long for only $90. It was an evening of splendid music and pany. Too bad the disco floor fell through at 23:00. 也可以使用正则表达式进行删除操作。 下面的例子删除包含文本 „ N e a v e‟的行。 [sam@chenwy sam]$ sed 39。 /Neave/d39。 The honeysuckle band played all night long for only $90. It was an evening of splendid music and pany. Too bad the disco floor fell through at 23:00. 替换文本 替换命令用替换模式替换指定模式,格式为: [ a d d r e s s [, address]] s/ patterntofind /replacementpattern/[g p w n] s选项通知 s e d这是一个替换操作,并查询 p a t t e r n t o f i n d,成功后用 r e p l a c e m e n t p a t t e r n替换它。 替换选项如下: g 缺省情况下只替换第一次出 现模式,使用 g选项替换全局所有出现模式。 p 缺省 s e d将所有被替换行写入标准输出,加 p选项将使 n选项无效。 n选项不打印输出结果。 w 文件名使用此选项将输出定向到一个文件。 如替换 n i g h t为 N I G H T,首先查询模式 n i g h t,然后用文本 N I G H T替换它。 [sam@chenwy sam]$ sed 39。 s/night/NIGHT/39。 The honeysuckle band played all NIGHT long for only $90. It was an evening of splendid music and pany. Too bad the disco floor fell through at 23:00. The local nurse Miss was in attendance. 要从 $ 9 0 中删除 $ 符号(记住这是一个特殊符号,必须用 \ 屏蔽其特殊含义),在 r e p l a c e m e n t p a t t e r n部分不写任何东西,保留空白,但仍需要用斜线括起来。 在 s e d中也可以这样删除一个字符串。 [sam@chenwy sam]$ sed 39。 s/\$//39。 The honeysuckle band played all night long for only 90. It was an evening of splendid music and pany. Too bad the disco floor fell through at 23:00. The local nurse Miss was in attendance. 要进行全局替换,即替换所有出现模式,只需在命令后加 g选项。 下面的例子将所有 T h e替换成 Wo w。 [sam@chenwy sam]$ sed 39。 s/The/Wow!/g39。 Wow! honeysuckle band played all night long for only $90. It was an evening of splendid music and pany. Too bad the disco floor fell through at 23:00. Wow! local nurse Miss was in attendance. 将替换结果写入一个文件用 w选项,下面的例子将 s p l e n d i d替换为 S P L E N D I D的替换结果写入文件 s e d . o u t: [sam@chenwy sam]$ sed 39。 s/splendid/SPLENDID/w 39。 The honeysuckle band played all night long for only $90. It was an evening of SPLENDID music and pany. Too bad the disco floor fell through at 23:00. The local nurse Miss was in attendance. 注意要将文件名括在 s e d的单引号里。 文件结果如下: [sam@chenwy sam]$ cat It was an evening of SPLENDID music and pany. 使用替换修改字 符串 如果要附加或修改一个字符串,可以使用( amp。 )命令, amp。 命令保存发现模式以便重新调用它,然后把它放在替换字符串里面。 先给出一个被替换模式,然后是一个准备附加在第一个模式后的另一个模式,并且后面带有 amp。 ,这样修改模式将放在匹配模式之前。 例如, s e d语句 s/nurse/Helloamp。 /p 的结果如下 [sam@chenwy sam]$ sed n 39。 s/nurse/hello amp。 /p39。 The local hello nurse Miss was in attendance. 原句是文本行 The local nurse Miss was in attendance。 记住模式中要使用空格,因为输出结果表明应加入空格。 还有一个例子: [sam@chenwy sam]$ sed n 39。 s/played/from Hockering amp。 /p39。 The honeysuckle band from Hockering played all night long for only $90. 原句是 The honeysuckle band played all night long for only $90。 将 sed结果写入文件命令 像使用 文件重定向发送输出到一个文件一样,在 s e d命令中也可以将结果输入文件。 格式有点像使用替换命令: [ a d d r e s s [, address]]w filename „w‟选项通知 s e d将结果写入文件。 f i l e n a m e是自解释文件名。 下面有两个例子。 [sam@chenwy sam]$ sed 39。 1,2 w filedt39。 The honeysuckle band played all night long for only $90. It was an evening of splendid music and pany. Too bad the disco floor fell through at 23:00. The local nurse Miss was in attendance. 文件 q u o t e . t x t输出到屏幕。 模式范围即 1, 2行输出到文件 f i l e d t。 [sam@chenwy sam]$ cat filedt The honeysuckle band played all night long for only $90. It was an evening of splendid music and pany. 下面例子中查询模式 N e a v e,匹配结果行写入文件 f i l e d h t。 [sam@chenwy sam]$ sed 39。 /Neave/ w dht39。 The honeysuckle band played all night long for only $90. It was an evening of splendid music and pany. Too bad the disco floor fell through at 23:00. The local nurse Miss was in attendance. [sam@chenwy sam]$ cat dht The local nurse Miss was in attendance. 从文件中读文本 处理文件时, s e d允许从另一个文件中读文本,并将其文本附加在当前文件。 此命令放在模式匹配行后,格式为: address r filename 这里 r通知 s e d将从另一个文件源中读文本。 f i l e n a m e是其文件名。 现在创建一个小文件 s e d e x . t x t,内容如下: [sam@chenwy sam]$ echo Boom boom went the music [sam@chenwy sam]$ cat Boom boom went the music 将 s e d e x . t x t内容附加到文件 q u o t e . t x t的拷贝。 在模式匹配行 / c o m p a n y /后放置附加文本。 本例为第三行。 注意所读的文件名需要用单引号括起来。 [sam@chenwy sam]$ sed 39。 /pany./r 39。 The honeysuckle band played all night long for onl。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。