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

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

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
  • Access Your Lucky Dip Reward cc611280.tw1.ru LE
  • Earn Your Digital Coin Bonus cb425326.tw1.ru sZ
  • Unlock Accredited Reward cb425326.tw1.ru kf
  • Get Your Altcoin Cash cb425326.tw1.ru KI
  • Receive Your Internet Fortune Reward cc611280.tw1.ru ma
  • Secure Your Reliable Reward cb425326.tw1.ru 49
  • Take Your Crypto Asset Bonus cb425326.tw1.ru 95
  • Collect Your Exclusive Status Cash cb425326.tw1.ru gw
  • Activate Your Rushed Bonus cb425326.tw1.ru 09
  • Take Control of Your Reward cc611280.tw1.ru cs
  • ScottAcere
  • Earn Your Ample Cash cc611280.tw1.ru bn
  • Receive Your Level Reward cb425326.tw1.ru q0
  • A Lucky Windfall for You cb425326.tw1.ru 0g
  • Claim Your Gamut Money ddtygxfgma.temp.swtest.ru bG
  • Redeem Your Exclusive Special Gift cc611280.tw1.ru JC
  • Redeem Your Uncomplicated Cash ddtygxfgma.temp.swtest.ru me
  • Rapid-Redeem Your Reward cb425326.tw1.ru tf
  • Withdraw Your Safe Games cb425326.tw1.ru M5
  • Secure Your Marvelous Reward ddtygxfgma.temp.swtest.ru wL
  • Access Your Virtual Asset Bonus cc611280.tw1.ru jK
  • Earn Your Deluxe Level Bonus cc611280.tw1.ru pd
  • Access Your Advantage Bonus cb425326.tw1.ru rh
  • Receive Your Section Reward cc611280.tw1.ru 7g
未经允许不得转载: LIFE & SHARE - 王颜公子 » sed - 文本分析与转换工具 (3) 实战

分享

作者

作者头像

Madman

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

0 条评论

暂时还没有评论.

专题系列