shell-sed命令

shell -sed命令

1.在命令行定义编辑器命令(直接将数据通过管道输入sed编辑器处理

echo “this is a test” | sed 's/test/big test/'
输出:this ia a big test

2.在命令行使用多个编辑器命令

sed -e
sed -e ‘ ’ ;‘ ’ script file

3.从文件中读取编辑器指令

sed -f  file 。将file中指定的命令天添加到已有命令中

4.替换标记

替换命令默认是替换每行中出现的第一次,让替换命令替换一行中不同地方出现的文本必须用替换标记。替换标记在替换命令之后设置。格式为:
s/pattern/replacement/flags
有4种可用的替换标记:
数字:表明新文本将替换第几处模式匹配的地方
$ 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. 
$

g:新文本将替换所有匹配的文本
	$ cat data4.txt 
	This is a test of the test script. 
	This is the second test of the test script. 
	$
	$ sed 's/test/trial/g' data4.txt 
	This is a trial of the trial script. 
	This is the second trial of the trial script. 
	$
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. 
$
w file : 将替换结果写进文件
$ 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. 
$

5.行寻址

sed编辑器中使用的命令会作用于文本数据的所有行,如果只想作用于特定行或某些行,则必须使用行寻址。有两种方式的行寻址:
以数字形式表示行区间
用文本模式来过滤行

数字方式的行寻址

当使用数字方式的行寻址时,可以用行在文本流中的行位置来引用。sed编辑器会将文本流中的第一行编号为1,然后继续按顺序为接下来的行分配行号。
在命令中指定的地址可以是单个行号,或是用起始行号、逗号以及结尾行号指定的一定区间范围内的行。这里有个sed命令作用到指定行号的例子
$ 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 ‘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
$

文本模式的行寻址

$ grep Samantha /etc/passwd
Samantha❌502:502::/home/Samantha:/bin/bash
$
$ sed ‘/Samantha/s/bash/csh/’ /etc/passwd
Samantha❌502:502::/home/Samantha:/bin/csh
$

删除行

1.删除全部行
$ 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
$
2.删除指定行
$ 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 ‘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编辑器不会修改原始文件。你删除的行只是从sed编辑器的输出中消失了。原始文件仍然包含那些“删掉的”行。

插入和附加文本

插入(insert)命令(i)会在指定行前增加一个新行;
附加(append)命令(a)会在指定行后增加一个新行
格式如下:sed ‘address command\new line’
$ 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
$

修改行

修改(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.
$

转换命令

转换(transform)命令(y)是唯一可以处理单个字符的sed编辑器命令。转换命令格式如下:
[address]y/inchars/outchars/
转换命令会对inchars和outchars值进行一对一的映射。inchars中的第一个字符会被转换为outchars中的第一个字符,第二个字符会被转换成outchars中的第二个字符。这个映射过程会一直持续到处理完指定字符。如果inchars和outchars的长度不同,则sed编辑器会产生一
条错误消息。
$ 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.
$

回顾打印

 p命令用来打印文本行;
 等号(=)命令用来打印行号;
 l(小写的L)命令用来列出行。

sed 处理文件

1.写入文件
w命令用来向文件写入行。该命令的格式如下:
[address]w filename
filename可以使用相对路径或绝对路径,但不管是哪种,运行sed编辑器的用户都必须有文件的写权限。地址可以是sed中支持的任意类型的寻址方式,例如单个行号、文本模式、行区间或文本模式
$ 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.
$
2.从文件中读取数据
读取(read)命令(r)允许你将一个独立文件中的数据插入到数据流中。读取命令的格式如下:
[address]r filename
$ 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编辑器会将数据文件中的所有文本行都插入到数据流中。同样的方法在使用文本模式地
址时也适用。
$ 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、付费专栏及课程。

余额充值