http://witmax.cn/linux-sed-usage.html

http://www.linuxsir.org/bbs/showthread.php?t=189620

Sed学习笔记

1. sed简介

sed 是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。以下介绍的是Gnu版本的Sed 3.02。

2. 定址

可以通过定址来定位你所希望编辑的行,该地址用数字构成,用逗号分隔的两个行数表示以这两行为起止的行的范围(包括行数表示的那两行)。如1,3表示1,2,3行,美元符号($)表示最后一行。范围可以通过数据,正则表达式或者二者结合的方式确定 。

3. sed命令

调用sed命令有两种形式:
sed [options] ‘command’ file(s)
sed [options] -f scriptfile file(s)
a\
在当前行后面加入一行文本。
b lable
分支到脚本中带有标记的地方,如果分支不存在则分支到脚本的末尾。
c\
用新的文本改变本行的文本。
d
从模板块(Pattern space)位置删除行。
D
删除模板块的第一行。
i\
在当前行上面插入文本。
h
拷贝模板块的内容到内存中的缓冲区。
H
追加模板块的内容到内存中的缓冲区
g
获得内存缓冲区的内容,并替代当前模板块中的文本。
G
获得内存缓冲区的内容,并追加到当前模板块文本的后面。
l
列表不能打印字符的清单。
n
读取下一个输入行,用下一个命令处理新的行而不是用第一个命令。
N
追加下一个输入行到模板块后面并在二者间嵌入一个新行,改变当前行号码。
p
打印模板块的行。
P(大写)
打印模板块的第一行。
q
退出Sed。
r file
从file中读行。
t label
if分支,从最后一行开始,条件一旦满足或者T,t命令,将导致分支到带有标号的命令处,或者到脚本的末尾。
T label
错误分支,从最后一行开始,一旦发生错误或者T,t命令,将导致分支到带有标号的命令处,或者到脚本的末尾。
w file
写并追加模板块到file末尾。
W file
写并追加模板块的第一行到file末尾。
!
表示后面的命令对所有没有被选定的行发生作用。
s/re/string/
用string替换正则表达式re。
=
打印当前行号码。
#
把注释扩展到下一个换行符以前。
以下的是替换标记

  • g表示行内全面替换。
  • p表示打印行。
  • w表示把行写入一个文件。
  • x表示互换模板块中的文本和缓冲区中的文本。
  • y表示把一个字符翻译为另外的字符(但是不用于正则表达式)

4. 选项

-e command, –expression=command
允许多台编辑。
-h, –help
打印帮助,并显示bug列表的地址。
-n, –quiet, –silent

取消默认输出。
-f, –filer=script-file
引导sed脚本文件名。
-V, –version
打印版本和版权信息。

5. 元字符集^

锚定行的开始 如:/^sed/匹配所有以sed开头的行。
$
锚定行的结束 如:/sed$/匹配所有以sed结尾的行。
.
匹配一个非换行符的字符 如:/s.d/匹配s后接一个任意字符,然后是d。
*
匹配零或多个字符 如:/*sed/匹配所有模板是一个或多个空格后紧跟sed的行。
[]
匹配一个指定范围内的字符,如/[Ss]ed/匹配sed和Sed。
[^]
匹配一个不在指定范围内的字符,如:/[^A-RT-Z]ed/匹配不包含A-R和T-Z的一个字母开头,紧跟ed的行。
\(..\)
保存匹配的字符,如s/\(love\)able/\1rs,loveable被替换成lovers。
&
保存搜索字符用来替换其他字符,如s/love/**&**/,love这成**love**。
\<
锚定单词的开始,如:/\<love/匹配包含以love开头的单词的行。
\>
锚定单词的结束,如/love\>/匹配包含以love结尾的单词的行。
x\{m\}
重复字符x,m次,如:/0\{5\}/匹配包含5个o的行。
x\{m,\}
重复字符x,至少m次,如:/o\{5,\}/匹配至少有5个o的行。
x\{m,n\}
重复字符x,至少m次,不多于n次,如:/o\{5,10\}/匹配5–10个o的行。

6. 实例

删除:d命令
*
$ sed ’2d’ example—–删除example文件的第二行。
*
$ sed ’2,$d’ example—–删除example文件的第二行到末尾所有行。
*
$ sed ‘$d’ example—–删除example文件的最后一行。
*
$ sed ‘/test/’d example—–删除example文件所有包含test的行。

替换:s命令
*
$ sed ‘s/test/mytest/g’ example—–在整行范围内把test替换为mytest。如果没有g标记,则只有每行第一个匹配的test被替换成mytest。
*
$ sed -n ‘s/^test/mytest/p’ example—–(-n)选项和p标志一起使用表示只打印那些发生替换的行。也就是说,如果某一行开头的test被替换成mytest,就打印它。
*
$ sed ‘s/^192.168.0.1/&localhost/’ example—–&符号表示替换换字符串中被找到的部份。所有以192.168.0.1开头的行都会被替换成它自已加 localhost,变成192.168.0.1localhost。
*
$ sed -n ‘s/\(love\)able/\1rs/p’ example—–love被标记为1,所有loveable会被替换成lovers,而且替换的行会被打印出来。
*
$ sed ‘s#10#100#g’ example—–不论什么字符,紧跟着s命令的都被认为是新的分隔符,所以,“#”在这里是分隔符,代替了默认的“/”分隔符。表示把所有10替换成100。

选定行的范围:逗号
*
$ sed -n ‘/test/,/check/p’ example—–所有在模板test和check所确定的范围内的行都被打印。
*
$ sed -n ’5,/^test/p’ example—–打印从第五行开始到第一个包含以test开始的行之间的所有行。
*
$ sed ‘/test/,/check/s/$/sed test/’ example—–对于模板test和west之间的行,每行的末尾用字符串sed test替换。

多点编辑:e命令
*
$ sed -e ’1,5d’ -e ‘s/test/check/’ example—–(-e)选项允许在同一行里执行多条命令。如例子所示,第一条命令删除1至5行,第二条命令用check替换test。命令的执 行顺序对结果有影响。如果两个命令都是替换命令,那么第一个替换命令将影响第二个替换命令的结果。
*
$ sed –expression=’s/test/check/’ –expression=’/love/d’ example—–一个比-e更好的命令是–expression。它能给sed表达式赋值。

从文件读入:r命令
*
$ sed ‘/test/r file’ example—–file里的内容被读进来,显示在与test匹配的行后面,如果匹配多行,则file的内容将显示在所有匹配行的下面。

写入文件:w命令
*
$ sed -n ‘/test/w file’ example—–在example中所有包含test的行都被写入file里。

追加命令:a命令
*
$ sed ‘/^test/a\\—>this is a example’ example<—–’this is a example’被追加到以test开头的行后面,sed要求命令a后面有一个反斜杠。

插入:i命令
$ sed ‘/test/i\\
new line
————————-’ example
如果test被匹配,则把反斜杠后面的文本插入到匹配行的前面。

下一个:n命令
*
$ sed ‘/test/{ n; s/aa/bb/; }’ example—–如果test被匹配,则移动到匹配行的下一行,替换这一行的aa,变为bb,并打印该行,然后继续。

变形:y命令
*
$ sed ’1,10y/abcde/ABCDE/’ example—–把1–10行内所有abcde转变为大写,注意,正则表达式元字符不能使用这个命令。

退出:q命令
*
$ sed ’10q’ example—–打印完第10行后,退出sed。
保持和获取:h命令和G命令
*
$ sed -e ‘/test/h’ -e ‘$G example—–在sed处理文件的时候,每一行都被保存在一个叫模式空间的临时缓冲区中,除非行被删除或者输出被取消,否则所有被处理的行都将 打印在屏幕上。接着模式空间被清空,并存入新的一行等待处理。在这个例子里,匹配test的行被找到后,将存入模式空间,h命令将其复制并存入一个称为保 持缓存区的特殊缓冲区内。第二条语句的意思是,当到达最后一行后,G命令取出保持缓冲区的行,然后把它放回模式空间中,且追加到现在已经存在于模式空间中 的行的末尾。在这个例子中就是追加到最后一行。简单来说,任何包含test的行都被复制并追加到该文件的末尾。
保持和互换:h命令和x命令
*
$ sed -e ‘/test/h’ -e ‘/check/x’ example —–互换模式空间和保持缓冲区的内容。也就是把包含test与check的行互换。

7. 脚本

Sed脚本是一个sed的命令清单,启动Sed时以-f选项引导脚本文件名。Sed对于脚本中输入的命令非常挑剔,在命令的末尾不能有任何空白或文本,如果在一行中有多个命令,要用分号分隔。以#开头的行为注释行,且不能跨行。

8. 小技巧

