sed实践

简介:
  sed说白了就是对行进行操作,所有的操作围绕‘行’的相关操作展开,可以把它理解为一个行编辑器。
1、查看sed版本: sed --version
[oracle@136_20 ~]$ sed --version
GNU sed version 4.1.5
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.    There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.
数据准备:
[stanley@qa13625 test]$ cat datafile 
northwest             NW            Charles Main                        3.0         .98         3             34
western                 WE            Sharon Gray                         5.3         .97         5             23
southwest             SW            Lewis Dalsass                     2.7         .8            2             18
southern                SO            Suan Chin                             5.1         .95         4             15
southeast             SE            Patricia Hemenway             4.0         .7            4             17
eastern                 EA            TB Savage                             4.4         .84         5             20
northeast             NE            AM Main Jr.                         5.1         .94         3             13
north                     NO            Margot Weber                        4.5         .89         5             9
central                 CT            Ann Stephens                        5.7         .94         5             13

2、 sed -n '/north/p' datafile   查找行

[stanley@qa13625 test]$ sed -n '/north/p' datafile 
northwest             NW            Charles Main                        3.0         .98         3             34
northeast             NE            AM Main Jr.                         5.1         .94         3             13
north                     NO            Margot Weber                        4.5         .89         5             9

3、sed -n 's/north/NORTH/p' datafile 替换

[stanley@qa13625 test]$ sed -n 's/north/NORTH/p' datafile 
NORTHwest             NW            Charles Main                        3.0         .98         3             34
NORTHeast             NE            AM Main Jr.                         5.1         .94         3             13
NORTH                     NO            Margot Weber                        4.5         .89         5             9

4、定址处理

删除1到3行: sed '1,3d' datafile 

[stanley@qa13625 test]$ sed '1,3d' datafile 
southern                SO            Suan Chin                             5.1         .95         4             15
southeast             SE            Patricia Hemenway             4.0         .7            4             17
eastern                 EA            TB Savage                             4.4         .84         5             20
northeast             NE            AM Main Jr.                         5.1         .94         3             13
north                     NO            Margot Weber                        4.5         .89         5             9
central                 CT            Ann Stephens                        5.7         .94         5             13

模式查找:sed -n '/[Aa]nn/p' datafile 

[stanley@qa13625 test]$ sed -n '/[Aa]nn/p' datafile 
central                 CT            Ann Stephens                        5.7         .94         5             13

5、命令

a\:在当前行后面添加一行
c\:
d:delete
i\: insert, 在之前插入
h:
H:
g:
G:
I:list nonprinting characters
p:print lines
n:
q:quit
!:
s:替换
 
6、替换标记 substitution flags
g:global
p:print
w:write lines out to a file
x:exchange
y:translates one character to another

-e:多层编辑
-f:
-n:

删除包含 north的行: sed '/north/d' datafile 
[stanley@qa13625 test]$  sed '/north/d' datafile 
western                 WE            Sharon Gray                         5.3         .97         5             23
southwest             SW            Lewis Dalsass                     2.7         .8            2             18
southern                SO            Suan Chin                             5.1         .95         4             15
southeast             SE            Patricia Hemenway             4.0         .7            4             17
eastern                 EA            TB Savage                             4.4         .84         5             20
central                 CT            Ann Stephens                        5.7         .94         5             13

删除不包含north的行: sed '/north/!d' datafile 
[stanley@qa13625 test]$  sed '/north/!d' datafile 
northwest             NW            Charles Main                        3.0         .98         3             34
northeast             NE            AM Main Jr.                         5.1         .94         3             13
north                     NO            Margot Weber                        4.5         .89         5             9

在sh、ksh、bash中使用 sed '/north/!d' datafile  ; 在csh、tcsh中, 使用sed '/north/\!d' datafile

7、元字符
&:
^
.
*
[]
\
(
)
查找替换: sed 's/CT/**&**/g' datafil
[stanley@qa13625 test]$  sed 's/CT/**&**/g' datafile 
northwest             NW            Charles Main                        3.0         .98         3             34
western                 WE            Sharon Gray                         5.3         .97         5             23
southwest             SW            Lewis Dalsass                     2.7         .8            2             18
southern                SO            Suan Chin                             5.1         .95         4             15
southeast             SE            Patricia Hemenway             4.0         .7            4             17
eastern                 EA            TB Savage                             4.4         .84         5             20
northeast             NE            AM Main Jr.                         5.1         .94         3             13
north                     NO            Margot Weber                        4.5         .89         5             9
central                  **CT**            Ann Stephens                        5.7         .94         5             13

[stanley@qa13625 test]$  sed 's/north/&ing/g' datafile 
northingwest             NW            Charles Main                        3.0         .98         3             34
western                 WE            Sharon Gray                         5.3         .97         5             23
southwest             SW            Lewis Dalsass                     2.7         .8            2             18
southern                SO            Suan Chin                             5.1         .95         4             15
southeast             SE            Patricia Hemenway             4.0         .7            4             17
eastern                 EA            TB Savage                             4.4         .84         5             20
northingeast             NE            AM Main Jr.                         5.1         .94         3             13
northing                     NO            Margot Weber                        4.5         .89         5             9
central                 CT            Ann Stephens                        5.7         .94         5             13

