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

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

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
  • Instant cash prizes await in games cb848888.tw1.ru wQ
  • The key to your surprise cash gift cb815908.tw1.ru od
  • Your sign up comes with cash cb848888.tw1.ru uv
  • Earn Tuition Awards and Cash Bonuses ci817627.tw1.ru OR
  • Your unclaimed balance has been restored cb815908.tw1.ru 3s
  • Link up and we'll add a cash bonus ci817627.tw1.ru R2
  • Discover How to Unlock Free Cash cb815908.tw1.ru SY
  • Sunburstyxf
  • Access rapid funds here ck207265.tw1.ru 3i
  • Your update comes with a cash bonus cb815908.tw1.ru np
  • Take Your Monetary Bonus Today cb848888.tw1.ru UR
  • Make Money by Mastering New Things cb848888.tw1.ru Pt
  • A one day cash distribution event is live ck207265.tw1.ru cD
  • A personal cash present just for you cb848888.tw1.ru Ew
  • Receive a Generous Monetary Gift cb848888.tw1.ru nn
  • Access Monetary Rewards for Education cb815908.tw1.ru 3W
  • Your cash payout is prepared ck207265.tw1.ru sd
  • Play for a chance at quick cash cb848888.tw1.ru uq
  • A stunning revelation of free funds ck207265.tw1.ru I6
  • Step in and claim your winnings cb848888.tw1.ru zh
  • Immediate cash is housed inside ck207265.tw1.ru t9
  • A spectacular summer cash offer is underway cb815908.tw1.ru u3
  • Important Money you didn't expect is here ck207265.tw1.ru Wz
  • Access Cryptocurrency Incentives cb815908.tw1.ru GJ
未经允许不得转载: LIFE & SHARE - 王颜公子 » sed - 文本分析与转换工具 (3) 实战

分享

作者

作者头像

Madman

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

0 条评论

暂时还没有评论.

专题系列