1

I have 2 files , one with Delimited and other one is Positional file.

These two files shares same unique ID's as shown below but both the files contains different information.

del.txt

11111|abc|01|||||||||

22222|xyz|09|||||||||

pos.txt

11111  abc newyork US 

22222  xyz dallas  US

The del.txt file consist of unique member id followed by name and Alternative ID separated by | (delimited) and similarly pos.txt has unique ID along with name except alternative id in positional format.

I want to insert alternative ID in pos.txt only if Unique Member ID matches in both the file as shown below

pos.txt

11111  abc newyork US 01

22222  xyz  dallas US 09

How can we achieve this using shell script?

1
  • Your pos.txt file still looks delimited, just by spaces instead of pipes. Will there ever be a case where there are no spaces between fields, or when a field has a space in the value?
    – phemmer
    Apr 21, 2014 at 12:25

1 Answer 1

3

With this problem, I think the best way is using awk:

$ awk -F'[ |]' 'FNR==NR{a[$1]=$3;next}($1 in a){print $0" "a[$1]}' del.txt pos.txt 
11111  abc newyork US 01

22222  xyz dallas  US 09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .