sed命令基础

本文是《Linux命令行与shell脚本编程大全》的学习笔记

1.替换选项

        默认情况下,替换选项只替换每行中出现的第一次匹配,如果想要替换一行中不同地方出现的文本,必须使用替换标记。

s/pattern/replacement/flags

flag可选的包括:

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

数字选项:只替换每一行中第2次出现的test

$ cat data4.txt
This is a test of the test script.
This is the second test of the test script.

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

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.
$

2.行寻址

        默认情况下,sed会将命令使用于文本中的所有行,如果只想对文本的某些行进行操作,可以使用行寻址,有两种寻址方式:

  • 使用数字表示行区间

  • 使用文本过滤行
    使用时,将地址放置于命令前面。格式如下:
    [address] command
    或address { command1
    command2
    command3
    }

    数字寻址举例:

$ 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 '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,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
$

文本匹配方式寻址

/pattern/command
pattern表示匹配的文本,必须使用/将pattern封起来

$ grep Samantha /etc/passwd
Samantha:x:502:502::/home/Samantha:/bin/bash
$ sed '/Samantha/s/bash/csh/' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[...]
Christine:x:501:501:Christine B:/home/Christine:/bin/bash
Samantha:x:502:502::/home/Samantha:/bin/csh
Timothy:x:503:503::/home/Timothy:/bin/bash
$

命令组合寻址
使用大括号将多条命令组合在一期{}。

$ 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命令可以结合寻址删除指定行。如果使用数字寻址,删除格式如下:
sed ‘[address]d’ file
例如

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

文本模式下寻址
sed ‘/pattern/d’ file

$ sed '/number 1/d' data6.txt
This is line number 2.
This is line number 3.
This is line number 4.
$

文本寻址模式下,也可以使用区间删除模式,规则如下:

  • 匹配的第一个模式表达开始删除,一直删除到匹配的第二个文本,如果没有匹配到,则一直删除到文件末尾。
  • 匹配到第二个文本后,如果后续再出现第一个匹配文本,重新开始删除行为,直到匹配到第二个文本。
$ sed '/1/,/3/d' data6.txt   # 从包含1的行开始山删除,一直到包含3的行
This is line number 4.
$
$ 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/,/3/d' data7.txt
#从包含1 的行开始删除,一直到包含3的行,又匹配到包含1的行,又开始删除。
This is line number 4.
$

4插入和附加文本

插入(insert)命令(i)会在指定行前增加一个新行;
附加(append)命令(a)会在指定行后增加一个新行。
命令格式如下:
sed '[address]command\content

$ 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
$

5修改行

sed ‘[address]command\pattern’ input

$ 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 '/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.
$
$ sed '2,3c\
> This is a new line of text.' data6.txt #sed编辑器会用这一行文本来替换数据流中的两行文本,而不是逐一修改这两行文本。
This is line number 1.
This is a new line of text.
This is line number 4.
$

6转换命令

用于处理单个字符的命令。命令如下:
[address]y/inchars/outchars/
转换命令会对inchars和outchars值进行一对一的映射。inchars中的第一个字符会被转
换为outchars中的第一个字符,第二个字符会被转换成outchars中的第二个字符。这个映射过程会一直持续到处理完指定字符。

$ 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)命令用来列出行。可以打印数据流中的文本和不可打印的ASCII字符
  • -n 只打印匹配的行

命令 sed [address] p input
-n举例

$ sed -n '/3/{ 
> p
> s/line/test/p
> }' data6.txt
This is line number 3.
This is test number 3.
#sed编辑器命令会查找包含数字3的行,然后执行两条命令。首先,脚本用p命令来打印出原
#始行;然后它用s命令替换文本,并用p标记打印出替换结果。输出同时显示了原来的行文本和新
#的行文本。
$

等号举例

$ 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.
$
$ sed -n '/number 4/{
> =
> p
> }' data6.txt
4
This is line number 4.
$

l举例

$ cat data9.txt
This    line    contains    tabs.
$
$ sed -n 'l' data9.txt
This\tline\tcontains\ttabs.$
$

8.处理文件

8.1 写入文件

sed ‘[address]w output’ input
address可以是数字寻址或匹配模式寻址
filename是输出文件,要写入的问题,input是输入的文件,从input中匹配到文件后写入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.
$

8.2读取数据

命令sed ‘[address]r input’ output
将input 写入到output的address后

$ 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将data12.txt插入到data6.txt之后
$
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值