Sorting 如何比较两个文件并打印两个不同文件的值

Sorting 如何比较两个文件并打印两个不同文件的值,sorting,unix,awk,solaris,filecompare,Sorting,Unix,Awk,Solaris,Filecompare,有两个文件。我需要先对它们进行排序,然后比较两个文件,然后打印文件1和文件2的值 文件1: pair,bid,ask AED/MYR,3.918000,3.918000 AED/SGD,3.918000,3.918000 AUD/CAD,3.918000,3.918000 文件2: pair,bid,ask AUD/CAD,3.918000,3.918000 AUD/CNY,3.918000,3.918000 AED/MYR,4.918000,4.918000 输出应为: pair,inpu

有两个文件。我需要先对它们进行排序,然后比较两个文件,然后打印文件1和文件2的值

文件1:

pair,bid,ask
AED/MYR,3.918000,3.918000
AED/SGD,3.918000,3.918000
AUD/CAD,3.918000,3.918000
文件2:

pair,bid,ask
AUD/CAD,3.918000,3.918000
AUD/CNY,3.918000,3.918000
AED/MYR,4.918000,4.918000
输出应为:

pair,inputbid,inputask,outputbid,outtputask
AED/MYR,3.918000,3.918000,4.918000,4.918000
两个文件中唯一的区别是AED/MYR,具有不同的出价/要价。如何打印文件1和文件2的差值

我尝试使用以下命令:

nawk -F, 'NR==FNR{a[$1]=$4;a[$2]=$5;next} !($4 in a) || !($5 in a) {print $1 FS a[$1] FS a[$2] FS $4 FS $5}' file1 file2
结果输出如下:

pair,bid,ask,bid,ask
AUD/CAD,3.918000,3.918000,3.918000,3.918000
AUD/CHF,3.918000,3.918000,3.918000,3.918000
AUD/CNH,3.918000,3.918000,3.918000,3.918000
AUD/CNY,3.918000,3.918000,3.918000,3.918000
AED/MYR,3.918000,3.918000,4.918000,4.918000

我们仍然无法得到唯一的区别。

请您尝试使用GNU
awk
编写并测试以下示例

awk -v header="pair,inputbid,inputask,outputbid,outtputask" '
BEGIN{
  FS=OFS=","
}
FNR==NR{
  arr[$1]=$0
  next
}
($1 in arr) && arr[$1]!=$0{
  val=$1
  $1=""
  sub(/^,/,"")
  if(!found){
    print header
    found=1
  }
  print arr[val],$0
}'  Input_file1  Input_file2
说明:添加上述内容的详细说明

awk -v header="pair,inputbid,inputask,outputbid,outtputask" '  ##Starting awk program from here and setting this to header value here.
BEGIN{                                                         ##Starting BEGIN section of this program from here.
  FS=OFS=","                                                   ##Setting field separator and output field separator as comma here.
}
FNR==NR{                                                       ##Checking condition FNR==NR which will be TRUE when Input_file1 is being read.
  arr[$1]=$0                                                   ##Creating arr with index $1 and keep value as current line.
  next                                                         ##next will skip all further statements from here.
}
($1 in arr) && arr[$1]!=$0{                                    ##Checking condition if first field is present in arr and its value NOT equal to $0
  val=$1                                                       ##Creating val which has current line value in it.
  $1=""                                                        ##Nullifying irst field here.
  sub(/^,/,"")                                                 ##Substitute starting , with NULL here.
  if(!found){                                                  ##Checking if found is NULL then do following.
    print header                                               ##Printing header here only once.
    found=1                                                    ##Setting found here.
  }
  print arr[val],$0                                            ##Printing arr with index of val and current line here.
}' Input_file1  Input_file2                                    ##Mentioning Input_files here.

使用
bash
过程替换,然后使用
join
然后使用
awk
选择:

# print header
printf "%s\n" "pair,inputbid,inputask,outputbid,outtputask"
# remove first line from both files, then sort them on first field
# then join them on first field and output first 5 fields
join -t, -11 -21 -o1.1,1.2,1.3,2.2,2.3 <(tail -n +2 file1 | sort -t, -k1) <(tail -n +2 file2 | sort -t, -k1) |
# output only those lines, that columns differ
awk -F, '$2 != $4 || $3 != $5' 
#打印标题
printf“%s\n”对,inputbid,inputask,outputbid,outtputask
#从两个文件中删除第一行,然后在第一个字段中对它们进行排序
#然后在第一个字段上加入它们,并输出前5个字段

加入-t,-11-21-o1.1,1.2,1.3,2.2,2.3欢迎加入SO。因此,我们强烈鼓励用户添加他们为解决问题而付出的努力,因此请在您的问题中添加同样的努力,然后让我们知道。嗨,Ravinder,更新了同样的内容。请检查,你好,拉文德。谢谢你的详细回答。非常感谢。我们在SunOS上工作,它与nawk一起工作。@Siddy_21111990,您的欢迎和愉快的学习干杯:)是的,在SunOS上您需要将其更改为
nawk
,做得好:)