linux shell获取多个文件交集

b5lpy0ml  于 2022-11-02  发布在  Linux
关注(0)|答案(1)|浏览(204)

我有几个txt文件示例1.txt 2.txt 3.txt 4.txt我想得到1.txt 2.txt 3.txt 4.txt内容交集

cat 1.txt 2.txt | sort | uniq -c > tmp.txt
cat tmp.txt 3.txt | sort | uniq -c > tmp2.txt 
and so on ....

有没有更好的办法?

input text
1.txt
1
2
3
4

2.txt
1
2
3

3.txt
1
2

4.txt
1
5

expected output:
1
nc1teljy

nc1teljy1#

使用您显示的示例,请尝试以下awk代码。

*第一个解决方案:*这考虑到您可能在单个Input_file本身中具有重复的行值,则您可以尝试以下操作:

awk '
!arr2[FILENAME,$0]++{
  arr1[$0]++
}
END{
  for(i in arr1){
    if(arr1[i]==(ARGC-1)){
       print i
    }
  }
}
' *.txt

*第二种解决方案:*此解决方案假定Input_file中没有重复项如果是这种情况,请尝试以下操作:

awk '
{
  arr[$0]++
}
END{
  for(i in arr){
    if(arr[i]==(ARGC-1)){
       print i
    }
  }
}
' *.txt

***说明:***添加上述详细说明。

awk '                      ##Starting awk program from here.
{
  arr[$0]++                ##Creating an array named arr with index of $0 and keep increasing its value.
}
END{                       ##Starting END block of this program from here.
  for(i in arr){           ##Traversing through array arr here.
    if(arr[i]==(ARGC-1)){  ##Checking condition if value of current item in arr is Equal to total number of files then print it.
       print i
    }
  }
}
' *.txt                    ##Passing all .txt files as an input to awk program from here.

相关问题