我有幾個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
uj5u.com熱心網友回復:
使用您顯示的示例,請嘗試以下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.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/520156.html
標籤:linux壳
上一篇:如何使用sh添加輸出引數
