sed基础

24 篇文章 0 订阅
6 篇文章 0 订阅

  二 sed 编辑器基础

1.更多的替换选项

用s命令(substitute)来在行中替换文本

  1. 1替换标记

      替换命令在替换多行中的文本时能正常工作,但默认情况下它只替换每行中出现的第一处

       例如

       $ cat data4.txt
        This is a test of the test script.
        This is the second test of the test script.
      $
      $ sed 's/test/trial/' data4.txt
       This is a trial of the test script.
       This is the second trial of the test script.

     要让替换命令能够替换一行中不同地方出现的文本必须使用替换标记(substitution flag)。替换标记会在替换命令字符串之后设置。
     s/pattern/replacement/flags

有4种可用的替换标记:
 数字,表明新文本将替换第几处模式匹配的地方;
 g,表明新文本将会替换所有匹配的文本;
 p,表明原先行的内容要打印出来;
 w file,将替换的结果写到文件中。

例如

$ sed 's/test/trial/2' data4.txt
This is a test of the trial script.
This is the second test of the trial script.
将替换标记指定为2的结果就是:sed编辑器只替换每行中第二次出现的匹配模式

$ sed 's/test/trial/g' data4.txt
This is a trial of the trial script.
This is the second trial of the trial script.

g替换标记使你能替换文本中匹配模式所匹配的每处地方

p替换标记会打印与替换命令中指定的模式匹配的行。这通常会和sed的-n选项一起使用。
$ cat data5.txt
This is a test line.
This is a different line.
$
$ sed -n 's/test/trial/p' data5.txt
This is a trial line.

w替换标记会产生同样的输出,不过会将输出保存到指定文件中。
$ sed 's/test/trial/w test.txt' data5.txt
This is a trial line.
This is a different line.
$
$ cat test.txt
This is a trial line.

  1.  2替换字符
  2. 使用地址

 在sed编辑器中有两种形式的行寻址:

 以数字形式表示行区间
 用文本模式来过滤出行

两种形式都使用相同的格式来指定地址:
[address]command
也可以将特定地址的多个命令分组:
address {
command1
command2
command3
}

$ sed '2s/dog/cat/' data1.txt
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
$
sed编辑器只修改地址指定的第二行的文本。这里有另一个例子,这次使用了行地址区间。
$ sed '2,3s/dog/cat/' data1.txt
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy dog
$
如果想将命令作用到文本中从某行开始的所有行,可以用特殊地址——美元符。
$ sed '2,$s/dog/cat/' data1.txt
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat

2.2 使用文本模式过滤器

另一种限制命令作用到哪些行上的方法会稍稍复杂一些。sed编辑器允许指定文本模式来过
滤出命令要作用的行。格式如下:
/pattern/command

2.3 命令组合
如果需要在单行上执行多条命令,可以用花括号将多条命令组合在一起。sed编辑器会处理
地址行处列出的每条命令。
$ sed '2{s/fox/elephant/;s/dog/cat/}' data1.txt
The quick brown fox jumps over the lazy dog.
The quick brown elephant jumps over the lazy cat.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.

3.删除行

删除命令d

它会删除匹配指定寻址模式的所有行。使用该命令时要特别小心,如果你忘记加入寻址模式的话,流中的所有文本行都会被删除。

$ cat data1.txt
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
$
$ sed 'd' data1.txt

可以从数据流中删除特定的文本行,通过行号指定:

$ sed '3d' data6.txt
This is line number 1.
This is line number 2.
This is line number 4.
$
或者通过特定行区间指定:
$ sed '2,3d' data6.txt
This is line number 1.
This is line number 4.
$
或者通过特殊的文件结尾字符:
$ sed '3,$d' data6.txt
This is line number 1.
This is line number 2.

sed编辑器的模式匹配特性也适用于删除命令。
$ sed '/number 1/d' data6.txt
This is line number 2.
This is line number 3.
This is line number 4.
$
sed编辑器会删掉包含匹配指定模式的行。

$ cat data7.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is line number 1 again.
This is text you want to keep.
This is the last line in the file.

$ sed '/1/,/5/d' data7.txt
$

删除功能在匹配到第一个模式的时候打开了,但一直没匹配到结束模式,所以整个数据
流都被删掉了。

4.插入和附加文本

