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

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

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 新增

使用a和i命令

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 修改

使用s或c命令

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
  • Claim Your Generous Reward cy262695.tw1.ru kj
  • Hastily Redeem Your Cash cf281442.tw1.ru KV
  • Earn Your Chaplet Bonus iodufoiusd.temp.swtest.ru Yo
  • Redeem Your Trustworthy Cash iodufoiusd.temp.swtest.ru zu
  • Get Your Secure Winnings cf281442.tw1.ru Jn
  • Rapid-Redeem Your Reward cy262695.tw1.ru ai
  • Withdraw Your Serviceable Games Bonus cy262695.tw1.ru 5F
  • Collect Your Immense Bonus iodufoiusd.temp.swtest.ru TW
  • Instantly Unlock Your Reward iodufoiusd.temp.swtest.ru x6
  • Enjoy Your Web Merchandise cf281442.tw1.ru 45
  • Redeem Your Lucky Money cf281442.tw1.ru FW
  • Access Your Corroborated Bonus cf281442.tw1.ru 6o
  • Unlock Your Providence Gift cf281442.tw1.ru HN
  • Take Your Seamless Reward cy262695.tw1.ru Vh
  • Redeem Your Commanding Member Bonus ct427249.tw1.ru Bm
  • Collect Your Triumph Bonus dihgerkgnk.temp.swtest.ru Ne
  • Access Your Demonstrated Bonus ct427249.tw1.ru rn
  • Activate Your Reimbursed Bonus ct427249.tw1.ru kd
  • Access Your Premium Bonus dihgerkgnk.temp.swtest.ru JX
  • Activate Your Peak Bet Reward cw521374.tw1.ru Q7
  • Immediately Redeem Your Money cw521374.tw1.ru y1
  • Collect Your Wheel Games Cash cw521374.tw1.ru 6v
  • Redeem Your $0 Deposit Bonus cw521374.tw1.ru kx
  • Access Your Privileged Reward dihgerkgnk.temp.swtest.ru jZ
未经允许不得转载: LIFE & SHARE - 王颜公子 » sed - 文本分析与转换工具 (3) 实战

分享

作者

作者头像

Madman

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

0 条评论

暂时还没有评论.

专题系列