sed用法介绍

输出第1行到第3行
-bash-3.00$ sed -n '1,3p' sed.txt
liuzk423   pts/6        Jul 20 08:27    (219.245.104.240)
wangmin803   pts/16       Jul 29 14:18  (219.149.138.142)
majorchan   pts/21       Jul 29 15:18   (202.203.137.236)


输出第2行
-bash-3.00$ sed -n '2p' sed.txt
wangmin803   pts/16       Jul 29 14:18  (219.149.138.142)


查找含有tomotoboy的行让后输出
-bash-3.00$ sed -n '/tomotoboy/'p sed.txt
tomotoboy   pts/45       Jul 29 13:53   (219.221.99.155)
tomotoboy   pts/46       Jul 29 15:24   (219.221.99.155)
tomotoboy   pts/52       Jul 29 16:20   (219.221.99.155)


输出当前的所有用户
/home/l/g/tomotoboy >who
liuzk423   pts/6        Jul 20 08:27    (219.245.104.240)
tomotoboy   pts/16       Aug  7 21:24   (219.221.98.71)
guise      pts/21       Aug  7 17:56    (124.76.10.207)
guise      pts/35       Aug  7 21:13    (58.41.162.27)
yagamil    pts/46       Aug  7 20:48    (199.40.206.191)


输出第一行到含有字符串tomotoboy的行
/home/l/g/tomotoboy >who | sed -n '1,/tomotoboy/'p
liuzk423   pts/6        Jul 20 08:27    (219.245.104.240)
tomotoboy   pts/16       Aug  7 21:24   (219.221.98.71)


输出原文的同时,输出tomotoboy所在的行号
-/home/l/g/tomotoboy >who|sed '/tomotoboy/'=
liuzk423   pts/6        Jul 20 08:27    (219.245.104.240)
2
tomotoboy   pts/16       Aug  7 21:24   (219.221.98.71)
guise      pts/21       Aug  7 17:56    (124.76.10.207)
guise      pts/35       Aug  7 21:13    (58.41.162.27)
yagamil    pts/46       Aug  7 20:48    (199.40.206.191)


输出含有tomotoboy的行的行号
/home/l/g/tomotoboy >who | sed -e '/tomotoboy/'=
liuzk423   pts/6        Jul 20 08:27    (219.245.104.240)
2
tomotoboy   pts/16       Aug  7 21:24   (219.221.98.71)
guise      pts/21       Aug  7 17:56    (124.76.10.207)
guise      pts/35       Aug  7 21:13    (58.41.162.27)
kindy      pts/38       Aug  7 21:33    (reverse.gdsz.cncnet.net)
yagamil    pts/46       Aug  7 20:48    (199.40.206.191)


输出第一行到最后一行
/home/l/g/tomotoboy >who | sed -n '1,$p'
liuzk423   pts/6        Jul 20 08:27    (219.245.104.240)
tomotoboy   pts/16       Aug  7 21:24   (219.221.98.71)
guise      pts/21       Aug  7 17:56    (124.76.10.207)
guise      pts/35       Aug  7 21:13    (58.41.162.27)
yagamil    pts/46       Aug  7 20:48    (199.40.206.191)


输出最后一行
/home/l/g/tomotoboy >who|sed -n '$p'
yagamil    pts/46       Aug  7 20:48    (199.40.206.191)


append.sed:sed脚本实现附加功能,将hello tomotoboy附件在tomotoboy所在行之后
/home/l/g/tomotoboy >cat append.sed
#!/bin/sed -f
/tomotoboy/ a\
hello tomotoboy

/home/l/g/tomotoboy >who| append.sed
liuzk423   pts/6        Jul 20 08:27    (219.245.104.240)
tomotoboy   pts/16       Aug  7 21:24   (219.221.98.71)
hello tomotoboy
guise      pts/21       Aug  7 17:56    (124.76.10.207)
guise      pts/35       Aug  7 21:13    (58.41.162.27)
kindy      pts/38       Aug  7 21:41    (reverse.gdsz.cncnet.net)
yagamil    pts/46       Aug  7 20:48    (199.40.206.191)


文本输出时,把tomotoboy替换成hello
/home/l/g/tomotoboy >who|sed 's/tomotoboy/hello/'
liuzk423   pts/6        Jul 20 08:27    (219.245.104.240)
hello   pts/16       Aug  7 21:24       (219.221.98.71)
guise      pts/21       Aug  7 17:56    (124.76.10.207)
guise      pts/35       Aug  7 21:13    (58.41.162.27)
yagamil    pts/46       Aug  7 20:48    (199.40.206.191)


只输出被替换的行
/home/l/g/tomotoboy >who|sed -n 's/tomotoboy/hello/p'
hello   pts/16       Aug  7 21:24       (219.221.98.71)


如果要附加或修改一个字符串,可以使用(&)命令,&命令保存发现模式以便重新调用它,然后把它放在替换字符串里面。
/home/l/g/tomotoboy >who|sed -n 's/tomotoboy/hello &/p'
hello tomotoboy   pts/16       Aug  7 21:24     (219.221.98.71)
/home/l/g/tomotoboy >who|sed -n 's/tomotoboy/& my friend/p'
tomotoboy my friend   pts/16       Aug  7 21:24 (219.221.98.71)


删除最后一行
bash-3.00$ sed '$d' sed.txt 
删除第一到最后6行
-bash-3.00$ sed '1,6d' sed.txt 
删除第一到最后一行
-bash-3.00$ sed '1,$d' sed.txt 
删除tomotoboy的行
-bash-3.00$ sed '/tomotoboy/d' sed.txt 
用seawolf替代tomotoboy
-bash-3.00$ sed 's/tomotoboy/seawolf/' sed.txt 
用seawolf替代tomotoboy然后输出到sed.out
-bash-3.00$ sed 's/tomotoboy/seawolf/w sed.out' sed.txt


看看我们写好了哪些sed脚本
/home/l/g/tomotoboy >ls -al|grep 'sed$'
-rwxr--r--   1 tomotoboy member        45 Aug  7 21:41 append.sed
-rwxr--r--   1 tomotoboy member        68 Jul 30 16:28 change.sed

看看change.sed的用途是什么?
/home/l/g/tomotoboy >cat change.sed
#!/bin/sed -f
#change.sed
/tomotoboy/ c\
I love China,Ilove Wanzhi.

找到tomotoboy,替换为I love China,Ilove Wanzhi.
/home/l/g/tomotoboy >who|change.sed
liuzk423   pts/6        Jul 20 08:27    (219.245.104.240)
I love China,Ilove Wanzhi.
guise      pts/21       Aug  7 17:56    (124.76.10.207)
guise      pts/35       Aug  7 21:13    (58.41.162.27)
uyty       pts/38       Aug  7 22:09    (p3213-ipbf803souka.saitama.ocn.ne.jp)
yagamil    pts/46       Aug  7 20:48    (199.40.206.191)


你可能感兴趣的:(.net,脚本,F#,bash)