如何比较两个文件并将所需内容输出到第三个文件

如何比较两个文件并将所需内容输出到第三个文件

我有 2 个输入文件。

文件1内容为( file1):

"BRGTEST-242" a.txt
"BRGTEST-240" a.txt
"BRGTEST-219" e.txt

文件2内容为( file2):

"BRGTEST-244" a.txt
"BRGTEST-244" b.txt
"BRGTEST-231" c.txt
"BRGTEST-231" d.txt
"BRGTEST-221" e.txt

我想得到最终输出为(file3):

BRGTEST-244 is depdendent on BRGTEST-242 for file a.txt    
BRGTEST-244 is depdendent on BRGTEST-240 for file a.txt    
BRGTEST-221 is dependent on BRGTEST-219 for the file e.txt 

如何编写shell脚本逻辑呢?我尝试使用awk但无法得到我想要的结果。

答案1

使用支持多维数组的 GNU Awk (gawk),逻辑似乎是:

gawk '
  NR==FNR {
    a[$2][++c[$2]] = $1; next
  } 
  $2 in a {
    for (i in a[$2]) 
      print $1 " is dependent on " a[$2][i] " for " $2
  }
' File1 File2
"BRGTEST-244" is dependent on "BRGTEST-242" for a.txt
"BRGTEST-244" is dependent on "BRGTEST-240" for a.txt
"BRGTEST-221" is dependent on "BRGTEST-219" for e.txt

如果您还需要删除引号:

gawk '
  NR==FNR {
    a[$2][++c[$2]] = substr($1,2,length($1)-2); next
  } 
  $2 in a {
    for (i in a[$2]) 
      print substr($1,2,length($1)-2) " is dependent on " a[$2][i] " for " $2
  }
' File1 File2
BRGTEST-244 is dependent on BRGTEST-242 for a.txt
BRGTEST-244 is dependent on BRGTEST-240 for a.txt
BRGTEST-221 is dependent on BRGTEST-219 for e.txt

答案2

这就是你想要的:

#!/bin/bash

while read line1; do
  f21=$(echo $line1 | cut -d" " -f1)
  f22=$(echo $line1 | cut -d" " -f2)
  #echo "outside: $f21 -- $f22"

  while read line2; do
    f11=$(echo $line2 | cut -d" " -f1)
    f12=$(echo $line2 | cut -d" " -f2)
    #echo "inside: $f11 -- $f12"

    if [ "$f22" == "$f12" ]; then
      echo "$f21 is dependent on $f11 for file $f22"
    fi

  done < file1
done < file2

例子

./parsy.bash
"BRGTEST-244" is dependent on "BRGTEST-242" for file a.txt
"BRGTEST-244" is dependent on "BRGTEST-240" for file a.txt
"BRGTEST-221" is dependent on "BRGTEST-219" for file e.txt

怎么运行的

我在此脚本中留下了 2 个echo ...命令,您可以取消注释以直观地查看该脚本如何遍历其他 2 个文件。当您取消注释它们时,您将看到以下输出:

$ ./parsy.bash | head -10
outside: "BRGTEST-244" -- a.txt
inside: "BRGTEST-242" -- a.txt
"BRGTEST-244" is dependent on "BRGTEST-242" for file a.txt
inside: "BRGTEST-240" -- a.txt
"BRGTEST-244" is dependent on "BRGTEST-240" for file a.txt
inside: "BRGTEST-219" -- e.txt
outside: "BRGTEST-244" -- b.txt
inside: "BRGTEST-242" -- a.txt
inside: "BRGTEST-240" -- a.txt
inside: "BRGTEST-219" -- e.txt

执行此操作的关键是认识到您需要遍历 的内容file2并查看每个元素是否根据您的条件匹配 中的内容file1。为此,您必须将一个循环嵌套在另一个循环内。

一旦有了这个结构,并且相应地解析了数据,就可以使用一个简单的语句来确定来自和if的第二列是否匹配,如果是,我们将使用我们收集的片段来发送消息。file1file2echo

相关内容