在sed的命令行中引用shell变量时要使用双引号,而不是通常所用的单引号。下面是一个根据name变量的内容来删除named.conf文件中zone段的脚本:name=’zone\ “localhost”‘
sed “/$name/,/};/d” named.conf

作者:Jims of 肥肥世家 <jims.yang@gmail.com >

Copyright © 2004,2005, 本文遵从GNU 的自由文档许可证(Free Document License)的条款,欢迎转载、修改、散布。

***********分***********隔***********线***********

sed新手使用进阶全功略

看了各个linux 论坛的帖子,感觉sed的介绍不少,但有点零乱,在这里整理一下,希望能对学习者有所帮助!

注:sed使用中除了多个命令时, -e选项一般可以省略!

基础:

正则表达式(Regular Expression)

在学习sed前,首先了解RE的基本知识,大体上最基本也需要知道下面这些,如果不了解正则表达式,那么您将很难进阶

引用:

- 錨點(anchor)
用以標識 RE 於句子中的位置所在. 常見有:
^: 表示句首. 如 ^abc 表示以 abc 開首的句子.
$: 表示句尾. 如 abc$ 表示以 abc 結尾的句子.
\<: 表示詞首. 如 \<abc 表示以 abc 開首的詞.
\>: 表示詞尾. 如 abc\> 表示以 abc 結尾的詞.- 修飾字符(modifier)
獨立表示時本身不具意義, 專門用以修改前一個 char. set 的出現次數. 常見有:
*: 表示前一個 char. set 的出現次數為 0 或多次. 如 ab*c 表示 a 與 c 之間可有 0 或多個 b 存在.
?: 表示前一個 char. set 的出現次數為 0 或 1 次. 如 ab?c 表示 a 與 c 之間可有 0 或 1 個 b 存在.
+: 表示前一個 char. set 的出現次數為 1 或多次. 如 ab+c 表示 a 與 c 之間可有 1 或多個 b 存在.
{n}: 表示前一個 char. set 的出現次數必須為 n 次. 如 ab{3,}c 表示 a 與 c 之間必須有 3 個 b 存在.{n,}: 表示前一個 char. set 的出現次數至少為 n 次. 如 ab{3,}c 表示 a 與 c 之間至少有 3 個 b 存在.
{n,m}: 表示前一個 char. set 的出現次數為 n 到 m 次. 如 ab{3,5}c 表示 a 與 c 之間有 3 到 5 個 b 存在.

. : 匹配任意一个字符(1个)
.*:匹配任意多个字符(1或多个)

sed全功略

1.sed用法介绍

sed是一个非交互性文本流编辑器。它编辑文件或标准输入导出的文本拷贝。
• 抽取域。
• 匹配正则表达式。
• 比较域。
• 增加、附加、替换。
• 基本的sed命令和一行脚本。
无论命令是什么, sed并不与初始化文件打交道,它操作的只是一个拷贝,然后所有的改动如果没有重定向到一个文件,将输出到屏幕。

基本格式:

代码:

1
2
sed [-n] [-e] 'command' file(s)
sed [-n] -f scriptfile file(s)

(1)sed怎样读取数据

s e d从文件的一个文本行或从标准输入的几种格式中读取数据,将之拷贝到一个编辑缓冲区,然后读命令行或脚本的第一条命令,并使用这些命令查找模式或定位行号编辑它。重复此过程直到命令结束。

(2)调用sed

调用s e d有三种方式:
a.在命令行键入命令; sed [选项] s e d命令输入文件
b.将s e d命令插入脚本文件,然后调用s e d; sed [选项] -f sed脚本文件输入文件
c.将s e d命令插入脚本文件,并使s e d脚本可执行。 sed脚本文件 [选项] 输入文件

2.sed选项

s e d选项如下:
-n 不打印;s e d不写编辑行到标准输出,缺省为打印所有行(编辑和未编辑)。p命令可以用来打印编辑行。
-f 如果正在调用s e d脚本文件,使用此选项。此选项通知s e d一个脚本文件支持所有的s e d命令,例如:sed -f myscript.sed input_file,这里m y s c r i p t . s e d即为支持s e d命令的文件。
-c 下一命令是编辑命令。使用多项编辑时加入此选项。如果只用到一条s e d命令,此选项无用,但指定它也没有关系。
-i 编辑原文件(此选项慎用,如果使用则原文件就会被修改,无法恢复)。

3.保存sed输出

重定向(下面将sed命令的所有输出至文件 output-file 中)

代码:

1
zhyfly@zhyfly:~$ sed 'some-sed-commands' input-file&gt;output-file

w 写文本到一个文件

代码:

1
[address [,address]]w filename

代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
zhyfly@zhyfly:~/bash$ cat test.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e '1,2w test.bak' test.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ cat test.bak
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.

r 从另一个文件中读文本

代码:

1
address r filename

4.sed基础用法

使用sed在文件中查询文本的方式:
sed浏览输入文件时,缺省从第一行开始,有两种方式定位文本:
a. 使用行号,可以是一个简单数字,或是一个行号范围。
b. 使用正则表达式。
例:
x #为一行号,如1
x,y #表示行号范围从x到y,如2,5表示从第2行到第5行
/pattern/ #查询包含模式的行。例如/disk/或/[a-z]/
/pattern/pattern/ #查询包含两个模式的行。例如/disk/disks/
/pattern/,x #在给定行号上查询包含模式的行。如/ribbon/,3
x,/pattern/ #通过行号和模式查询匹配行。3,/vdu/
x,y! #查询不包含指定行号x和y的行。1,2!

基本sed编辑命令:
sed编辑命令
p 打印匹配行
= 显示文件行号
a\ 在定位行号后附加新文本信息
i\ 在定位行号后插入新文本信息
d 删除定位行
c\ 用新文本替换定位文本
s 使用替换模式替换相应模式
r 从另一个文件中读文本
w 写文本到一个文件
q 第一个模式匹配完成后推出或立即推出
l 显示与八进制A S C I I代码等价的控制字符
{ } 在定位行执行的命令组
n 从另一个文件中读文本下一行,并附加在下一行
g 将模式2粘贴到/pattern n/
y 传送字符
n 延续到下一输入行;允许跨行的模式匹配语句

举例:

代码:

1
2
3
4
5
zhyfly@zhyfly:~/bash$ cat test.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.

代码:

1
2
3
4
5
6
7
8
zhyfly@zhyfly:~/bash$ sed -e '1p' test.txt
The honeysuckle band played all night long for only $90.
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -n -e '1p' test.txt
The honeysuckle band played all night long for only $90.

代码:

1
2
zhyfly@zhyfly:~/bash$ sed -n -e '2p' test.txt
It was an evening of splendid music and company.

代码:

1
2
3
zhyfly@zhyfly:~/bash$ sed -n -e '2,3p' test.txt
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.

代码:

1
2
zhyfly@zhyfly:~/bash$ sed -n -e '/company/p' test.txt
It was an evening of splendid music and company.

代码:

1
2
3
zhyfly@zhyfly:~/bash$ sed -n -e '2,/23:00/p' test.txt
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.

代码:

1
2
3
zhyfly@zhyfly:~/bash$ sed -n -e '2,3!p' test.txt
The honeysuckle band played all night long for only $90.
The local nurse Miss P.Neave was in attendance.

代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
zhyfly@zhyfly:~/bash$ sed -e '=' test.txt
1
The honeysuckle band played all night long for only $90.
2
It was an evening of splendid music and company.
3
Too bad the disco floor fell through at 23:00.
4
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -n -e '=' test.txt
1
2
3
4
zhyfly@zhyfly:~/bash$ sed -n -e '/music/p' test.txt
It was an evening of splendid music and company.
zhyfly@zhyfly:~/bash$ sed -n -e '/music/=' test.txt
2
zhyfly@zhyfly:~/bash$ sed -n -e '/music/p' -e '/music/=' test.txt
It was an evening of splendid music and company.
2
zhyfly@zhyfly:~/bash$ sed -e '=;p' test.txt
1
The honeysuckle band played all night long for only $90.
The honeysuckle band played all night long for only $90.
2
It was an evening of splendid music and company.
It was an evening of splendid music and company.
3
Too bad the disco floor fell through at 23:00.
Too bad the disco floor fell through at 23:00.
4
The local nurse Miss P.Neave was in attendance.
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -n -e '=;p' test.txt
1
The honeysuckle band played all night long for only $90.
2
It was an evening of splendid music and company.
3
Too bad the disco floor fell through at 23:00.
4
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -n -e '=' -e 'p' test.txt
1
The honeysuckle band played all night long for only $90.
2
It was an evening of splendid music and company.
3
Too bad the disco floor fell through at 23:00.
4
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$

附加 [address]a\附加内容 #缺省放在每一行后面

