linux sed基本用法

葫芦的运维日志

下一篇 搜索 上一篇

浏览量 4797

2015/03/13 01:21


sed命令选项
-e script 在处理输入时,将script中指定的命令天到运行的命令中。
-f file 在处理输入时,将file中指定的命令添加到运行的命令中
-n 不要为每个命令生成输出,等待print命令来输出
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@pppoe ~]# echo this  is wangzi | sed 's/wangzi/tiancai w/'
this is tiancai w
sed编辑器本身不会修改文本文件的数据。他只会将修改后的数据发送到stdout。如果查看原来的文本文件,他仍然啊保留着原始数据。
[root@pppoe ~]# cat s
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
[root@pppoe ~]# sed -e 's/brown/green/; s/dog/cat/' s
The quick green fox jumps over the lazy cat
The quick green fox jumps over the lazy cat
The quick green fox jumps over the lazy cat
The quick green fox jumps over the lazy cat

[root@pppoe ~]# sed -e '
> s/brown/green/
> s/fox/elephant/
> s/dog/cat/' s
The quick green elephant jumps over the lazy cat
The quick green elephant jumps over the lazy cat
The quick green elephant jumps over the lazy cat
The quick green elephant jumps over the lazy cat

[root@pppoe ~]# cat s
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
[root@pppoe ~]# sed -f script1 s
The quick green elephant jumps over the lazy cat
The quick green elephant jumps over the lazy cat
The quick green elephant jumps over the lazy cat
The quick green elephant jumps over the lazy cat
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[root@pppoe ~]# cat script3
{
text = "'s home directory is"
print $1 text $6
}
[root@pppoe ~]# gawk -F: -f script3 /etc/passwd
root's home directory is/root
bin's home directory is/bin
daemon's home directory is/sbin
adm's home directory is/var/adm
lp's home directory is/var/spool/lpd
sync's home directory is/sbin
shutdown's home directory is/sbin

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@pppoe ~]# gawk 'BEGIN {print "Hello World!"}'
Hello World!
[root@pppoe ~]#
这次print命令会在读取任何数据前显示文本。但在他显示了文本后,他会快速退出而不用等待任何数据。这样的原因是在gawk处理任何数据前,BEGIN关键字只执行指定的脚本。要想在正常的程序脚本中处理数据,必须用另一个脚本段来定义程序。 
[root@pppoe ~]# cat data4
Line 1
Line 2
Line 3
Line 4
[root@pppoe ~]# gawk 'BEGIN { print "The data4 file contents:" } { print $0}' data4
The data4 file contents:
Line 1
Line 2
Line 3
Line 4

[root@pppoe ~]# gawk 'BEGIN { print "The data4 file contents:" } { print $0} END                                                                                                                                                              { print "End of ile" }' data4
The data4 file contents:
Line 1
Line 2
Line 3
Line 4
End of ile
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@pppoe ~]# cat script4
BEGIN {
print "The latest list of users and shells"
print "Userid     shell"
print "------     -----"
FS=":"
}
{
print $1 "     " $7
}
END{
print "This concludes the listing"
}
[root@pppoe ~]# gawk -f script4 /etc/passwd
The latest list of users and shells
Userid     shell
------     -----
root     /bin/bash
bin     /sbin/nologin
daemon     /sbin/nologin
adm     /sbin/nologin
lp     /sbin/nologin
sync     /bin/sync
shutdown     /sbin/shutdown

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sed 's/test/trial/' data5
默认情况下substitute命令在替换多行中的文本时能正常工作,但默认情况下他只替换每行中出现的第一处。要让替换命令对一行中不同的地方出现的文本都起作用,必须使用替换标记(substitution flag).替换标记会在替换命令字符串之后设置。
s/pattern/replacement/flags
有四种替换标记
1、数字 表明新文本将替换地挤出模式匹配的地方
2、g  表明新文本将会替换所有已有文本出现的地方
3、p 表明原来行的内容要打印出来
4、 w file 将替换的结果写到文件中
[root@pppoe ~]# cat data5
This is a test line
this is a different line
[root@pppoe ~]# sed -n 's/test/wz/p' data5
This is a wz line

[root@pppoe ~]# sed 's/test/wz/w te' data5
This is a wz line
this is a different line
[root@pppoe ~]# cat te
This is a wz line
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@pppoe ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

[root@pppoe ~]# sed 's!/bin/bash!/bin/csh!' /etc/passwd
root:x:0:0:root:/root:/bin/csh
bin:x:1:1:bin:/bin:/sbin/nologin
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[address]command
or
address {
command1
command2
command3
}
[root@pppoe ~]# cat s
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
只替换第二行
[root@pppoe ~]# sed '2s/dog/cat/' s
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
[root@pppoe ~]# sed '2,4s/dog/cat/' s
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
替换2到最后一行
[root@pppoe ~]# sed '2,$s/dog/cat/' s
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
文本模式过滤
[root@pppoe ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@pppoe ~]# sed '/root/s/bash/csh/' /etc/passwd
root:x:0:0:root:/root:/bin/csh
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync

