原帖由 corvus 於 2009-1-2 23:56 發表 
又awk又sed
不如重寫只用其中一種。
因為懶學, 所以一路都淨係用 awk 揀 text 入面o既 components 同用 sed 做 substitution 俾你講一講, 又走去望吓, search awk oneliners 同 sed oneliners 勁多 examples, 又學到o野 sed 搭 regex 係勁, 但真係難明 反而 awk 話晒都係一個 language 易上手好多
用 sed:- ls -l /etc/rc2.d/ | sed -e '/init.d/!d' -e 's/.* ..\/init.d\///'
複製代碼 '/init.d/!d' 先將有 init.d o既 lines 唔好 delete, 其他全部 delete
's/.* ..\/init.d\///' 然後用 regex 將 "空格../init.d/" replace 做冇o野, 因為 greedy match o既關係 ".*空格" 會 match 最後一個空格
用 awk:- ls -l /etc/rc2.d/ | awk ' { sub(/..\/init.d\//,""); } $10!~/^$/ { print $10 } '
複製代碼 sub(/..\/init.d\//,"") 先有理冇理將全部 ../init.d/ replace 做冇o野先
$10!~/^$/ 如果第十個 component 唔係空白的話
{ print $10 } 寫返第十個 component 出黎
只可以話呢 D unix utilities 太博大精深, 如果可以精到其中一件都好好囉 
[ 本帖最後由 foobar 於 2009-1-3 08:32 編輯 ] |