环境:CentOS
鉴于语句描述苍白无力,用例子直接说明。
mytxt文件内容:
zilzhang 19881110 jiangxi 18 film
zhagnsan 21321 sichuan 100 card
lisi 3435 hunan 65 TV
1. 找出文件第二行
$ sed -n ‘2p‘ mytxt
zhagnsan 21321 sichuan 100 card
2. 找出文件第1行到第三行
$ sed -n ‘1,3p‘ mytxt
zilzhang 19881110 jiangxi 18 film
zhagnsan 21321 sichuan 100 card
lisi 3435 hunan 65 TV
3. 打印含sichu的行
sed -n ‘/sichu/p‘ mytxt
zhagnsan 21321 sichuan 100 card
3. 打印最后1行
$ sed -n ‘$‘p mytxt
lisi 3435 hunan 65 TV
4. 打印所有行
$ sed -n ‘1,$‘p mytxt
zilzhang 19881110 jiangxi 18 film
zhagnsan 21321 sichuan 100 card
lisi 3435 hunan 65 TV
5. 打印行号(自己多试试去掉-e,= ;加-n)
sed -e ‘/sichu/=‘ mytxt
zilzhang 19881110 jiangxi 18 film
2
zhagnsan 21321 sichuan 100 card
lisi 3435 hunan 65 TV
6. 删除第二行
$ sed ‘2d‘ mytxt
zilzhang 19881110 jiangxi 18 film
lisi 3435 hunan 65 TV
7. 删除包含hunan的行
$ sed ‘/hunan/d‘ mytxt
zilzhang 19881110 jiangxi 18 film
zhagnsan 21321 sichuan 100 card
8. 替换文本
$ sed ‘s/hunan/xinjiang/g‘ mytxt
zilzhang 19881110 jiangxi 18 film
zhagnsan 21321 sichuan 100 card
lisi 3435 xinjiang 65 TV
原文:http://blog.csdn.net/zlcd1988/article/details/25338519