繁体   English   中英

在Linux中使用AWK匹配文件

[英]Matching files using awk in linux

我有2个文件:

1.txt:

e10adc3949ba59abbe56e057f20f883e
f8b46e989c5794eec4e268605b63eb59
e3ceb5881a0a1fdaad01296d7554868d

2.txt:

e10adc3949ba59abbe56e057f20f883e:1111
679ab793796da4cbd0dda3d0daf74ec1:1234
f8b46e989c5794eec4e268605b63eb59:1@/233:

我想要2个文件作为输出:一个是result.txt,其中包含来自2.txt的行,其匹配项位于1.txt,另一个是left.txt,其中包含来自1.txt的行,其匹配项不在2.txt中

这两个文件的预期输出如下:result.txt

e10adc3949ba59abbe56e057f20f883e:1111
f8b46e989c5794eec4e268605b63eb59:1@/233:

left.txt

e3ceb5881a0a1fdaad01296d7554868d

我用awk尝试了1-2种方法,但没有成功。 任何帮助将不胜感激。

我的剧本:

awk '
FNR==NR{
  val=$1;
  sub(/[^:]*/,"");
  sub(/:/,"");
  a[val]=$0;
  next
}
!($NF in a){
  print > "left.txt";
  next
}
{
  print $1,$2,a[$NF]> "result.txt"
}
'  FS=":" 2.txt FS=":"  OFS=":" 1.txt

跟随awk可能会帮助您。

awk 'FNR==NR{a[$1]=$0;next} ($0 in a){print a[$0] > "results.txt";next} {print > "left.txt"}' FS=":" OFS=":" 2.txt FS=" " OFS=":" 1.txt

编辑:这里也添加了代码说明。

awk '
FNR==NR{    ##FNR==NR condition will be TRUE when first Input_file is being read by awk. Where FNR and NR are the out of the box variables for awk.
  a[$1]=$0; ##creating an array named a whose index is $1 and value is $2 from 2.txt Input_file.
  next      ##next is out of the box keyword from awk and will skip all further statements of awk.
}
($0 in a){  ##Checking here condition if current line of Input_file 1.txt is present in array named a then do following.
  print a[$0] > "results.txt"; ##Printing the current line into output file named results.txt, since current line is coming in array named a(which was created by 1st file).
  next      ##next is awk keyword which will skip further statements for awk code now.
}
{
  print > "left.txt" ##Printing all lines which skip above condition(which means they did not come into array a) to output file named left.txt as per OP need.
}
' FS=":" OFS=":" 2.txt FS=" " OFS=":" 1.txt ##Setting FS(field separator) as colon for 2.txt and Setting FS to space for 1.txt here. yes, we could set multiple field separators for different Input_file(s).

这个怎么样:

awk 'BEGIN{ FS = ":" }NR==FNR{ a[$0]; next }$1 in a{ print $0 > "results.txt"; delete a[$1]; next }END{ for ( i in a ) print i > "left.txt" }' 1.txt 2.txt

输出:

results.txt

e10adc3949ba59abbe56e057f20f883e:1111
f8b46e989c5794eec4e268605b63eb59:1@/233:

left.txt

e3ceb5881a0a1fdaad01296d7554868d

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM