0

鉴于这种foo.txt

/bin/栏:
        内核基础 => /sbin/内核基础 (0x77670000)
        intl-8 => /usr/bin/intl-8 (0x6f970000)
        iconv-2 => /usr/bin/iconv-2 (0x6f980000)
        pcre-0 => /usr/bin/pcre-0 (0x6f780000)
        gcc_s-1 => /usr/bin/gcc_s-1 (0x6fdd0000)
/斌/巴兹:
        内核基础 => /sbin/内核基础 (0x77670000)
        intl-8 => /usr/bin/intl-8 (0x6f970000)
        iconv-2 => /usr/bin/iconv-2 (0x6f980000)

我想要这个输出

/usr/bin/intl-8
/usr/bin/iconv-2
/usr/bin/pcre-0
/usr/bin/gcc_s-1

也就是说,我想取包含 的唯一行/usr,然后打印正确的字段。目前我正在使用管道

grep /usr foo.txt | sort -u | cut -d' ' -f3

但是,这可以通过单行awk命令来完成吗?我 遇到了 这个例子

awk '!a[$0]++'

但我看不到如何只用/usr线条来使用它。

4

2 回答 2

2
 awk '/\usr/ && ! a[$0]++{print $3}' foo.txt
于 2013-05-01T07:01:54.317 回答
0

The pattern matching in awk is one of its great advantages. Your example uses some shortcuts for simple cases, but an equivalent command to awk '!a[$0]++' is awk '($0 in a) {a[$0]; print}', which leads quite easily to adding an additional pattern. Also printing only the third field yields:

awk '/\/usr/ && !($0 in a) {a[$0]; print $3}'
于 2013-05-01T06:15:25.543 回答