代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
zhyfly@zhyfly:~/bash$ sed -e 'a\this line will be added to the end of each line!oooooooooo' test.txt
The honeysuckle band played all night long for only $90.
this line will be added to the end of each line!oooooooooo
It was an evening of splendid music and company.
this line will be added to the end of each line!oooooooooo
Too bad the disco floor fell through at 23:00.
this line will be added to the end of each line!oooooooooo
The local nurse Miss P.Neave was in attendance.
this line will be added to the end of each line!oooooooooo
zhyfly@zhyfly:~/bash$ sed -e '/music/a\this line will be added to the end of the matching line!oooooooooo' test.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
this line will be added to the end of the matching line!oooooooooo
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.

插入 [address]i\插入内容 #缺省放在每一行前面

代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
zhyfly@zhyfly:~/bash$ sed -e 'i\this line will be inserted to the begin of each line!oooooooooo' test.txt
this line will be inserted to the begin of each line!oooooooooo
The honeysuckle band played all night long for only $90.
this line will be inserted to the begin of each line!oooooooooo
It was an evening of splendid music and company.
this line will be inserted to the begin of each line!oooooooooo
Too bad the disco floor fell through at 23:00.
this line will be inserted to the begin of each line!oooooooooo
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e '/music/i\this line will be inserted to the begin of the matching line!oooooooooo' test.txt
The honeysuckle band played all night long for only $90.
this line will be inserted to the begin of the matching line!oooooooooo
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.

更改行 [address]c\更改后整行内容 #缺省修改每一行

代码:

01
02
03
04
05
06
07
08
09
10
zhyfly@zhyfly:~/bash$ sed -e 'c\this line will be modified to the each line!oooooooooo' test.txt
this line will be modified to the each line!oooooooooo
this line will be modified to the each line!oooooooooo
this line will be modified to the each line!oooooooooo
this line will be modified to the each line!oooooooooo
zhyfly@zhyfly:~/bash$ sed -e '/music/c\this line will be modified to the matching line!oooooooooo' test.txt
The honeysuckle band played all night long for only $90.
this line will be modified to the matching line!oooooooooo
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.

删除定位行

代码:

1
2
3
4
5
6
7
8
9
zhyfly@zhyfly:~/bash$ cat test.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e '/music/d' test.txt
The honeysuckle band played all night long for only $90.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.

5.sed高级用法—替换

替换

1
2
3
4
[address]s/old/new/g
  
[address]s/pattern/replacement #the first occurrence on the address(缺省所有行)
[address]s/pattern/replacement/g #all occurrences on the address(缺省所有行)

代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
zhyfly@zhyfly:~/bash$ cat test.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e 's/h/ooooo/' test.txt
Toooooe honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad toooooe disco floor fell through at 23:00.
Toooooe local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e 's/h/ooooo/g' test.txt
Toooooe ooooooneysuckle band played all nigooooot long for only $90.
It was an evening of splendid music and company.
Too bad toooooe disco floor fell tooooorougooooo at 23:00.
Toooooe local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e '1,2s/h/ooooo/' test.txt
Toooooe honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e '1,2s/h/ooooo/g' test.txt
Toooooe ooooooneysuckle band played all nigooooot long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e '/\$/s/h/ooooo/g' test.txt
Toooooe ooooooneysuckle band played all nigooooot long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e '/^It/,/23:00/s/l/ooooo/g' test.txt
The honeysuckle band played all night long for only $90.
It was an evening of spoooooendid music and company.
Too bad the disco fooooooor feoooooooooo through at 23:00.
The local nurse Miss P.Neave was in attendance.

使用替换修改字符串 &

代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
zhyfly@zhyfly:~/bash$ cat test.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e 's/$90/&amp;230/g' test.txt
The honeysuckle band played all night long for only $90230.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e 's/90/120,&amp;/g' test.txt
The honeysuckle band played all night long for only $120,90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.

分隔符变换(避免产生歧义)

代码:

1
2
3
4
zhyfly@zhyfly:~/bash$ echo $PWD
/home/zhyfly/bash
zhyfly@zhyfly:~/bash$ echo $PWD|sed -e 's:/:@:g'
@home@zhyfly@bash

规则表达式混乱

题目

1
2
zhyfly@zhyfly:~/bash$ cat test
This</b> is what <b>I</b> meant.

想要得到的答案:

This is what I meant.

错误解法:

代码:

