0

I have file a and file lookup and the structure is below.

vi a.txt

empid|ename|sal_grade|MANAGER_ID
1|raj|A|202
2|MAN|B|203
3|JOS|C|204

vi lookup.txt

Grade|sal|manager_id
A|$100000|202
A|$1000|099
B|$1000000|203
B|$100|011

Output:
1|raj|A|202
2|MAN|B|203

command:

awk 'BEGIN {FS=OFS="|"} NR==FNR{a[$1];next} $3 in a{print $0}' lookup.txt a.txt >matched.txt

Here in the both files grade and manager_id is common and i want to join a.txt with lookup.txt on grade and manager_id and get the data out from a.txt where it got a match from lookup.txt .I tried with below command but it will join only on column i.e on grade column but i need to join on both grade and manager id column .

Thank's in advance.

0

If you need a combined key, build a combined key.

awk '
  BEGIN { FS=OFS="|" }
  NR==FNR { a[$1 "-" $3]; next }
  ($3 "-" $4) in a { print $0 }
' lookup.txt a.txt > matched.txt

You can use "-" as a separator, or another string, or nothing at all. With the data from your example it would work without a separator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.