我有一个包含很多行的文件。每一行都有8个分号分隔的列。
我如何(在Linux中)返回重复的行,而不是仅基于第2列?我应该使用grep还是其他什么?
发布于 2009-09-20 02:51:01
在awk脚本中查看我的注释
$ cat data.txt
John Thomas;jd;301
Julie Andrews;jand;109
Alex Tremble;atrem;415
John Tomas;jd;302
Alex Trebe;atrem;416
$ cat dup.awk
BEGIN { FS = ";" }
{
# Keep count of the fields in second column
count[$2]++;
# Save the line the first time we encounter a unique field
if (count[$2] == 1)
first[$2] = $0;
# If we encounter the field for the second time, print the
# previously saved line
if (count[$2] == 2)
print first[$2];
# From the second time onward. always print because the field is
# duplicated
if (count[$2] > 1)
print
}输出示例:
$ sort -t ';' -k 2 data.txt | awk -f dup.awk
John Thomas;jd;301
John Tomas;jd;302
Alex Tremble;atrem;415
Alex Trebe;atrem;416这是我的解决方案#2:
awk -F';' '{print $2}' data.txt |sort|uniq -d|grep -F -f - data.txt此解决方案的优点在于,它以同时使用许多工具(awk、sort、uniq和fgrep)为代价来保持行的顺序。
awk命令打印出第二个字段,然后对其输出进行排序。接下来,uniq -d命令挑选出重复的字符串。此时,标准输出包含重复的第二个字段的列表,每行一个。然后我们将该列表通过管道传输到fgrep中。'-f -‘标志告诉fgrep从标准输入中查找这些字符串。
是的,您可以使用命令行全力以赴。我更喜欢第二种解决方案,因为它使用了很多工具,逻辑也更清晰(至少对我来说是这样)。缺点是工具的数量和可能使用的内存。另外,第二种解决方案效率低下,因为它扫描数据文件两次:第一次使用awk命令,第二次使用fgrep命令。仅当输入文件较大时,此注意事项才重要。
发布于 2009-09-20 02:21:21
有一个复杂的awk脚本。
awk 'BEGIN { FS=";" } { c[$2]++; l[$2,c[$2]]=$0 } END { for (i in c) { if (c[i] > 1) for (j = 1; j <= c[i]; j++) print l[i,j] } }' file.txt它的工作方式是保留第二个字段中每个值的所有匹配项的计数器,以及具有该值的行,然后打印出计数器大于1的行。
将所有$2实例替换为所需的任何字段编号,并将末尾的file.txt替换为您的文件名。
发布于 2009-09-20 02:34:30
正如@mjv猜测的那样,awk (或者Perl,或者Python)是更好的选择:
awk -F';' ' {
if (assoc[$2]) { # This field 2 has been seen before
if (assoc[$2] != 1) { # The first occurrence has not been printed
print assoc[$2]; # Print first line with given $2
assoc[$2] = 1; # Reset array entry so we know we've printed it;
# a full line has 8 fields with semi-colons and
# cannot be confused with 1.
}
print $0; # Print this duplicate entry
}
else {
assoc[$2] = $0; # Record line in associative array, indexed by
# second field.
}
}' <<!
a;b;c;d;e;f;g;h
a;c;c;d;e;f;g;h
a;1;c;d;e;f;g;h
a;1;c;d;e;f;g;h
a;2;c;d;e;f;g;h
a;z;c;d;e;f;g;h
a;q;c;d;e;f;g;h
a;4;c;d;e;f;g;h
a;1;c;d;e;f;g;h
a;1;c;d;e;f;g;h
a;x;c;d;e;f;g;h
a;c;c;d;e;f;g;h
a;1;c;d;e;f;g;h
a;q;c;d;e;f;g;h
a;4;c;d;e;f;g;h
!这是可行的,但它可以稍微重新排序数据-因为它会在重复行的第二个出现时打印第一个出现的行。示例输出为:
a;1;c;d;e;f;g;h
a;1;c;d;e;f;g;h
a;1;c;d;e;f;g;h
a;1;c;d;e;f;g;h
a;c;c;d;e;f;g;h
a;c;c;d;e;f;g;h
a;1;c;d;e;f;g;h
a;q;c;d;e;f;g;h
a;q;c;d;e;f;g;h
a;4;c;d;e;f;g;h
a;4;c;d;e;f;g;hawk脚本的这个变体对测试进行了重新排序,使得表示法更加紧凑。它还显式地忽略不包含由分号分隔的8个字段的格式错误的数据行。它被打包为一个shell脚本,但没有任何选项处理,因此您只能提供要扫描的文件列表(如果没有列出任何文件,它将读取标准输入)。我删除了脚本中的Perl-ish分号;awk不需要它们。
#!/bin/sh
awk -F';' '
NF == 8 {
if (!assoc[$2]) assoc[$2] = $0
else if (assoc[$2] != 1)
{
print assoc[$2]
assoc[$2] = 1
print $0
}
else print $0
}' "$@"另外,@mjv评论说,如果输入很大,那么像这样的解决方案可能会出现内存问题,因为它保留了关联数组'assoc‘中每个不同的字段2值的记录。如果输入到awk的数据经过排序,我们可以消除这种情况,当然,使用sort可以确保这一点。下面是一个处理异常输入的变体脚本(因为如果需要保存中间结果,sort会将数据溢出到磁盘):
sort -t';' -k 2,2 "$@" |
awk -F';' '
BEGIN { last = ";"; line = "" }
NF == 8 {
if ($2 != last)
{
last = $2
line = $0
}
else if (line != "")
{
print line
line = ""
print $0
}
else print $0;
}'这只保留一行输入的副本。当然,样本数据的输出是按排序顺序给出的。
https://stackoverflow.com/questions/1450085
复制相似问题