linux sed进阶

葫芦的运维日志

下一篇 搜索 上一篇

2013/12/19 01:41


多行命令
N: 将数据流的下一行加进来创建一个多行组来处理。
D: 删除多行组中的一行
P: 打印多行组中的一行

next命令
单行next命令
[root@localhost ~]# cat e
this is the header line.

this is a data line.

this is a header


this the last line

this header fdasf

wa
[root@localhost ~]# sed '/^$/d' e
this is the header line.
this is a data line.
this is a header
this the last line
this header fdasf
wa
[root@localhost ~]# sed '/header/{n;d}' e
this is the header line.
this is a data line.

this is a header

this the last line

this header fdasf
wa
合并文本行
[root@localhost ~]# cat data2
This is the header line.
This is the first data line.
This is the second data line
This is the last line.
[root@localhost ~]# sed '/first/{N;s/\n/ /}' data2
This is the header line.
This is the first data line. This is the second data line
This is the last line.


[root@localhost ~]# cat data3
The first meeting of the Linux System
Administrator's group will be heald on Tuesday.
All System Administrators should attend this meeting.
Thank you for your attendance.
[root@localhost ~]# sed 's/System Administrator/Desktop User/' data3
The first meeting of the Linux System
Administrator's group will be heald on Tuesday.
All Desktop Users should attend this meeting.
Thank you for your attendance.
[root@localhost ~]# sed 'N ; s/System.Administrator/Desktop User/' data3
The first meeting of the Linux Desktop User's group will be heald on Tuesday.
All Desktop Users should attend this meeting.
Thank you for your attendance.
[root@localhost ~]# sed '
N
s/System Administrator/Desktop User/
s/System\nAdministrator/Desktop\nUser/
' data3
The first meeting of the Linux Desktop
User's group will be heald on Tuesday.
All Desktop Users should attend this meeting.
Thank you for your attendance.
如果要匹配的在数据流中的最后一行,N命令会错过他。因为没有其他行可读入道模式空间跟这行合并。
[root@localhost ~]# sed '
N
s/System Administrator/Desktop User/
s/System\nAdministrator/Desktop\nUser/
' data3
The first meeting of the Linux Desktop
User's group will be heald on Tuesday.
All Desktop Users should attend this meeting.
Thank you for your attendance.
解决上面的问题只需将单行命令放到N前面多行命令放到它后面。如下

[root@localhost ~]# sed '
>
> s/System Administrator/Desktop User/
> N
> s/System\nAdministrator/Desktop\nUser/
> ' data3
The first meeting of the Linux Desktop
User's group will be heald on Tuesday.
All Desktop Users should attend this meeting.
[root@localhost ~]# cat data3
The first meeting of the Linux System
Administrator's group will be heald on Tuesday.
All System Administrators should attend this meeting.
[root@localhost ~]# sed '
s/System Administrator/Desktop User/
N
s/System\nAdministrator/Desktop\nUser/
' data3
The first meeting of the Linux Desktop
User's group will be heald on Tuesday.
All Desktop Users should attend this meeting.

删除数据流中出现在第一行前的空白行
[root@localhost ~]# cat data5

This is the header line.
This is a data line.

This is the last line.
[root@localhost ~]# sed '/^$/{N;/header/D}' data5
This is the header line.
This is a data line.

This is the last line.

当多行匹配出现时,P命令只会打印模式空间中的第一行。
[root@localhost ~]# cat data3
The first meeting of the Linux System
Administrator's group will be heald on Tuesday.
All System Administrators should attend this meeting.
[root@localhost ~]# sed -n  'N;/System\nAdministrator/P' data3
The first meeting of the Linux System

保持空间
h 将模式空间复制到保持空间
H 将模式空间复加到保持空间
g 将保持空间复制到模式空间
G 将保持空间复加到模式空间
x 交换模式空间和保持空间
[root@localhost ~]# cat data2
This is the header line.
This is the first data line.
This is the second data line
This is the last line.
[root@localhost ~]# sed -n '/first/{
> h
> p
> n
> p
> g
> p
> }' data2
This is the first data line.
This is the second data line
This is the first data line.
将1、2行相反顺序输出
[root@localhost ~]# sed -n '/first/{
> h
> n
> p
> g
> p
> }' data2
This is the second data line
This is the first data line.
反转行
[root@localhost ~]# sed -n '{1!G;h;$p}' data2
This is the last line.
This is the second data line
This is the first data line.
This is the header line.
跳转b
[address]b [label]