取消默认打印输出:-n
[stanley@qa13625 test]$  sed '/south/p' datafile 
northwest             NW            Charles Main                        3.0         .98         3             34
western                 WE            Sharon Gray                         5.3         .97         5             23
southwest             SW            Lewis Dalsass                     2.7         .8            2             18
southwest             SW            Lewis Dalsass                     2.7         .8            2             18
southern                SO            Suan Chin                             5.1         .95         4             15
southern                SO            Suan Chin                             5.1         .95         4             15
southeast             SE            Patricia Hemenway             4.0         .7            4             17
southeast             SE            Patricia Hemenway             4.0         .7            4             17
eastern                 EA            TB Savage                             4.4         .84         5             20
northeast             NE            AM Main Jr.                         5.1         .94         3             13
north                     NO            Margot Weber                        4.5         .89         5             9
central                 CT            Ann Stephens                        5.7         .94         5             13
[stanley@qa13625 test]$  sed -n '/south/p' datafile 
southwest             SW            Lewis Dalsass                     2.7         .8            2             18
southern                SO            Suan Chin                             5.1         .95         4             15
southeast             SE            Patricia Hemenway             4.0         .7            4             17

以两位数字结尾的,在其后面加上'.5':

 sed 's/[0-9][0-9]$/&.5/' datafile

[stanley@qa13625 test]$ sed 's/[0-9][0-9]$/&.5/' datafile 
northwest             NW            Charles Main                        3.0         .98         3             34.5
western                 WE            Sharon Gray                         5.3         .97         5             23.5
southwest             SW            Lewis Dalsass                     2.7         .8            2             18.5
southern                SO            Suan Chin                             5.1         .95         4             15.5
southeast             SE            Patricia Hemenway             4.0         .7            4             17.5
eastern                 EA            TB Savage                             4.4         .84         5             20.5
northeast             NE            AM Main Jr.                         5.1         .94         3             13.5
north                     NO            Margot Weber                        4.5         .89         5             9
central                 CT            Ann Stephens                        5.7         .94         5             13.5

以#做为分隔符: sed 's#north#NORTH#g' datafile 

[stanley@qa13625 test]$ sed 's#north#NORTH#g' datafile 
NORTHwest             NW            Charles Main                        3.0         .98         3             34
western                 WE            Sharon Gray                         5.3         .97         5             23
southwest             SW            Lewis Dalsass                     2.7         .8            2             18
southern                SO            Suan Chin                             5.1         .95         4             15
southeast             SE            Patricia Hemenway             4.0         .7            4             17
eastern                 EA            TB Savage                             4.4         .84         5             20
NORTHeast             NE            AM Main Jr.                         5.1         .94         3             13
NORTH                     NO            Margot Weber                        4.5         .89         5             9
central                 CT            Ann Stephens                        5.7         .94         5             13

范围查找:以第一个west开始,第一个east结尾的之间的行:

sed -n '/west/,/east/p' datafile 
[stanley@qa13625 test]$ sed -n '/west/,/east/p' datafile 
northwest             NW            Charles Main                        3.0         .98         3             34
western                 WE            Sharon Gray                         5.3         .97         5             23
southwest             SW            Lewis Dalsass                     2.7         .8            2             18
southern                SO            Suan Chin                             5.1         .95         4             15
southeast             SE            Patricia Hemenway             4.0         .7            4             17

在范围之内替换
在范围内的末尾加上' **ALI** ':   sed '/west/,/east/s/$/**ALI**/' datafile
[stanley@qa13625 test]$ sed '/west/,/east/s/$/**ALI**/' datafile 
northwest             NW            Charles Main                        3.0         .98         3             34**ALI**
western                 WE            Sharon Gray                         5.3         .97         5             23**ALI**
southwest             SW            Lewis Dalsass                     2.7         .8            2             18**ALI**
southern                SO            Suan Chin                             5.1         .95         4             15**ALI**
southeast             SE            Patricia Hemenway             4.0         .7            4             17**ALI**
eastern                 EA            TB Savage                             4.4         .84         5             20
northeast             NE            AM Main Jr.                         5.1         .94         3             13
north                     NO            Margot Weber                        4.5         .89         5             9
central                 CT            Ann Stephens                        5.7         .94         5             13

去掉空行:sed '/^$/d' datafile 
源:
[oracle@136_20 tmp]$ more datafile 
0
1

8

10

13
14

目标:
[oracle@136_20 tmp]$  sed '/^$/d' datafile 
0
1
8
10
13
14

删除多个空行为一个空行
sed '/^$/{N;/^\n*$/D}' 
datafile

读取从第2行到第4行:sed -n 2,4p datafile

读取从第2行sed -n 2p datafile 
[oracle@136_20 tmp]$ sed -n 2,4p datafile 
1

8
[oracle@136_20 tmp]$ sed -n 2p datafile 
1

 
 本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/370689,如需转载请自行联系原作者
相关文章
|
Shell Perl 自然语言处理
|
Perl Linux 机器学习/深度学习
|
Perl 存储 网络安全