[英]Shell script to find increase in occurance count between two files
File1.log文件1.log
2000 apple
2333 cat
5343 dog
1500 lion
File2.log文件2.log
2500 apple
2333 cat
1700 lion
Need a shell script to output as below:需要一个shell脚本来输出如下:
500 apple
200 lion
Have tried lot of solution but nothing worked out as I'm having both text and string.尝试了很多解决方案,但没有任何效果,因为我同时拥有文本和字符串。 Could someone help on this.有人可以帮忙解决这个问题。 Thanks谢谢
EDIT(by RavinderSingh13): Added OP's efforts which OP had shown in comments to in post:编辑(由 RavinderSingh13 提供):添加了 OP 在评论中显示的 OP 努力:
#!/bin/bash
input1="./File1.log"
input2="./File2.log"
while IFS= read -r line2
do
while IFS=read -r line1
do
echo "$line1"
done < "$input1"
echo "$line2"
done < "$input2"
Could you please try following.你能不能试试以下。
awk 'FNR==NR{a[$2]=$1;next} $2 in a && ($1-a[$2])>0{print $1-a[$2],$2}' file1 file2
Adding a non-one liner form of above solution:添加上述解决方案的非单衬形式:
awk '
FNR==NR{
a[$2]=$1
next
}
($2 in a) && ($1-a[$2])>0{
print $1-a[$2],$2
}
' Input_file1 Input_file2
Explanation: Adding a detailed explanation for above solution here.说明:在此处添加对上述解决方案的详细说明。
awk ' ##Starting awk program from here.
FNR==NR{ ##Checking condition if FNR==NR which will be TRUE once file1 is being read then do following.
a[$2]=$1 ##Creating an array a whose index is $2 and value is $1 of current line.
next ##Using next function of awk, to skip all further lines from here.
} ##Closing condition BLOCK for FNR==NR here.
($2 in a) && ($1-a[$2])>0{ ##Checking condition if $2 is present in array a AND difference of $1 and array a with index $2 is greater than 0 then do following.
print $1-a[$2],$2 ##Printing difference between $1 and array a with index $2 along with current $2 here.
} ##Closing BLOCK for above condition here.
' file1 file2 ##Mentioning Input_file names here.
awk '{if (!($2 in entry)) { entry[$2]=$1 } else { delta=$1-entry[$2]; if (delta!=0) {print delta,$2} } }' FILE_1 FILE2
You can also put this into a file, eg delta.awk
:您也可以将其放入文件中,例如delta.awk
:
{
if (!($2 in entry)) {
entry[$2]=$1
} else {
delta=$1-entry[$2]
if (delta !=0) { # Only output lines of non-zero increment/decrement
print delta,$2
}
}
}
Invoke via awk -f delta.awk FILE_1.txt FILE_2.txt
.通过awk -f delta.awk FILE_1.txt FILE_2.txt
调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.