zar*_*ara 2 shell perl grep comm
我有15个不同的文件,我想要一个新文件,其中只包含所有文件中的公共行.例如:
File1:
id1
id2
id3
file2:
id2
id3
id4
file3:
id10
id2
id3
file4
id100
id45
id3
id2
I need the output be like:
newfile:
id2
id3
Run Code Online (Sandbox Code Playgroud)
我知道这个命令适用于每对文件:
grep -w -f file1 file2>输出
但我需要一个命令来工作超过2个文件.
有什么建议吗?
Perl救援:
perl -lne 'BEGIN { $count = @ARGV }
$h{$_}{$ARGV} = 1;
}{
print $_ for grep $count == keys %{ $h{$_} }, keys %h
' file* > newfile
Run Code Online (Sandbox Code Playgroud)
-n 逐行读取输入文件-l 添加换行符 print@ARGV数组包含输入文件名,将其分配给$count在BEGIN宁只是他们计数$ARGV 包含当前输入文件的名称$_ 包含从文件中读取的当前行.%h哈希值包含ID作为键,每个键包含文件名包含ID作为密钥的散列引用}{ 是"Eskimo greeting"运算符,它引入了输入耗尽后运行的代码