仅打印所有输入文件中存在的行

问题描述 投票:2回答:4

仅打印所有四个给定输入文件中存在的行。从下面显示的输入文件中只有/ dev / dev_sg2和/ dev / dev_sg3存在于所有输入文件中

$ cat file1
/dev/dev_sg1
/dev/dev_sg2
/dev/dev_sg3
/dev/dev_sg4

$ cat file2
/dev/dev_sg8
/dev/dev_sg2
/dev/dev_sg3
/dev/dev_sg6

$ cat file3
/dev/dev_sg5
/dev/dev_sg2
/dev/dev_sg3
/dev/dev_sg6

$ cat file4
/dev/dev_sg2
/dev/dev_sg3
/dev/dev_sg1
/dev/dev_sg4

试过的工具: -

cat file* | sort |uniq -c

      1 /dev/dev_sg1
      4 /dev/dev_sg2
      4 /dev/dev_sg3
      1 /dev/dev_sg4
      1 /dev/dev_sg5
      2 /dev/dev_sg6
      1 /dev/dev_sg8
sorting awk sed grep uniq
4个回答
0
投票

以下awk代码可能会帮助你。

awk 'FNR==NR{a[$0];next} ($0 in a){++c[$0]} END{for(i in c){if(c[i]==3){print i,c[i]+1}}}' Input_file1 Input_file2 Input_file3 Input_file4

输出如下。

/dev/dev_sg2 4
/dev/dev_sg3 4

编辑:如果您不想拥有行的计数,只想打印所有4个Input_files中的行,那么下面将执行操作:

awk 'FNR==NR{a[$0];next} ($0 in a){++c[$0]} END{for(i in c){if(c[i]==3){print i}}}'  Input_file1 Input_file2 Input_file3 Input_file4

EDIT2:现在也为代码添加解释。

awk '
FNR==NR{ ##FNR==NR condition will be TRUE when very first Input_file here Input_file1 is being read.
 a[$0];  ##creating an array named a whose index is current line $0.
 next    ##next is awk out of the box keyword which will avoid the cursor to go forward and will skip all next statements.
}
($0 in a){ ##These statements will be executed when awk complete reading the first Input_file named Input_file1 name here. Checking here is $0 is in array a.
 ++c[$0]   ##If above condition is TRUE then make an increment in array named c value whose index is current line.
}
END{       ##Starting END block of awk code here.
for(i in c){##Initiating a for loop here by which we will iterate in array c.
 if(c[i]==3){ ##checking condition here if array c value is equal to 3, which means it appeared in all 4 Input_file(s).
   print i    ##if, yes then printing the value of i which is actually having the line which is appearing in all 4 Input_file(s).
}
}}
' Input_file1 Input_file2 Input_file3 Input_file4 ##Mentioning all the 4 Input_file(s) here.

1
投票

使用comm管道:

comm -12 <(sort file1) <(sort file2) | comm -12 - <(sort file3) | comm -12 - <(sort file4)
  • -12 - 抑制两个输入文件唯一的行,仅打印公共行

输出:

/dev/dev_sg2
/dev/dev_sg3

0
投票

如果您事先知道不会有超过4个输入文件,您只需在现有解决方案的末尾添加grep,如下所示:

cat file* | sort |uniq -c | egrep '^4'

这将仅显示在行首处具有最多(4)个计数的行。

如果您需要它来处理任意数量的文件,则需要更好的解决方案。


0
投票

如果订单不需要维护

$ j() { join <(sort $1) <(sort $2); }; j <(j file1 file2) <(j file3 file4)

/dev/dev_sg2
/dev/dev_sg3
© www.soinside.com 2019 - 2024. All rights reserved.