linux工具-sed

1. 基本语法

定义:sed:Stream Editor文本流编辑.

语法格式:

sed [option] '[address-range| pattern-to-match] sed-command' filename
sed [option] -f sed_script_file filename

2. option

sed的主要option如下:

选项 说明
-n 只打印模式匹配的行,默认情况下sed会把文件中所有行都打印出来.
-e 直接在命令行模式上进行sed的动作编辑,即sed动作写在命令行,不使用脚本,与-f选项相对应. 此为默认选项.
-f sed动作写在一个脚本文件内,用-f file.sed来执行sed动作
-r 支持扩展的正则表达式.
-i 直接修改文件内容,默认情况下sed只修改输出到Terminal的内容.

3. sed动作

格式:'[address-range| pattern-to-match] sed-command'

sed动作分两部分 作用
address-range或pattern-to-match 指定文件中的一系列文本行,可以是特定范围内的行,也可以是匹配某模式的行.
sed-command 对上述的文本行进行处理.

注意:

  1. sed动作要写在一对单引号中,防止被shell解释为其它命令.
  2. sed动作的两部分之间可以有空格,也可以没空格.
  3. address-range| pattern-to-match是可选的,如果不指定,默认是文件中的所有行.

3.1. address-range| pattern-to-match

作用:指定文件中的一系列文本行,默认为文本中的所有文本行.

格式:address-range可以通过行号指定,也可以通过/pattern/指定

addr-range or pattern 说明
m 处理第m行,m为行号
/pattern/ 处理匹配/pattern/的行
m, n 处理m到n范围内的所有行,m/n为行号,逗号两边可以加空格
/pattern1/, /pattern2/ 处理匹配/pattern1/和/pattern2/范围内的所有行
m, /pattern/ 处理第m行~匹配/pattern/范围内的所有行.
/pattern/, n 处理匹配/pattern/~第n行范围内的所有行.
m, n! ??

例子:

文件内容:

$ cat a.txt # 文件内容  
line first  
line second  
line third  
line forth  
line fifth  
line sixth  
line seventh  

通过行号指定行

$ sed -n '3 p' a.txt     # 打印第3行到Terminal  
line third  
$ sed -n '3,5 p' a.txt   # 打印第3~5行到Terminal  
line third  
line forth  
line fifth  

通过pattern指定行

$ sed -n '/ne th/,/ne fi/ p' a.txt # 打印匹配 /ne th/和/ne fi/ 之间的行到Terminal  
line third  
line forth  
line fifth  
$  
$ sed -n '3,/ne fi/ p' a.txt   
line third  
line forth  
line fifth  
$ sed -n '/ne th/,5 p' a.txt  
line third  
line forth  
line fifth  

反选,指定3~5行之外的行.

$ sed -n '3,5! p' a.txt  # 打印第3~5行之外的行到Terminal  
line first  
line second  
line sixth  
line seventh  

如果第二个pattern没匹配到,则打印到文件结尾

$ sed -n '/ne th/,/xxxx/ p' a.txt # 如果第二个pattern没匹配到,则打印到文件结尾  
line third  
line forth  
line fifth  
line sixth  
line seventh  

如果第一个pattern没匹配到,则匹配结果为空

$ sed -n '/xxxx/,/ne fi/ p' a.txt # 如果第一个pattern没匹配到,则匹配结果为空  
$   

3.2. sed-command

作用:对文本行进行处理.

常用sed命令如下:

命令 说明
p 打印匹配行(和-n选项配合使用)
= 显示文件行号
a ext 新增,在定位到的每一行后面附加新行
i ext 新增,在定位到的每一行前面附加新行
d 删除,定位到的行删除
c ext 替换,用新文本替换旧文本,
w filename 将文本写到filename文件中,类似于输出重定向
r filename 从文件filename中读文本,类似于输入重定向
s/pattern/txt/ 替换文本
q 第一个模式匹配后即出 或 立即退出
{命令;命令…} 执行多个命令,使用分号分隔开.
...

例子:

文件内容:

$ cat a.txt # 文件内容  
line first  
line second  
line third  
line forth  
line fifth  
line sixth  
line seventh  

使用p命令,打印文本

$ sed -n '/ne th/,/ne fi/ p' a.txt  
line third  
line forth  
line fifth  

使用=命令,打印行号

$ sed -n '/ne th/,/ne fi/ =' a.txt  
3  
4  
5  

使用a命令,在每个匹配行后面追加文本

$ sed  '/ne th/,/ne fi/ a
ew' a.txt  
line first  
line second  
line third  
new  
line forth  
new  
line fifth  
new  
line sixth  
line seventh  

使用i命令,在每个匹配行前面追加文本

$ sed  '/ne th/,/ne fi/ i
ew' a.txt  
line first  
line second  
new  
line third  
new  
line forth  
new  
line fifth  
line sixth  
line seventh  

使用c命令,将匹配到的address-range行替换为新文本,注意不是每行替换一份,而是整个range替换为一份.

$ sed  '/ne th/,/ne fi/ c
ew' a.txt  
line first  
line second  
new  
line sixth  
line seventh  

使用d命令,删除匹配的行

$ sed  '/ne th/,/ne fi/ d' a.txt  
line first  
line second  
line sixth  
line seventh  

使用s命令,替换匹配行内容的文本

$ sed  '/ne th/,/ne fi/ s/line/Number/' a.txt  
line first  
line second  
Number third  
Number forth  
Number fifth  
line sixth  
line seventh  

4. /pattern/支持的正则

待完成

5.

$ VAR="var0"
$ text="1234,abcd"
$ echo $text | sed 's/abcd/$VAR/' # 在单引号中变量替换不成功
1234,$VAR
$ echo $text | sed "s/abcd/$VAR/" # 在双引号中变量替换成功
1234,var0
$

待完成

原文地址:https://www.cnblogs.com/gaiqingfeng/p/13644746.html