如何根据bash中列的变量解析输入

时间:2012-08-29 06:50:36

标签: bash parsing sed awk

我需要帮助来解析这样的输入。

192.168.0.168: 1
192.168.0.158: 0
192.168.0.198: 0
192.168.0.148: 0
192.168.0.158: 1
192.168.0.168: 0

如果任何ip的第二列中有1,我想删除第二列中有0且第一列中有相同ip的行。所以我的输出应该是这样的。

192.168.0.168: 1
192.168.0.198: 0
192.168.0.148: 0
192.168.0.158: 1

我想这可以通过使用awk,sed等来完成,但我不知道该怎么做。我希望我能正确解释我的问题。谢谢......

6 个答案:

答案 0 :(得分:2)

一种方式:

awk '
    { 
        ips[ $1 ] = ( ips[ $1 ] == 1 ) ? 1 : $2 
    } 
    END { 
        for ( ip in ips ) { 
            print ip, ips[ ip ] 
        } 
    }
' infile

产量(产出可能无序):

192.168.0.168: 1
192.168.0.198: 0
192.168.0.148: 0
192.168.0.158: 1

答案 1 :(得分:2)

这可能适合你(GNU sed):

cat -n file | 
sort -k2,2 -k3,3nr | 
sed ':a;$!N;/^\s*\S*\s*\(\S*\)\s*1\s*\n.*\1/s/\n.*0\s*//;ta;P;D' | 
sort -n | 
sed 's/^\s*\S*\s*//'

答案 2 :(得分:1)

Perl解决方案:

perl -nae '$h{ $F[0] } += $F[1]
           }{
           print "$k ", $v ? 1 : 0, "\n" while ($k, $v) = each %h'

答案 3 :(得分:1)

有几个sort应该这样做:

sort file -r | sort -u -k1,1

前一种排序可以确保这些行是有序的,这样第二列上的1行将成为每个IP的第一行。

后一种类型只保留每个IP的第一个条目:-u - >唯一,-k1,1 - >仅限第一栏。

答案 4 :(得分:1)

awk '{a[$1]+=$2}END{for(i in a)print i,a[i]}' your_file

答案 5 :(得分:1)

功能方法(haskell编程语言):

-- function that having the two sublists with '0' and '1' ips,
-- filters and puts into   the '1' 
-- sublist all the '0' ips that are not included in '1'

fil [] result = result
fil (x: xs) result | (init x `elem` (map init result)) == False = fil xs (x:result)
            | otherwise = fil xs result


-- function that filters '0' and '1' sublists
getsublist alist character = filter (\x-> (last x) == character) alist



> let a = ["192.168.0.168: 1", "192.168.0.158: 0", "192.168.0.198: 0", "192.168.0.148: 0", "192.168.0.158: 1", "192.168.0.168: 0"]

> let b = getsublist a '0'

> let c = getsublist a '1'

> fil b c

<强>输出:

["192.168.0.148: 0","192.168.0.198: 0","192.168.0.168: 1","192.168.0.158: 1"]