awk - Compare two files and print Available and NotFound -Continuation: -


would compare second field f11.txt , first field f22.txt print match cases "available" , non match cases "notfound" if field $4 (file f11.txt) null, if field $4 (file f11.txt) not null print lines of f11.txt is.

inputs:  f11.txt a,10,zzz b,20,zzz,yyy c,50,zzz  f22.txt 10,yyy 20,yyy 30,yyy 40,yyy 

have tried below command, sat help.

awk -f "," 'nr==fnr{a[$1]=$0;next}{print $0 "," (a[$2]?"available":"notfound")  }' f22.txt f11.txt 

got below output

a,10,zzz,available b,20,zzz,yyy,available c,50,zzz,notfound 

where b,20,zzz,yyy match case don’t want override "available" $4 not null (empty)

expected output:

a,10,zzz,avilable b,20,zzz,yyy c,50,zzz,notfound 

i believe script below want

begin {     fs=ofs="," }  nr==fnr {     a[$1]=$0     next }  !$4 {     $4 = (a[$2] ? "available" : "notfound") }  1 

updated script explicitely check empty fourth field (to allow longer lines empty fourth field. updated again replace empty fourth field instead of appending field.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -