2012-03-27 41 views
2

我有一个文件与我的线,我想比较一个接一个的线,以测试它们是否相同,除了最后一列。例如:测试两条相邻的线

example/example 321 
example/example 456 
otherexample/otherexample 321 

在这种情况下,我想程序简单地返回:

example/example 

随着第一列相匹配,但第二列是不同的。使用Unix工具做这件事的最好方法是什么?到目前为止,我已经尝试过awk,但收效甚微。非常感谢。

回答

2
# sample data 
$ cat input.txt 
example/example 321 
example/example 456 
example/example 789 
otherexample/otherexample 321 
abc 
otherexample/otherexample 321 

$ awk 'x==$1{print $1; while(getline){if(x!=$1)break}}{x=$1}' input.txt 
example/example 
+0

非常感谢!奇迹般有效。虽然,我对awk很陌生,但我不确定是否完全理解此代码的内部工作原理,所以我有几个问题: 1.'=='和'='之间的区别是什么? ' 标志。我知道在许多语言中,一个变量设置为一个变量,另一个测试是相同的,这与awk是一样的吗? 2.为什么打印$ 1;在while循环之前而不是在最后? 非常感谢! – samturner 2012-03-27 09:46:38

+0

'=='与'C++'中的含义相同。你可以在'while ... loop'之后加上'print $ 1'。 – kev 2012-03-27 10:05:11

0

方式一:

内容 script.awk

内容 infile
## In first line, get path and init counter of consecutive paths. 
FNR == 1 { 
    path = $1 
    repeats = 1 
    next 
} 

FNR > 1 { 
    ## If current path is same as previous one, increment counter. 
    if (path == $1) { 
     ++repeats; 
    } 
    ## Else, there is new path, so print previous and init counter. 
    else { 
     print_repeated_path(repeats, path) 
     path = $1 
     repeats = 0 
    } 
} 

END { 
    print_repeated_path(repeats, path) 
} 

function print_repeated_path(r, path) { 
    if (r > 1) { 
     printf "%s\n", path 
    } 
} 

example/example 321 
example/example 456 
otherexample/otherexample 321 
other/example 456 
other/example 678 
other/example 123 
otherexample/otherexample 321 

运行它想:

awk -f script.awk infile 

有了结果如下:

example/example 
other/example 
0
sort -u temp.txt|awk '{a[$1]++}END{for (i in a){if(a[i]>1)print i;}}' 
0

这可能会为你工作:

cut -d' ' -f1 file | sort | uniq -d 

或本:

sort file | sed '$!N;/^\(\S*\) .*\n\1.*/!D;bb;:a;$!N;:b;s//\1/;ta;P;D'