[root@localhost ~]# sed '{2,3b;s/This is/Is this/;s/line./test?/}' data2
Is this the header test?
This is the first data line.
This is the second data line
Is this the last test?

[root@localhost ~]# cat data2
This is the header line.
This is the first data line.
This is the second data line
This is the last line.
[root@localhost ~]# sed '{/first/b jump1;s/This is the/No jump on/
> :jump1
> s/This is the/Jump here on/}' data2
No jump on header line.
Jump here on first data line.
No jump on second data line
No jump on last line.

[root@localhost ~]# echo "This, is , a, test, to,remove, commas." | sed -n '{
:start
s/,//1p
b start
}'
This is , a, test, to,remove, commas.
This is  a, test, to,remove, commas.
This is  a test, to,remove, commas.
This is  a test to,remove, commas.
This is  a test toremove, commas.
This is  a test toremove commas.

必须用ctrl+c停止
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@localhost ~]# echo "This, is , a, test, to,remove, commas." | sed -n '{
:start
s/,//1p
/,/b start
}'
This is , a, test, to,remove, commas.
This is  a, test, to,remove, commas.
This is  a test, to,remove, commas.
This is  a test to,remove, commas.
This is  a test toremove, commas.
This is  a test toremove commas.
正常停止
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@localhost ~]# echo "This, is , a, test, to,remove, commas." | sed -n '{
:start
s/,//1p
t start
}'
This is , a, test, to,remove, commas.
This is  a, test, to,remove, commas.
This is  a test, to,remove, commas.
This is  a test to,remove, commas.
This is  a test toremove, commas.
This is  a test toremove commas.
正常停止,当没有替换命令要做时,测试命令不会跳转而是继续执行剩下的脚本。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@localhost ~]# sed '{
> s/first/matched/
> t
> s/This is the/No match on/
> }' data2
No match on header line.
This is the matched data line.
No match on second data line
No match on last line.
第一个替换命令会查找模式文本first.如果他匹配了行中的模式,他会替换文本,而且测试命令会跳过后面的替换命令。如果第一个替换命令未能匹配模式,第二个命令就会被执行。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
通配符.来匹配多个单词时遇到的问题。
[root@pppoe ~]# echo "The cat sleeps in his hat." | sed 's/cat/"cat"/'
The "cat" sleeps in his hat.
[root@pppoe ~]# echo "The cat sleeps in his hat." | sed 's/.at/".at"/g'
The ".at" sleeps in his ".at".
解决此问题引入&符号
[root@pppoe ~]# echo "The cat sleeps in his hat." | sed 's/.at/"&"/g'
The "cat" sleeps in his "hat".
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
替换单独的单词 ,没理解。。
[root@pppoe ~]# echo "The System Administrator manual" | sed '
s/\(System\) Administrator/\1 User/'
The System User manual


[root@pppoe ~]# echo "That furry cat is pretty" | sed 's/furry \(.at\)/\1/'
That cat is pretty
[root@pppoe ~]# echo "That furry cat is pretty" | sed 's/furry \(.at\)/\1 a/'
That cat a is pretty

[root@pppoe ~]# echo "1234567" | sed '{
> :start
> s/\(.*[0-9]\)\([0-9]\{3\}\)/\1.\2/
> t start
> }'
1.234.567
~~~~~~~~~~~
*[0-9]
[0-9]{3}
~~~~~~~~~~~~~~~~~~
在脚本中使用sed
[root@pppoe ~]# cat reverse
#!/bin/bash
sed -n '{
1!G
h
$p
}' $1
[root@pppoe ~]# /root/reverse 2
cat wz >$(date -d "today" +"%Y%m%d_%H%M%S").txt
#!/bin/bash
~~~~~~~~~~~~~~~~~~~~~~~~

 

葫芦的运维日志

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2019 王梓 赞助联系方式 陕ICP备17005322号