[root@pppoe ~]# cat s
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
[root@pppoe ~]# sed '3,${
> s/brown/wz/
> s/dog/cat/
> }' s
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick wz fox jumps over the lazy cat
The quick wz fox jumps over the lazy cat
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
删除行[root@pppoe ~]# cat data3
one line of test.
two lin of test.
thress line is test
[root@pppoe ~]# sed 'd' data3
[root@pppoe ~]# sed '2d' data3
one line of test.
thress line is test

[root@pppoe ~]# sed '2,3d' data3
one line of test.
[root@pppoe ~]# sed '2,$d' data3
one line of test.

[root@pppoe ~]# sed '/one line/d' data3
two lin of test.
thress line is test
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
插入和附件文本
[root@pppoe ~]# echo "Test line 2" | sed 'i\Test d 1'
Test d 1
Test line 2
[root@pppoe ~]# echo "Test line 2" | sed 'a\Test d 1'
Test line 2
Test d 1
在第三行前面插入一行数据
[root@pppoe ~]# sed '3i\wz' data3
one line of test.
two lin of test.
wz
thress line is test
在第三行后面插入一行数据

[root@pppoe ~]# sed '3a\wz' data3
one line of test.
two lin of test.
thress line is test
wz

[root@pppoe ~]# sed '$a\wz' data3
one line of test.
two lin of test.
thress line is test
wz
插入多行时必须对新文本的每一行使用反斜线。
[root@pppoe ~]# sed '1i\
> wz\
> wc\
> wd' data3
wz
wc
wd
one line of test.
two lin of test.
thress line is test
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
修改行
[root@pppoe ~]# sed '2c\wangzi' data3
one line of test.
wangzi
thress line is test

[root@pppoe ~]# sed '/two lin/c\
〉wz\
〉dd' data3
one line of test.
wz
dd
thress line is test

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
转换命令
[root@pppoe ~]# echo "This 1 is a test of 1 try." | sed 'y/123/456/'
This 4 is a test of 4 try.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
打印查询
[root@pppoe ~]# sed -n '/wangzi/p' /etc/ppp/chap-secrets
wangzi * wangzi *

[root@pppoe ~]# sed -n '2,3p' /etc/ppp/chap-secrets
wangzi * wangzi *
b21001 * 112100 *

[root@pppoe ~]# sed -n '/3/p' /etc/ppp/chap-secrets
b21013 * 112310 *
b21036 * 112630 *
b21103 * 112301 *
b21143 * 112341 *
b22013 * 112310 *
b22113 * 112311 *
b22134 * 112431 *
b22138 * 112831 *
b22237 * 112732 *
b22263 * 112362 *
b23051 * 112150 *
b22038 * 112830 *
b22063 * 112360 *
b23007   *   113700   *
b22083   *   112380   *
b23069   *   113960   *
带行号显示

[root@pppoe ~]# sed '=' /etc/ppp/chap-secrets
1
zhangzhang * zhangzhang *
2
wangzi * wangzi *
3
b21001 * 112100 *
4
b21013 * 112310 *
5
b21036 * 112630 *
6
b21050 * 112050 *
7
b21067 * 112760 *
8
b21103 * 112301 *
9
b21143 * 112341 *

带行号查询
[root@pppoe ~]# sed -n '/wangzi/{
> =
> p
> }' /etc/ppp/chap-secrets
2
wangzi * wangzi *
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[address]w filename
[root@pppoe ~]# sed -n '1,2w /fei1' /etc/ppp/chap-secrets
[root@pppoe ~]# cat /fei1
zhangzhang * zhangzhang *
wangzi * wangzi *
1,2w表示将1,2行写入/fei1文件,-n表示不讲结果显示在标准stdout上。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[address]r filename
[root@pppoe ~]# cat data3
one line of test.
two lin of test.
thress line is test
[root@pppoe ~]# sed '2r /fei' data3
one line of test.
two lin of test.
zhangzhang * zhangzhang *
wangzi * wangzi *
thress line is test
读取文件插入到数据流中指定行上。
[root@pppoe ~]# sed '$r /fei' data3
one line of test.
two lin of test.
thress line is test
zhangzhang * zhangzhang *
wangzi * wangzi *
插入到最后一行

[root@pppoe ~]# cat letter
WOULD THE FOLLOWING PEOPLE:
LIST
please report to the office
[root@pppoe ~]# sed '/LIST/{
r /fei
d
}' letter
WOULD THE FOLLOWING PEOPLE:
zhangzhang * zhangzhang *
wangzi * wangzi *
please report to the office

读入文件替换占位文本LIST。

 

葫芦的运维日志

打赏

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2021 王梓打赏联系方式 陕ICP备17005322号-1