1
2
3
4
zhyfly@zhyfly:~/bash$ cat test
This&lt;/b&gt; is what &lt;b&gt;I&lt;/b&gt; meant.
zhyfly@zhyfly:~/bash$ sed -e 's/&lt;.*&gt;//g' test
This meant.

正确解法:

代码:

1
2
zhyfly@zhyfly:~/bash$ sed -e 's/&lt;[^&gt;]*&gt;//g' test
This is what I meant.

题目

要用sed把字符“\”转化成“’”该怎么写?

错误解法:

代码:

01
02
03
04
05
06
07
08
09
10
zhyfly@zhyfly:~/bash$ echo "hello\hh\haha"
hello\hh\haha
zhyfly@zhyfly:~/bash$ echo "hello\hh\haha"|sed -e 's/\\/'/g'
&gt;
&gt; bash: unexpected EOF while looking for matching `''
bash: syntax error: unexpected end of file
zhyfly@zhyfly:~/bash$ echo "hello\hh\haha"|sed -e 's/\\/\'/g'
&gt;
&gt; bash: unexpected EOF while looking for matching `''
bash: syntax error: unexpected end of file

正确解法:

代码:

1
2
3
zhyfly@zhyfly:~/bash$ echo "hello\hh\haha"|sed -e 's/\\/'"'"'/g'
hello'hh'haha
zhyfly@zhyfly:~/bash$

你看出来了吗?

更多字符匹配:
‘[a-x]*’
这将匹配零或多个全部为 ‘a’、’b'、’c'…’v'、’w'、’x’ 的字符。另外,可以使用 ‘[:space:]‘ 字符类来匹配空格。
以下是可用字符类的相当完整的列表:
字符类 描述

代码:

01
02
03
04
05
06
07
08
09
10
11
12
[:alnum:] 字母数字 [a-z A-Z 0-9]
[:alpha:] 字母 [a-z A-Z]
[:blank:] 空格或制表键
[:cntrl:] 任何控制字符
[:digit:] 数字 [0-9]
[:graph:] 任何可视字符(无空格)
[:lower:] 小写 [a-z]
[:print:] 非控制字符
[:punct:] 标点字符
[:space:] 空格
[:upper:] 大写 [A-Z]
[:xdigit:] 十六进制数字 [0-9 a-f A-F]

关于域应用一个较复杂的例子讲解

题目:

[file1.txt]
1C2
1C3
1C31
1C32
1C4
2C3
2C4
1D1
1D10
1D12
1D2
1D3
1D31
1RC2
1RC20
1RC21
1RC3
1RC31
1WR1
1WR2
1WR20
1WR21
1WR23

排序后

[file2.txt]
1 C 2
1 C 3
2 C 3
1 C 4
2 C 4
1 C 31
1 C 32
1 D 1
1 D 2
1 D 3
1 D 10
1 D 12
1 D 31
1 RC 2
1 RC 3
1 RC 20
1 RC 21
1 RC 31
1 WR 1
1 WR 2
1 WR 20
1 WR 21
1 WR 23

规律:将每行分成三部分: “数字1” “字符串” “数字2”(即三个域)

第一、三字段按numberic顺序排序,中间部分按字母排序 ,优先级顺序2 3 1

解答思路:

首先需要将文件的每行分成三个域,这就利用到sed的分域功能,可以这样分(其中包含分域的格式):

代码:

1
/^\([0-9]*\)\([A-Z]*\)\([0-9]*\)/\1 \2 \3/

代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
zhyfly@zhyfly:~/bash$ cat file1.txt
1C2
1C3
1C31
1C32
1C4
2C3
2C4
1D1
1D10
1D12
1D2
1D3
1D31
1RC2
1RC20
1RC21
1RC3
1RC31
1WR1
1WR2
1WR20
1WR21
1WR23

分域后

代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
zhyfly@zhyfly:~/bash$ sed -e 's/^\([0-9]*\)\([A-Z]*\)\([0-9]*\)/\1 \2 \3 /g' file1.txt
1 C 2
1 C 3
1 C 31
1 C 32
1 C 4
2 C 3
2 C 4
1 D 1
1 D 10
1 D 12
1 D 2
1 D 3
1 D 31
1 RC 2
1 RC 20
1 RC 21
1 RC 3
1 RC 31
1 WR 1
1 WR 2
1 WR 20
1 WR 21
1 WR 23

排序后

