2015-01-14 84 views
-2

我正在嘗試學習sed/awk,並計劃在以下任務中使用它。我有打印出的文件類似下面的列表(可能不止一個,每行)的命令:sed/awk採取第一部分'字'

--- /section/1 --- 
appname1/detail1/something appname1/detail2/somethingelse another/app/2.0 
sillyapp/details/here bug/2.5 
--- /section2/details/here --- 
apname2/3.2.5 apname2/3.2.6 apname3/something.0.4/here 

,我希望做兩件事情:

(1)用sed取只有文件的第一部分(從'到「/」),這樣我們就擁有

--- /section/1 --- 
appname1 appname1 another 
sillyapp bug 
--- /section2/details/here --- 
apname2 apname2 apname3 

(2)用awk(我想?)來找出每個應用程序多少次上市所以我們可以有

appname1: 2 
another: 1 
sillyapp: 1 
bug: 1 
apname2: 2 
apname3: 1 

sed/awk可以用於這個嗎?如果是這樣,有人可以給出詳細的指示,說明如何完成每一個(爲什麼它有效)?

回答

4

我會使用grep與-o只提取火柴,-P得到Perl兼容的正則表達式:

grep -Po '(^|\s)\K\w+(?=/)' file | sort | uniq -c 
1 another 
    2 apname2 
    1 apname3 
    2 appname1 
    1 bug 
    1 sillyapp 

這正則表達式是:

(^|\s) # either the beginning of the line, or a space 
\K  # forget about what came before (i.e. don't remember the space) 
\w+  # some word characters 
(?=/) # the next character is a slash (look-ahead) 

sed:我不是ag烏魯,但我想出了這個:

sed -nr '/^---/d; s/(^| +)([^/]+)[^ ]+/\2 /g; H; ${x;s/\n//g;s/ $//; s/ /\n/g;p}' file 
appname1 
appname1 
another 
sillyapp 
bug 
apname2 
apname2 
apname3 

也就是說

sed -nr '   # -n suppress printing; -r enable extended regular expressions 
    /^---/d      # delete "header" lines 
    s/(^| +)([^/]+)[^ ]+/\2 /g # extract the words you want, add a trailing space 
    H       # append this transformed line to the hold space 
    ${       # on the last line of input: 
     g      # bring the hold space contents into the pattern space 
     s/\n//g     # remove newlines 
     s/ $//     # remove a trailing space 
     s/ /\n/g     # change spaces into newlines 
     p      # and, finally, print the results 
    } 
' file 

在此之後,添加| sort | uniq -c作爲

+1

上面這是一個很好的SED資源:HTTPS:/ /www.gnu.org/software/sed/manual/sed.html –

+0

這裏是你可以添加的gawk方式(如果你想:))'gawk -vRS =「+ | \ n +」-F「/」'{ $ 1〜/ ^($ | - )!/ &&一個[1 $] ++} END {對於(ⅰ在a)印刷我,a [i]}'' –

相關問題