[英]awk Compare 2 files, print match and difference based on Start Range And End Range:
I need to comapre two files f1.txt
and f2.txt
and obtain matches, and non-matches, for this case I am looking to check Second field of f2.txt
is lying between StartRange and EndRange of f1.txt
,if yes then print first the second field of f2.txt
, then print the entire line of f1.txt
. 我需要comapre两个文件f1.txt
和f2.txt
并获得比赛和非比赛中,这种情况下,我希望检查的第二场f2.txt
躺在StartRange和EndRange之间f1.txt
,如果是,则首先打印的第二个字段f2.txt
,然后打印的整个线f1.txt
。 And for no match found on f1.txt
to state "Not Found" and then print f2.txt
entire line. 并且在f1.txt
上找不到匹配f1.txt
以声明“未找到”,然后打印f2.txt
整行。
f1.txt f1.txt
Flag,StartRange,EndRange,Month
aa,1000,2000,cc,Jan-13
bb,2500,3000,cc,Feb-13
dd,5000,9000,cc,Mar-13
f2.txt f2.txt
ss,1500
xx,500
gg,2800
yy,15000
Desired Output 期望的输出
ss,1500,aa,1000,2000,cc,Jan-13
xx,500,Not Found,Not Found,Not Found,Not Found
gg,2800,bb,2500,3000,cc,Feb-13
yy,15000,Not Found,Not Found,Not Found,Not Found
This might work for you: 这可能对您有用:
gawk 'BEGIN {
FS="," # Field separator
c=1 # counter
while ((getline line < ARGV[1]) > 0) {
if (line !~ "Flag,StartRange,EndRange,Month") { # No need for header
F[c]=line; # store line
split(line,a,",") # split line
F2[c]=a[2] ; F3[c]=a[3] # store the lines' range parts
c++
}
}
}
FILENAME==ARGV[2] {
# Work on second file
for (i in F) { # For every line scan the first file
# if within a range, step out
if ($2>=F2[i] && $2<=F3[i]) {found=i ; break}
# else check next
else {found=0}
}
# if the above found anything print the line from second file
# with the relavant line from the first
if (found>0) {
print $0 "," F[found]
}
# otherwise the not found message
else {
print $0 ",Not Found,Not Found,Not Found,Not Found"
}
}' f1.txt f2.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.