-1

I have two pipe-delimited text files (say, A.txt and B.txt).

Below is A.txt file format, a standard format (can think of as Universal Set)

"EmpId"|"FName"|"LName"|"Sex"|"DOB"|"SSN"|"TagId1"|"TagId2"

Below is B.txt file (can think of Sub Set) with column and 2 records.

"SSN"|"LName"|"FName"|"DOB"|"Sex"|"EmpId"
"123"|"Barrat"|"Alanzon"|"1983"|"F"|"4455"
"678"|"Alexia"|"Timothy"|"1975"|"M"|"2222"||"baz"

I need to convert B.txt into A.txt format’s columns order. Expected result is:

"EmpId"|"FName"|"LName"|"Sex"|"DOB"|"SSN"|"TagId1"|"TagId2"
"4455"|"Alanzon"|"Barrat"|"F"|"1983"|"123"|||
"2222"|" Timothy "|" Alexia"|"M"|"1975"|"678"||"baz"

How to go about it?

madhead
  • 31,729
  • 16
  • 153
  • 201

2 Answers2

1

Since the two formats are basically the same, except the columns are reordered, I would recommend just reading B.txt into a dictionary (with the column name as the key) and then printing this dictionary to a file, with the columns in the right order.

Paul Manta
  • 30,618
  • 31
  • 128
  • 208
0
awk -F'|' -v OFS='|' '{print $6, $3, $2, $5, $4, $1, $7, $8}' B.txt > A.txt
Barmar
  • 741,623
  • 53
  • 500
  • 612