Chinaunix首页 | 论坛 | 博客
  • 博客访问: 292885
  • 博文数量: 49
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 901
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-31 14:32
个人简介

追求卓越 因为经历,所以幸福

文章分类

全部博文(49)

文章存档

2015年(5)

2014年(30)

2013年(14)

分类: LINUX

2014-02-19 16:31:19

sed命令的基本语法

1.       Sed命令的工作流程:

Sed命令对文本的搜索是一行一行进行。

Man手册的语法描述:

NAME

       sed - stream editor for filtering and transforming text

SYNOPSIS

       sed [OPTION]... {script-only-if-no-other-script} [input-

       file]...

DESCRIPTION

       Sed is a stream editor.  A stream editor is used to per-

       form  basic  text  transformations on an input stream (a

       file or input from a pipeline).  While in some ways sim-

       ilar  to an editor which permits scripted edits (such as

       ed),  sed  works  by  making  only  one  pass  over  the

       input(s), and is consequently more efficient.  But it is

       sed’s ability to filter text in a pipeline which partic-

       ularly distinguishes it from other types of editors.

       -n,  --quiet, --silent

              suppress automatic printing of pattern space

       -e  script, --expression=script

              add the script to the commands to be executed

       -f  script-file, --file=script-file

              add  the  contents of script-file to the commands

              to be executed

   p      Print the current pattern space.

   P      Print  up  to  the  first embedded newline of the

              current pattern space.

s  /regexp/replacement/

              Attempt  to  match  regexp  against  the  pattern

              space.    If  successful,  replace  that  portion

              matched with replacement.   The  replacement  may

              contain  the special character & to refer to that

              portion of the pattern space which  matched,  and

              the special escapes \1 through \9 to refer to the

              corresponding  matching  sub-expressions  in  the

              regexp.

   w   filename

              Write the current pattern space to filename.

   W   filename

              Write the first line of the current pattern space

              to filename.  This is a GNU extension.

A.     sed 一般命令格式:

sed [options] {sed-commands} {input-file}

1)        p 命令与-n 选项(print, --quiet –silent

只打印第一行

$ sed -n '1 p' example.txt

打印13

              $sed -n '1,3 p' example.txt

      

       选项中加上-n与没加-n的区别:命令对比  $ sed '1,3 p' example.txt

      

       打印Jason 所在行

sed -n '/Jason/ p' example.txt

打印Jason Jane 所在的行

sed -n '/Jason/,/Jane/ p' example.txt

打印以103 开头的行

sed -n '/^103/ p' example.txt

打印以Manager 结尾的行

sed -n '/Manager$/ p' example.txt

以数字开头,而且是连续重复三次

sed -n '/^[0-9]\{3\}/ p' example.txt

每隔一行打印一次

sed -n '1~2 p' example.txt

2)        d 命令(delete

d命令与p命令基本用法一致

删除103 字符串所在的行

sed '/103/ d' example.txt

3)        -f选项(-f 从文件中读入命令)

新建cmd.sed 命令文件,文件内容如下: /Jane/ p

在终端输入命令:sed -f cmd.sed example.txt

注意:可在以命令文件中写多个命令

4)        -e 选项(执行多个命令)

打印Jason Jane 所在的行

形式一:sed -n -e '/John/ p' -e '/Jane/ p' example.txt

              形式二:sed -n -e '/John/ p;/Jane/ p' example.txt

B.     替换命令格式

sed '[address-range|pattern-range]s/originalstring/replacement-string/[substitute-flags]'inputfile

 

address-range:地址列表,表示从哪个地方开始执行,如:13 表示第一行

到第3 行。

pattern-range:样式列表,表示从哪个样式匹配串开始。如:/Jane/,表示

从行中包含字符串Jane 的行开始执行。[]中的都是可选的。

s:表示要执行替换命令(substitute)

originalstring:需要被替换的字符串

replacement-string替换后的字符串。

substitute-flags:可选替换标志符

1)        address-range pattern-range

a) 把第一行的John 替换成guozi 字符串

sed '1 s/John/guozi/' example.txt

b) 把第二行至第五行的Manager 替换成guozi

sed '2,5 s/Manager/guozi/' example.txt

c) John 所以字符串行当中的CEO 替换成XXX

sed '/John/ s/CEO/XXX/' example.txt

d) 从第四行而且包含字符串Ram 的行中,把Developer 字符串替换成XXX

sed '4,/Ram/ s/Developer/XXX/' example.txt

e) 如果没有address-range pattern-range,那么就会把每一行碰到的第一

个匹配字符串给替换掉。

sed 's/1/AAA/' example.txt

2)        substitute-flags

a)        全局标志g(globe)

全局标志会把遇到的所有满足条件的字符串给替换掉

sed 's/1/AAA/g' example.txt

b)        数字标志1,2,3

把要匹配串中的第2个符合条件的匹配串替换成我们想要的字符串

sed 's/1/AAA/2 example.txt

c)        又见p(print)标志

sed -n 's/1/AAA/p' example.txt

d)       写标志w(write)

把每一行碰到第一个字符串John 替换成guozi 字符串,并写入

output.txt

sed 's/John/guozi/w output.txt' example.txt

对比重定向:

重定向会把所以输出到终端的内容全部放到文本当中,而w        开关只把

我们所做过修改的行写入文件当中。

           sed 's/John/guozi/' example.txt >output.txt

e)        敏感标志i(ignore flag)

忽略大小写,把文本当中所有包含jason 字符串的行替换成AAAA 字符串

sed 's/jason/AAAA/gi' example.txt

f)         联合使用标志

可以使用多个标志来对字符串进行替换并保存

sed 's/jason/AAAA/gipw output2.txt' example.txt

3)        替换命令定界符

格式1Sed s/// example.txt

格式2Sed s@@@ example.txt

用处使用合理的定界符可以方便的阅读我们的程序代码。

4)        强大的&——样式匹配

origin-string 替换到replace-string 当中

sed 's/John/[&]/' example.txt

需求:将开头为三位数字的外面再加一层{}

形式一:sed 's/^[0-9]\{3\}/{&}/' example.txt

形式二:sed 's/^[0-9].*/{&}/' example.txt

C.     使用a 命令追加行

格式:sed '[address] a the-line-to-append' input-file

示例:

1) 在文本的第2行后追加一行

sed '2 a 203,Jack Johnson,Engineer' employee.txt

2) 在文本最后追加一行

sed '$ a 106,Jack Johnson,Engineer' employee.txt

3) 在文本匹配处增加多行

sed '/Jason/a\

203,Jack Johnson,Engineer\

204,Mark Smith,Sales Engineer' employee.txt

注意:反斜杠“\”续行符的使用

D.     使用i命令插入行

格式:sed '[address] i the-line-to-insert' input-file

用法跟a命令类似,不同的是在匹配地址所有行之前

E.     使用c命令修改行

格式:sed '[address] c the-line-to-insert' input-file

意义不同,用法类似

F.      使用l命令打印隐藏字符

格式:sed n l input-file

G.    使用=命令打印行号

格式:sed = input-file

打印字符串所在行号

sed /string/ = input-file

H.    使用y命令作字符转换

格式: sed y/original-string/replace-string/ input-file

示例:sed 'y/abc/ABC/' employee.txt

可用于加密字符串

I.        使用q命令终止执行

示例:  sed 5 q input-file 执行完第5行后终止

sed /root/        q input-file匹配到root行后退出

本文中的一些图片无法显示可转到CSDN http://blog.csdn.net/sdreamq/article/details/19493649



阅读(1669) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~