Home > Software design >  Compare 2 nd columns from 2 files unix
Compare 2 nd columns from 2 files unix

Time:01-18

Compare 2 nd columns from 2 files, unmatch match first file record s write into output file

Example:

@ delimiter

Filename_clientid.txt

RIA00024_MA_plan_BTR_09282022_4.xml@RIA00025
RIA00024_MA_plan_BTR_09282022_5.xml@RIA00024
RIA00026_MA_plan_BTR_09282022_6.xml@RIA00026

Client_id.txt

ramesh@RIA000025
suresh@RIA000024
vamshi@RIA000027

Excepted output:

RIA00026_MA_plan_BTR_09282022_6.xml@RIA00026

I used awk command not working can you help me

awk  -F '@' 'NR==FNR{a[$2]; next} FNR==1 || !($1 in a)' Client_id.txt Filename_clientid.txt

CodePudding user response:

The number of zeroes is not the same in both files. If they are the same, you can check that the field 2 value of Filename_clientid.txt does not occur in a

Filename_clientid.txt

RIA00024_MA_plan_BTR_09282022_4.xml@RIA00025
RIA00024_MA_plan_BTR_09282022_5.xml@RIA00024
RIA00026_MA_plan_BTR_09282022_6.xml@RIA00026

Client_id.txt

ramesh@RIA00025
suresh@RIA00024
vamshi@RIA00027

Example

awk -F'@' 'NR==FNR{a[$2]; next} !($2 in a)' Client_id.txt Filename_clientid.txt

Output

RIA00026_MA_plan_BTR_09282022_6.xml@RIA000026

CodePudding user response:

With corrected inputs (was wrong with number of zeroes):

file1

RIA00024_MA_plan_BTR_09282022_4.xml@RIA00025
RIA00024_MA_plan_BTR_09282022_5.xml@RIA00024
RIA000026_MA_plan_BTR_09282022_6.xml@RIA000026

file2

ramesh@RIA000025
suresh@RIA000024
vamshi@RIA000027
ramesh@RIA000026

code

awk -F'@' 'NR==FNR{a[$1]=$0;next} $2 in a{print a[$2]}' file1 file2

Output

RIA000026_MA_plan_BTR_09282022_6.xml
  • Related