0

我需要像哈希一样在文件中搜索。想知道 awk 是否是正确的解决方案

输入示例:

bla   123.123.123.0
# This line should be a comment
ble      www.ble.com
bli  <random whitespace> ::1
blo   anything

根据上下文,我需要两个不同的输出:

获取密钥:

bla ble bli blo

并搜索(ble)

www.ble.com

我希望这是微不足道的awk

编辑:改进了输入格式的描述

4

3 回答 3

1

像这样的一种方式(要求不是很清楚):

$ awk '!/^#/{a[$1]=$2}END{print a[x];}' x='ble' file
www.ble.com
于 2013-01-18T12:28:18.230 回答
1

获取密钥:

kent$  grep -oP "^\w+(?= )" file
bla
ble
bli
blo

如果您希望它们与 awk 放在一行中

kent$  awk 'NR!=2{printf $1" "}' test
bla ble bli blo 
于 2013-01-18T12:28:45.547 回答
0

不是很清楚格式...首先这个怎么样?

$ awk '/^[^#]/{print $1}/www/{print $2}' input.txt
bla
ble
www.ble.com
bli
blo

或“两个不同的输出:

/^[^#]/ {
    k[$1]++
}
/www/ {
    w[$2]++
}

END {
    print "keys:"
    for (x in k)
        print "\t"x
    print "urls:"
    for (x in w)
        print "\t"x

}

输出:

$ awk -f keys.awk input.txt
keys:
    bla
    ble
    bli
    blo
urls:
    www.ble.com
于 2013-01-18T12:27:24.213 回答