輸出第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)
-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)
-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)
-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)
/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)
/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)
-/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 -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 '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)
/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)
/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 '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 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)
/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
刪除最后一行
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
/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.
/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)