sed编辑器允许向数据流插入和附加文本行:

 插入(insert)命令(i)会在指定行前增加一个新行;
 附加(append)命令(a)会在指定行后增加一个新行。

格式如下:sed '[address]command\new line' ---new line中的文本将会出现在sed编辑器输出中你指定的位置

当使用插入命令时,文本会出现在数据流文本的前面。
$ echo "Test Line 2" | sed 'i\Test Line 1'
Test Line 1
Test Line 2
$

当使用附加命令时,文本会出现在数据流文本的后面。
$ echo "Test Line 2" | sed 'a\Test Line 1'
Test Line 2
Test Line 1

给文本插入数据,必须要在命名上加地址例如sed '[address]a/i \new line' 文本

将一个新行插入到数据流第三行前。
$ sed '3i\This is an inserted line.' data6.txt
This is line number 1.
This is line number 2.
This is an inserted line.
This is line number 3.
This is line number 4.
$
下面的例子是将一个新行附加到数据流中第三行后。
$ sed '3a\This is an appended line.' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is an appended line.
This is line number 4.
$

要插入或附加多行文本,就必须对要插入或附加的新文本中的每一行使用反斜线,直到最后一行。

$ sed '1i\This is one line of new text.\nThis is another line of new text.' data6.txt
This is one line of new text.
This is another line of new text.
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.

5.修改行

修改(change)命令允许修改数据流中整行文本的内容。

$sed '3c\ This is a changed line of text.' data6.txt
This is line number 1.
This is line number 2.
This is a changed line of text.
This is line number 4.

sed编辑器会修改第三行中的文本。也可以用文本模式来寻址。
$ sed '/number 3/c\ This is a changed line of text.' data6.txt
This is line number 1.
This is line number 2.
This is a changed line of text.
This is line number 4.

c命令不能进行多行修改,只能进行单行匹配

6.转换命令

转换(transform)命令(y)是唯一可以处理单个字符的sed编辑器命令。转换命令格式如下。
[address]y/inchars/outchars/
转换命令会对inchars和outchars值进行一对一的映射

$ cat data8.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is line number 1 again.
This is yet another line.
This is the last line in the file.

$ sed 'y/123/789/' data8.txt
This is line number 7.
This is line number 8.
This is line number 9.
This is line number 4.
This is line number 7 again.
This is yet another line.
This is the last line in the file.

7.回顾打印

来打印数据流中的信息:
 p命令用来打印文本行;
 等号(=)命令用来打印行号;
 l(小写的L)命令用来列出行

打印行

$ cat data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
$
$ sed -n '/number 3/p' data6.txt
This is line number 3.

$ sed -n '2,3p' data6.txt
This is line number 2.
This is line number 3.

打印行号

$ cat data1.txt
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
$
$ sed '=' data1.txt
1
The quick brown fox jumps over the lazy dog.
2
The quick brown fox jumps over the lazy dog.
3
The quick brown fox jumps over the lazy dog.
4
The quick brown fox jumps over the lazy dog.

8.使用sed 处理文件

8.1. 写入文件
w命令用来向文件写入行。该命令的格式如下:
[address]w filename

$ sed '1,2w test.txt' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
$
$ cat test.txt
This is line number 1.
This is line number 2.
$

$ cat data11.txt
Blum, R Browncoat
McGuiness, A Alliance
Bresnahan, C Browncoat
Harken, C Alliance
$
$ sed -n '/Browncoat/w Browncoats.txt' data11.txt
$
$ cat Browncoats.txt
Blum, R Browncoat
Bresnahan, C Browncoat
$

8.2. 从文件读取数据

读取(read)命令(r)允许你将一个独立文件中的数据插入到数据流中。读取命令的格式如下:
[address]r filename
filename参数指定了数据文件的绝对路径或相对路径,在读取命令中使用地址区间,只能指定单独一个行号或文本模式地址。sed编辑器会将文件中的文本插入到指定地址后。

$ cat data12.txt
This is an added line.
This is the second added line.

$ sed '3r data12.txt' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is an added line.
This is the second added line.
This is line number 4.

在使用文本模式地址时也适用。
$ sed '/number 2/r data12.txt' data6.txt
This is line number 1.
This is line number 2.
This is an added line.
This is the second added line.
This is line number 3.
This is line number 4.
$

在数据流的末尾添加文本,只需用美元符地址符就行了。
$ sed '$r data12.txt' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is an added line.
This is the second added line.
$

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值