专栏首页葫芦linux sed进阶

linux sed进阶

多行命令
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
~~~~~~~~~~~~~~~~~~~~~~~~

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • linux sed基本用法

    葫芦
  • linux shell创建临时文件

    [root@aoi ~]# cat d #!/bin/bash #creating and using a temp file tempfile=`mkt...

    葫芦
  • linux samba文件共享服务器

    葫芦
  • Linux 文件与目录管理 转

    pwd是Print Working Directory的缩写,也就是显示目前所在目录的命令。

    wuweixiang
  • 用 Python 分析 YouTube 百万条数据

    一份执着✘
  • k8s源码分析-----kubelet(6)statusManager

    第一时间获取文章,可以关注本人公众号 月牙寂道长 yueyajidaozhang

    月牙寂道长
  • Linux tree命令

    tree -L 4 >dirce.doc即可生成UTF8格式的文档..我们也可以在windows 下查看..

    流柯
  • Hive之导出文件按逗号分隔到本地文件

        如下所示,默认导出的是用\t分隔的,需要使用管道符进行转换,经常使用到,记录下.

    克虏伯
  • PHP内存模型

    php为单进程的,由apache代执行,每一个请求,由apache从进程池中取出进程,初始化数据结构,创建进程.

    mySoul
  • Android 性能优化

    总结就是,提高负载能力让程序运行更快,用更少的资源做更多的活就是性能优化。 2.为什么要性能优化 随着科技不断发展、移动互联网的迅猛发展,手机硬件不断进步以...

    萬物並作吾以觀復

扫码关注云+社区

领取腾讯云代金券