欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

sed编辑器基础

程序员文章站 2022-06-21 15:33:25
...

1. 替换标记

sed的-s选项用来替换文件中的内容,但是仅仅作用于每行的第一处,若需要替换其他地方则需要替换标记

s/pattern/replacement/flags
4种可用替换标记
1. 数字       表示文本每行记录的第几处进行替换
2. g         表示替换所有
3. p         表示原先行的内容要打印出来
4. w file    将替换结果写到文件中

-n 与 -p 使用效果是只输出被修改的行
-n 禁止sed编辑器输出,-p会输出修改过的行

$ 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编辑器中使用的命令会作用于文本数据的所有行。如果只想将命令作用
于特定行或某些行,则必须用行寻址(line addressing)

行寻址方式:
1、数字形式表示区间
2、文本模式进行过滤出行

只修改第2行
$ 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
$
修改2-3行
$ 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
$
修改第2行至最后一行,$表示最后一行
$ 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
$

$ sed '/Samantha/s/bash/csh/' /etc/passwd
表示匹配包含Samantha的行,并将bash值修改为csh值

3. 删除行

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

$ sed 'd' data1.txt
删除文件所有行

$ sed '3d' data6.txt
删除第3行

$ sed '2,3d' data6.txt
删除第2-3行

$ sed '3,$d' data6.txt
删除第3行到最后一行

4. 插入和附加文本

插入(insert)命令(i)会在指定行前增加一个新行;

附加(append)命令(a)会在指定行后增加一个新行。

# sed '[address]command\new line'

$ echo "Test Line 2" | sed 'i\Test Line 1'
Test Line 1
Test Line 2

Test Line 2该行前插入Test Line 1

$ echo "Test Line 2" | sed 'a\Test Line 1'
Test Line 2
Test Line 1
$

Test Line 2该行后附加Test Line 1

sed '3i\
> This is an inserted line.' data6.txt
表示第3行前插入行  This is an inserted line.

$ sed '3a\
> This is an appended line.' data6.txt
表示第3行后附加行  This is an appended line.

$ sed '1i\
> This is one line of new text.\
> This 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.
$

表示第1行前插入行 This is one line of new text. 与行 This is another line of new text.

5. 修改行

修改(change)命令允许修改数据流中整行文本的内容。它跟插入和附加命令的工作机制
一样,你必须在sed命令中单独指定新行

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

6. 转换命令

转换(transform)命令(y)是唯一可以处理单个字符的sed编辑器命令。转换命令格式如下:

[address]y/inchars/outchars/

$ echo "This 1 is a test of 1 try." | sed 'y/123/456/'
This 4 is a test of 4 try.
$

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


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

 

相关标签: shell