2

I'm using Amazon Linux with bash shell. I have a file with comma delimited data, where each row looks like this

2049,576804C7DF554416A9EA25B2A5A81514,X,

Then I have a second file, with comma delimited data, in which each row looks like this

id,user_name,user_email

Every row in the second file will be unique. My question is, how do I take every row from the first file, and replace the second token with matching data (based on matching the second token from the first file with the first token from the second file) from the second file? So for instance the lien I listed above would become

2049,576804C7DF554416A9EA25B2A5A81514,username1,email1,X,

Assuming that "576804C7DF554416A9EA25B2A5A81514" could be located in the second file.

1

Something like the following script, using gawk

#!/bin/bash
gawk -F, 'tbl[$1]!=""{$2=tbl[$1];OFS=",";print;next}{tbl[$1]=$2;}' $1 $2

You pass in the two files with the token file first. This assumes the first file does not have any repetitions, and if the second file have any extra lines, where the first field does not have a match in the first file, then those lines are omitted from the output.

The gawk program builds up the associative array tbl, indexed by the first field, from the first file, then uses that to replace the second field for the second file, then prints the lines with replacements and using "," as field seperator. See man gawk for more details.

  • Thanks but should this print the output to the console? I'm noticing if file1 contains "2041,id1,X,X,," and file2 contians "id1,username,email", running "gawk -F, 'tbl[$1]!=""{$2=tbl[$1];OFS=",";print;next}{tbl[$1]=$2;}' file1 file2" produces nothing. – Dave Jan 23 '17 at 22:44
  • I might have misunderstood how your input files look. Perhaps you can update the question with one example of each, and an example of the desired resulting output/file. I understood that the first field of file1 is to match with hte first field of file2, then the file2 line is printed but its second field is replaced by the corresponding second field from file1. – Ralph Rönnquist Jan 24 '17 at 1:49

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.