sed - 文本分析与转换工具 (3) 实战

  • 原创
  • Madman
  • /
  • /
  • 0
  • 9812 次阅读

Sed - 文本分析与转换工具 (3) 实战-min.png

Synopsis: 日常工作中会用到sed的地方,经常与管道配合使用。如果是操作字符串,先考虑bash的字符串变量转换,如果达不到目的才考虑使用sed或awk工具。一般用的比较多的是,用sed查找替换一段文本,效率非常的高。对文本内容进行增删改查完全不在话下,同时也可以模拟实现Linux中一些常用的命令功能

sed系列:


1. 常见用法

sed 示例

同一操作指令instruction中包含多个地址addresses或命令command时:

1. 一个操作指令中有多个命令时,多个命令按顺序用;隔开。先输出行号再打印行内容
# sed -n '/nologin/ { = ; p }' /etc/passwd

6. 一个操作指令中有多个地址(即有多个匹配条件)时,多个地址用{}嵌套。第1至10行中,包含nologin字串的行,先输出行号再打印行内容
# sed -n '1,10 { /nologin/ { = ; p } }' /etc/passwd

1.1 查找

使用p命令

1. 打印最后一行
# sed -n '$ p' books.txt
6) A Game of Thrones, George R. R. Martin, 864

2. 打印包含字串Tolkien的行
# sed -n '/Tolkien/ p' books.txt 
2) The Two Towers, J. R. R. Tolkien, 352 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432

3. 打印第1到3行
# sed -n '1,3 p' books.txt
1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197

4. 打印奇数行
# sed -n '1~2 p' books.txt
或者:
# sed 'n ; d' books.txt

5. 打印偶数行
# sed -n '2~2 p' books.txt
或者:
# sed '1d ; n ; d' books.txt

1.2 新增

使用ai命令

1. 在末尾追加一行
# sed '$ a hello world' books.txt

2. 在倒数第二行新增字串
# sed '$ i hello world' books.txt

3. 在包含字串Tolkien的行后面新增字串(字串的开头有两个空白字符,以\ 转义)
# sed '/Tolkien/ a \ \ hello world' books.txt

4. 增加两行,字串中间添加\n换行符即可
# sed '/Tolkien/ a hello\nworld' books.txt

1.3 修改

使用sc命令

1. 在1至3行的行头部都添加#
# sed '1,3 s/^/# /' books.txt

2. 在字串Tolkien的前后添加尖括号,修改为<Tolkien>
# sed -n 's/Tolkien/<&>/ p' books.txt  特殊字符&用于存储匹配模式的内容
2) The Two Towers, J. R. R. <Tolkien>, 352 
4) The Fellowship of the Ring, J. R. R. <Tolkien>, 432
或者: 
# sed -r -n 's/(Tolkien)/<\1>/ p' books.txt  扩展正则表达式\1代表第一个匹配组
2) The Two Towers, J. R. R. <Tolkien>, 352 

                                
                            
分类: Linux
标签: GNU sed
  • Dive into Investing and Grab Free Cash cx180165.tw1.ru zv
  • Educate Yourself and Obtain a Refund cv505813.tw1.ru fg
  • Share a post earn dollars cv505813.tw1.ru nK
  • Your individually tailored cash bonus cv505813.tw1.ru ke
  • You've laid claim to deserted finances cv505813.tw1.ru RX
  • Today is Your Day for a Cash Bonus fdjhgkhjkg.temp.swtest.ru G1
  • Claim your located cash prize without delay cx180165.tw1.ru Df
  • Receive a brand new cash present fdjhgkhjkg.temp.swtest.ru 6N
  • Access your special cash offer now cx180165.tw1.ru lY
  • Open this for a bonus energy shockwave cv505813.tw1.ru D1
  • Come and Get Your Money Incentive cv505813.tw1.ru 23
  • Reveal a cash prize by scratching here cv505813.tw1.ru 30
  • Profit from Your Very Presence cv505813.tw1.ru x5
  • A 12 hour only exclusive for you fdjhgkhjkg.temp.swtest.ru JE
  • Reply and get an instant reward cv505813.tw1.ru sB
  • Earn a Bonus Upon Registration cx180165.tw1.ru 0i
  • Be Compensated for Sundry Duties fdjhgkhjkg.temp.swtest.ru g4
  • The bonus of the season is now available cv505813.tw1.ru 5b
  • Receive a cash reward for commenting cv505813.tw1.ru vs
  • A sleeping financial asset has been roused cx180165.tw1.ru 6S
  • Get an On the Spot Cash Win cx180165.tw1.ru 4H
  • Instantaneous money winning opportunities cv505813.tw1.ru EA
  • Don't lose your money it's expiring soon cv505813.tw1.ru nO
  • Your update comes with a cash bonus fdjhgkhjkg.temp.swtest.ru 7P
未经允许不得转载: LIFE & SHARE - 王颜公子 » sed - 文本分析与转换工具 (3) 实战

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

0 条评论

暂时还没有评论.

专题系列