代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
zhyfly@zhyfly:~/bash$ sed -e 's/^\([0-9]*\)\([A-Z]*\)\([0-9]*\)/\1 \2 \3 /g' file1.txt|sort +1 -2 +2n +0 -1
1 C 2
1 C 3
2 C 3
1 C 4
2 C 4
1 C 31
1 C 32
1 D 1
1 D 2
1 D 3
1 D 10
1 D 12
1 D 31
1 RC 2
1 RC 3
1 RC 20
1 RC 21
1 RC 31
1 WR 1
1 WR 2
1 WR 20
1 WR 21
1 WR 23

下面的例子比较复杂:

题目

文件内容file.txt:
123456 345678 2005-05-06 123456
123456 234567 2003-5-6 234567
345555 987644 2003-4-23 543333
555555 999999 2004-11-5 999999

要将第四列数据变成正常的年月日,将2003-5-6 变成2003-05-0;
2003-4-23变成2003-04-23; 2004-11-5变成 2004-11-05

解答

首先将需要改变的部分分域

代码:

1
2
/-\([0-9]\)-/-0\1-/  #月
/-\([0-9]\) /-0\1 /  #日

注意实现起来又有多种方法:

代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
zhyfly@zhyfly:~/bash$ cat file.txt
123456 345678 2005-05-06 123456
123456 234567 2003-5-6 234567
345555 987644 2003-4-23 543333
555555 999999 2004-11-5 999999
zhyfly@zhyfly:~/bash$ sed -e 's/-\([0-9]\)-/-0\1-/g' -e 's/-\([0-9]\) /-0\1 /g' file.txt
123456 345678 2005-05-06 123456
123456 234567 2003-05-06 234567
345555 987644 2003-04-23 543333
555555 999999 2004-11-05 999999
zhyfly@zhyfly:~/bash$ sed -e 's/-\([0-9]\)-/-0\1-/g;s/-\([0-9]\) /-0\1 /g' file.txt
123456 345678 2005-05-06 123456
123456 234567 2003-05-06 234567
345555 987644 2003-04-23 543333
555555 999999 2004-11-05 999999
zhyfly@zhyfly:~/bash$ sed -e :a -e 's/-\([0-9]\)\([- ]\)/-0\1\2/;ta' file.txt
123456 345678 2005-05-06 123456
123456 234567 2003-05-06 234567
345555 987644 2003-04-23 543333
555555 999999 2004-11-05 999999

注:
:a – label
ta – goto to :a to rerun sed if the last sed is finished successfully.
简单说, 就是循环。

多个命令常用的方法
主要有
a.利用多个-e选项
b.利用 ;
c.使用脚本文件

代码:

01
02
03
04
05
06
07
08
09
10
zhyfly@zhyfly:~/bash$ sed -e '2,3s/a/ooooo/g' -e '2,3s/d/ddddd/g' test.txt
The honeysuckle band played all night long for only $90.
It wooooos ooooon evening of splendddddiddddd music ooooonddddd compooooony.
Too boooooddddd the dddddisco floor fell through ooooot 23:00.
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e '2,3s/a/ooooo/g;2,3s/d/ddddd/g' test.txt
The honeysuckle band played all night long for only $90.
It wooooos ooooon evening of splendddddiddddd music ooooonddddd compooooony.
Too boooooddddd the dddddisco floor fell through ooooot 23:00.
The local nurse Miss P.Neave was in attendance.

前两种方法比较简单,下面重点讲一下第三种方法:

创建sed脚本文件–>赋予执行权限–>执行文件

代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
zhyfly@zhyfly:~/bash$ cat sub.sed
#!/bin/sed -f
2,3s/a/ooooo/g
2,3s/d/ddddd/g
zhyfly@zhyfly:~/bash$ sudo chmod +x sub.sed
zhyfly@zhyfly:~/bash$ cat test.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ ./sub.sed test.txt
The honeysuckle band played all night long for only $90.
It wooooos ooooon evening of splendddddiddddd music ooooonddddd compooooony.
Too boooooddddd the dddddisco floor fell through ooooot 23:00.
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -f sub.sed test.txt
The honeysuckle band played all night long for only $90.
It wooooos ooooon evening of splendddddiddddd music ooooonddddd compooooony.
Too boooooddddd the dddddisco floor fell through ooooot 23:00.
The local nurse Miss P.Neave was in attendance.

转载于:https://my.oschina.net/alphajay/blog/34916

