sed / awk采取'单词的第一部分'

时间:2015-01-14 21:51:10

标签: awk sed

我正在尝试学习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吗?如果是这样,有人可以详细说明如何完成每个(任何原因)吗?

1 个答案:

答案 0 :(得分: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:我不是大师,但我想出了这个:

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