比较两个文件(file1 和 file2),如果两个文件的第一列匹配,则从 file2 到 file1 添加一列



我有两个文件(file1和file2(

file1

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行(

file2

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(的第一列。如果每个名称都匹配,它应该会给我上面列出的输出文件。否则转到下一行。如果我的两个文件是排序或未排序的格式,我应该得到输出。我们将不胜感激。谢谢

或者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

请您尝试以下操作。

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.

最新更新