【Linux】sed命令用法详解相关推荐

  1. linux sed 命令,Linux sed命令用法详解

    提到 sed 命令,就不得提到 Sed 工具.Sed(stream editor)是一种非交互式的流编辑器,通过多种转换修改流经它的文本. 首先,Sed 通过文件或管道读取文件的内容,在默认情况下,S ...

  2. linux中用less命令,Linux less 命令用法详解

    原标题:Linux less 命令用法详解 less 是一个Linux命令行实用程序,用于显示文件或命令输出的内容,它一次只显示一个页面.它类似于 more ,但具有更高级的功能,允许您在文件中向前和 ...

  3. linux教程 sed命令的用法,Linux基础教程之文件三剑客sed命令用法详解

    sed是一种流编辑器,它是文本处理中非常强大的工具,能够完美的配合正则表达式使用,用法简单实用,非常灵活. 工作原理:sed命令处理文本时,把当前处理的行存储在一个临时缓冲区中,称为"模式空 ...

  4. Linux sed命令实例详解

    简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(pattern space),接着用sed命令处理缓冲区中的内容,处 ...

  5. linux中split函数用法,Linux csplit 命令用法详解-Linux命令大全(手册)

    csplit Linux csplit命令用于分割文件. 将文件依照指定的范本样式予以切割后,分别保存成名称为xx00,xx01,xx02-的文件.若给予的文件名称为"-",则cs ...

  6. Linux who命令用法详解命令用法详解

    Linux who命令 Linux who命令用于显示系统中有哪些使用者正在上面,显示的资料包含了使用者 ID.使用的终端机.从哪边连上来的.上线时间.呆滞时间.CPU 使用量.动作等等. 使用权限: ...

  7. Linux tcpdump命令用法详解

    From: http://os.51cto.com/art/201005/201921.htm TCPDUMP简介 在传统的网络分析和测试技术中,嗅探器(sniffer)是最常见,也是最重要的技术之一 ...

  8. linux grep截取字符数,Linux grep命令用法详解

    grep: Global search regular expression and print out the line. 作用:文本搜索工具,根据用户指定的"模式(过滤条件)" ...

  9. linux pmap命令,Linux pmap 命令用法详解-Linux命令大全(手册)

    pmap 报告进程的内存映射关系 补充说明 pmap命令 用于报告进程的内存映射关系,是Linux调试及运维一个很好的工具. 语法 pmap(选项)(参数) 选项 -x:显示扩展格式: -d:显示设备 ...

  10. Linux wget命令用法详解

    Linux系统中的wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTPS和FTP协 ...

最新文章

  1. read-sequence的返回值
  2. ActivityIndicator(菊花)的用法
  3. 单节点部署Hadoop教程
  4. 【转】Win8/8.1/Win7小技巧:揪出C盘空间占用的真凶
  5. Activity的LaunchMode和taskAffinity
  6. win10证书服务器不可用,win10系统提示“安全证书的吊销信息不可用”的修复方法...
  7. 工作线程AfxBeginThread的使用
  8. c语言中判断输入是否为数字_C语言编程判断回文数
  9. 笔记本电脑磁盘加密技术
  10. 所有网站都是用虚拟服务器吗,哪类网站不适合使用虚拟主机?
  11. ChineseLunisolarCalendar 农历日期
  12. YOLO v3源码详解
  13. Knockout+RequireJS+Sammy搭建简单的SPA脚手架
  14. 浮动元素将父级高度撑开
  15. 【数据库系统】数据库编程
  16. verilog 实现32位加法器(超前进位)
  17. html5 3d游戏引擎演示,HTML5、JavaScript 3D游戏引擎和框架
  18. ABP开发框架前后端开发系列---(7)系统审计日志和登录日志的管理
  19. 【PDN仿真笔记6-使用Sigrity PowerSI进行PDN仿真的方法1】
  20. nginx入门基础(内附教学视频)

热门文章

  1. android内存泄露_Java应用程序中的内存泄漏及内存管理
  2. 中国计算机管理软件学院,中国管理软件学院官方网站
  3. 页面右下角弹广告案例
  4. PHP 模拟库存出库商品
  5. go.mod 文件中的 //indirect
  6. GLib-CRITICAL : g_variant_get_uint32: assertion ‘g_variant_is_of_type (value, G_VARIANT_TYPE_UINT32)
  7. ProGuard在windows上一定要加dontusemixedcaseclassnames
  8. 编程基本功:知识、经历可以获得,能力不可能提升
  9. VC使用命令行编译很复杂
  10. LINUX获得线程号、设置线程名