【问题标题】:Shell script to find increase in occurance count between two files用于查找两个文件之间出现次数增加的 Shell 脚本
【发布时间】:2020-04-08 21:20:03
【问题描述】:

文件1.log

2000 apple
2333 cat
5343 dog
1500 lion

文件2.log

2500 apple
2333 cat
1700 lion

需要一个shell脚本来输出如下:

500 apple
200 lion

尝试了很多解决方案,但没有任何解决方案,因为我同时拥有文本和字符串。有人可以帮忙吗。谢谢

编辑(由 RavinderSingh13 提供):将 OP 在 cmets 中显示的 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"

【问题讨论】:

  • 名称是否唯一?
  • 您还提到了Have tried lot of solution but nothing worked out as I'm having both text and string,所以请务必在代码标签中的问题中添加所有这些努力,然后也让我们知道。
  • 是的,两个文件的名称都是唯一的
  • @user2170023,我的答案已经准备好,但正在等待您为您的问题添加您的努力,所以请这样做。
  • @user2170023,感谢您的努力,cmets 并非用于展示代码示例,请将它们添加到您的问题中,并要求您编辑您的问题并让我们知道。

标签: linux bash shell awk sh


【解决方案1】:

请您尝试关注一下。

awk 'FNR==NR{a[$2]=$1;next} $2 in a && ($1-a[$2])>0{print $1-a[$2],$2}' file1 file2

添加上述解决方案的非单行形式:

awk '
FNR==NR{
  a[$2]=$1
  next
}
($2 in a) && ($1-a[$2])>0{
  print $1-a[$2],$2
}
'  Input_file1  Input_file2

说明:在此处添加对上述解决方案的详细说明。

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.

【讨论】:

    【解决方案2】:
    awk '{if (!($2 in entry)) { entry[$2]=$1 } else { delta=$1-entry[$2]; if (delta!=0) {print delta,$2} } }' FILE_1 FILE2
    

    您也可以将其放入文件中,例如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
        } 
      } 
    }
    

    通过awk -f delta.awk FILE_1.txt FILE_2.txt调用。

    【讨论】:

    • 非常感谢 - 像魅力一样工作,但我有点即兴发挥
    • @user2170023 你说的I'm bit improvising是什么意思?
    • s[$2]0 时,if(!s[$2]) 将失败。 ITYMif(!($2 in s))。包括换行符和缩进在内的一些空白将大大提高脚本的可读性。
    • 将添加空格和缩进。对不起。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 2021-10-07
    相关资源
    最近更新 更多