【问题标题】:Compare two files(file1 & file2) and add one column from from file2 to file1 if first column of two files matches比较两个文件(file1 和 file2),如果两个文件的第一列匹配,则从 file2 向 file1 添加一列
【发布时间】:2020-06-01 00:44:22
【问题描述】:

我有两个文件(file1 和 file2)

文件1

ABC=14.2.0.7.SAMPLE=git.xyz/plugins/gitiles/+/refs/heads/clientpatch/abc/patch142007
DEF=14.3.0.5.SAMPLE=git.xyz/plugins/gitiles/+/refs/heads/clientpatch/def/patch143005
DEF=14.3.0.5.SAMPLE2=git.calypso/plugins/gitiles/+/refs/heads/clientpatch/def/patch14300-calib
HIJ=12.0.0.0.Sp3.SAMPLE3=git.xyz/plugins/gitiles/+/refs/heads/clientpatch/hij/patch120000sp3
MNO=16.1.0.28.SAMPLE=git.xyz/plugins/gitiles/+/refs/heads/clientpatch/mno/patch161028

.......(150 行)

文件2

IJK = open 
ABC = closed 
PQR = closed 
DEF = open 
HIJ = open 
LMN = closed
MNO = closed 
PQR = open

......(> 150 行)

输出文件

ABC=14.2.0.7.SAMPLE=git.xyz/plugins/gitiles/+/refs/heads/client/abc/patch142007=closed
DEF=14.3.0.5.SAMPLE=git.xyz/plugins/gitiles/+/refs/heads/client/def/patch143005=open
DEF=14.3.0.5.SAMPLE2=git.xyz/plugins/gitiles/+/refs/heads/client/def/patch14300-calib=open
HIJ=12.0.0.0.Sp3.SAMPLE3=git.xyz/plugins/gitiles/+/refs/heads/client/hij/patch120000sp3=open
MNO=16.1.0.28.SAMPLE=git.xyz/plugins/gitiles/+/refs/heads/client/mno/patch161028=closed

我尝试了以下脚本。但它没有给我任何输出。甚至没有打印任何东西。没有错误

while IFS= read -r line
do
key1=`echo $line | awk -F "=" '{print $1}'` < file1
key2=`echo $line | awk -F "=" '{print $2}'` < file1 
key3=`echo $line | awk -F "=" '{print $3}'` < file1 
key4=`echo $line | awk -F "=" '{print $1}'` < file2   
value3=`echo $line | awk -F "=" '{print $2}'` < file2   
if [ "$key1" == "$key4" ]; then   
echo "$key1=$key2=$key3=$value3"   
fi   
done 

简要说明代码应如何工作。

代码应该比较两个文件(file1 和 file2)的第一列。如果每个名称都匹配,它应该给我上面列出的输出文件。否则转到下一行。如果我的两个文件是已排序或未排序格式,我应该得到输出。 帮助将不胜感激。谢谢

【问题讨论】:

    标签: shell awk


    【解决方案1】:

    请您尝试关注一下。

    awk '
    BEGIN{
      OFS="="
    }
    FNR==NR{
      a[$1]=$NF
      next
    }
    ($1 in a){
      print $0,a[$1]
    }
    '   Input_file2  FS="="  Input_file1
    

    说明:为上述代码添加详细说明。

    awk '                   ##Starting awk program from here.
    BEGIN{                  ##Starting BEGIN section from here.
      OFS="="               ##Setting OFS as = here for all lines.
    }
    FNR==NR{                ##Checking condition if FNR==NR which will be TRUE when file2 is being read.
      a[$1]=$NF             ##Creating an array a with index $1 and value is last field.
      next                  ##next will skip all further statements from here.
    }
    ($1 in a){              ##Checking condition if $1 of current line is present in array a then do following.
      print $0,a[$1]        ##Printing current line and value of array a with index $1.
    }
    '  file2 FS="=" file1   ##Mentioning Input_file file2 and file1 and setting FS="=" for file1 here.
    

    【讨论】:

    • 感谢 RavinderSingh 的快速回复。我得到了我想要的输出。
    • @RupeshMadhav,欢迎您,我现在也为我的代码添加了详细的解释,干杯
    • 您好 RavinderSingh,您对上述问题的命令并未打印我文件中的每一行。只需考虑 file1 有 150 行。它只打印不到 50 行。如果 file2 中的第二列包含两个单词,则它只打印第二个单词。你能建议另一个命令吗
    • @RupeshMadhav,你能打开一个新线程吗?总是建议每个线程只问一个问题,现在更改或添加到您的问题可能需要完全更改代码,所以恕我直言,如果您可以打开一个新问题,那就太好了。
    【解决方案2】:

    或者使用awk 的另一种方法,将file2 值存储在一个数组中,然后将正确的状态附加到file1 中的相应行:

    awk -F' = ' 'NR==FNR {a[$1]=$2; next} {print $0"="a[$1]}' file2 FS="=" file1
    

    使用/输出示例

    $ awk -F' = ' 'NR==FNR {a[$1]=$2; next} {print $0"="a[$1]}' file2 FS="=" file1
    ABC=14.2.0.7.SAMPLE=git.xyz/plugins/gitiles/+/refs/heads/clientpatch/abc/patch142007=closed
    DEF=14.3.0.5.SAMPLE=git.xyz/plugins/gitiles/+/refs/heads/clientpatch/def/patch143005=open
    DEF=14.3.0.5.SAMPLE2=git.calypso/plugins/gitiles/+/refs/heads/clientpatch/def/patch14300-calib=open
    HIJ=12.0.0.0.Sp3.SAMPLE3=git.xyz/plugins/gitiles/+/refs/heads/clientpatch/hij/patch120000sp3=open
    MNO=16.1.0.28.SAMPLE=git.xyz/plugins/gitiles/+/refs/heads/clientpatch/mno/patch161028=closed
    

    【讨论】:

    • 感谢大卫的快速回复。对于这个,我也得到了我想要的输出
    • 是的,有几种方法可以给这只猫剥皮,在这里,我们只使用split 来获取file1 值中的第一个字段,并使用它来附加open/closed存储在数组a 中。我实际上喜欢这两种解决方案:)
    • NR==FNR {a[$1]=$2; next} {foo} 会比NR==FNR {a[$1]=$2} NR&gt;FNR {foo}' 更惯用。此外,如果您使用了-F' ?= ?',那么您就不需要split()b[1],因为$1 将根据您的需要填充。
    • 谢谢 Ed,但-F "?=?" 不会导致file1 在每个字段中丢失一个字符吗?合并其余部分。
    猜你喜欢
    • 2020-03-24
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 2021-08-17
    相关资源
    最近更新 更多