- 論壇徽章:
- 0
|
本帖最后由 pianist_cu 于 2012-03-02 09:19 編輯
SED實例
作者 :pianist
Email:xiaopei0206@gmail.com
日期 :2009-07-28
轉載請注明作者。
sed 編輯器是 Linux 系統(tǒng)管理員的工具包中最有用的資產(chǎn)之一,因此,有必要徹底地了解其應用。不知道什么是sed的同學,請先搞明白sed的基本作用再來做這個題集。
根據(jù)學習曲線,看一遍,只能記住 10%,實際操作后能記住80%,所以,如果你想熟練掌握sed,最好打開一個窗口,跟著我一起做下面的練習。
“We Learn . . .
10% of what we read
20% of what we hear
30% of what we see
50% of what we see and hear
70% of what we discuss
80% of what we experience
95% of what we teach others.”— William Glasser
ubuntu@ubuntu:~$ cat > dat # 往dat文件里輸入以下數(shù)據(jù)
1 dat # 以后的大部分例子,都將使用dat文件作為處理對象
2 Desktop
3 dir1
4 dir2
5 Documents
6 Music
7 Pictures
8 Public
9 Templates
10 Videos
Ctrl+D 結束輸入
sed指令由兩部分組成,address和function,也就是說,對符合address的行執(zhí)行function。
下面介紹幾種address的形式:
[ubuntu@bourne ~]$ sed '6p' dat # p(print), 打印第6行,
1 dat #6是address,p是function,下同
2 Desktop
3 dir1
4 dir2
5 Documents
6 Music #sed默認是自動輸出的
6 Music #這一行是由命令p輸出的
7 Pictures
8 Public
9 Templates
10 Videos
ubuntu@ubuntu:~$ sed -n '6p' dat #n(quiet), 關閉自動輸出; 打印第6行
6 Music #通常,命令n和p結合使用,否則沒有任何輸出
ubuntu@ubuntu:~$ sed -n '$p' dat #$,表示最后一行,加上p,表示打印最后一行
10 Videos
ubuntu@ubuntu:~$ sed -n '/dir/p' dat # 打印包含“dir”的行
3 dir1
4 dir2
ubuntu@ubuntu:~$ sed -n '1,4p' dat # 打印1到4行
1 dat
2 Desktop
3 dir1
4 dir2
ubuntu@ubuntu:~$ sed -n '2, /Doc/p' dat # 打印第2行,到包含”Doc”之間的行
2 Desktop
3 dir1
4 dir2
5 Documents
ubuntu@ubuntu:~$ sed -n '/Doc/, /Pic/p' dat # 打印包含”Doc”,到包含”Pic”之間的行
5 Documents
6 Music
7 Pictures
ubuntu@ubuntu:~$ sed -n '/Doc/, 9p' dat # 打印包含”Doc”,到第9行之間的行
5 Documents
6 Music
7 Pictures
8 Public
9 Templates
ubuntu@ubuntu:~$ sed -n '2,+5p' dat # 打印第2行,及其后面的5行
2 Desktop
3 dir1
4 dir2
5 Documents
6 Music
7 Pictures
ubuntu@ubuntu:~$ sed -n '/Doc/,+3p' dat # 打印包含”Doc”的行,及其后面的三行
5 Documents
6 Music
7 Pictures
8 Public
ubuntu@ubuntu:~$ sed -n '1~2p' dat # 打印從第1行開始,行號依次遞增2的行
1 dat # 即1,3,5,7,9行
3 dir1
5 Documents
7 Pictures
9 Templates
ubuntu@ubuntu:~$ sed -n '1~3p' dat # 打印從第1行開始,行號依次遞增3的行
1 dat
4 dir2
7 Pictures
10 Videos
ubuntu@ubuntu:~$ sed -n '2~3p' dat #打印從第2行開始,行號依次遞增3的行
2 Desktop
5 Documents
8 Public
ubuntu@ubuntu:~$ sed '1,3d' dat #刪除1-3行
4 dir2
5 Documents
6 Music
7 Pictures
8 Public
9 Templates
10 Videos
ubuntu@ubuntu:~$ sed -e '1,3d' dat #e(expression),在命令行上直接使用sed命令
4 dir2 #當只有一個表達式(expression)時,命令e可以省略
5 Documents #我們之前的例子都沒有使用e
6 Music
7 Pictures
8 Public
9 Templates
10 Videos
ubuntu@ubuntu:~$ sed -n -e '1p' -e '/Doc/p' dat #有多個表達式時,e不可省略,
1 dat #每個表達式前都要加-e
5 Documents
[ubuntu@bourne ~]$ echo 1p > cmd #將上面兩個表達式輸入到一個文件cmd中
[ubuntu@bourne ~]$ echo "/Doc/p" >> cmd
[ubuntu@bourne ~]$ cat cmd
1p
/Doc/p
[ubuntu@bourne ~]$ sed -n -f cmd dat #f (script-file),將命令組成的腳本放在f參數(shù)后執(zhí)行
1 dat #在需要較多較長命令時很有效,便于編輯命令
5 Documents
ubuntu@ubuntu:~$ cat dat
1 dat
2 Desktop
3 dir1
4 dir2
5 Documents
6 Music
7 Pictures
8 Public
9 Templates
10 Videos
ubuntu@ubuntu:~$ cp dat dat.bk
ubuntu@ubuntu:~$ sed -i '1,3d' dat #上面所有例子的輸出都不會對源文件dat造成改變,
ubuntu@ubuntu:~$ cat dat #加入i(in-place)參數(shù)后,將直接對原文件進行編輯,
4 dir2 #因此編輯之前,最好備份原文件
5 Documents
6 Music
7 Pictures
8 Public
9 Templates
10 Videos
ubuntu@ubuntu:~$ cp dat.bk dat #恢復操作數(shù)據(jù),我們繼續(xù)
ubuntu@ubuntu:~$ sed -n '/dir1/=' dat #=打印dir1的行號,只有行號,不包含這一行的內容
3
ubuntu@ubuntu:~$ sed -n '/dir1/=; /dir1/p' dat #打印dir1的行號,和這一行的內容
3
3 dir1
ubuntu@ubuntu:~$ sed '/Document/a\oooooooooooooo' dat #a(append text),
1 dat #在包含Document的行后添加字符串
2 Desktop #有些時候可以直接用”a”,
3 dir1 #而不用包含后面的'\'
4 dir2
5 Documents
oooooooooooooo
6 Music
7 Pictures
8 Public
9 Templates
10 Videos
ubuntu@ubuntu:~$ sed '/Document/i\oooooooooooooo' dat #i(insert text), 在包含
1 dat #Document的行前插入字符串
2 Desktop
3 dir1
4 dir2
oooooooooooooo
5 Documents
6 Music
7 Pictures
8 Public
9 Templates
10 Videos
ubuntu@ubuntu:~$ sed '/Document/c\oooooooooooooo' dat #c,用字符串替換包含
1 dat #“Document”的行
2 Desktop
3 dir1
4 dir2
oooooooooooooo
6 Music
7 Pictures
8 Public
9 Templates
10 Videos
進行下面的操作之前,先了解一下sed基本的工作原理。
Sed以行為單位對文件進行處理,sed從待處理的文件中讀取一行數(shù)據(jù)存入模式空間(pattern space,是一段內存),對它執(zhí)行-e或-f參數(shù)中賦予的命令(如果這一行數(shù)據(jù)符合address的描述,如包含dir,就執(zhí)行function),輸出模式空間的內容,然后清空模式空間;讀取下一行數(shù)據(jù)存入模式空間,再執(zhí)行-e或-f參數(shù)中的命令....直到文件結束。
ubuntu@ubuntu:~$ sed '/Pictures/q' dat #q(quit), 執(zhí)行到包含Pictures的行時,
1 dat #打印當前模式空間的內容,再退出sed
2 Desktop #在這個例子中,“當前模式空間”中存儲的就是
3 dir1 #“7 Pictures”
4 dir2
5 Documents
6 Music
7 Pictures
ubuntu@ubuntu:~$ sed '/Pictures/Q' dat #Q(QUIT),執(zhí)行到包含Pictures的行時,
1 dat #不打印當前模式空間的內容,退出sed
2 Desktop
3 dir1
4 dir2
5 Documents
6 Music
ubuntu@ubuntu:~$ cat a
@@@@@@@@ hello
@@@@@@@@ world
ubuntu@ubuntu:~$ sed '/Public/r a' dat #r(read file),執(zhí)行到包含Public的行時,
1 dat #讀入文件a的內容
2 Desktop
3 dir1
4 dir2
5 Documents
6 Music
7 Pictures
8 Public
@@@@@@@@ hello
@@@@@@@@ world
9 Templates
10 Videos |
|