0

I have two .csv

items-to-change.txt is a single column list of unique ids:

218294
222084
197931
198161
197432
201021
214009

items-by-location.txt has unique ids and location number (tab delimited)

ID  Location
197432  1
201021  2
214009  4
214438  5
214492  1
...

I need to change the location field in items-by-location.txt to 6 for any ID from matches an ID in the items-to-change.txt, then create a new .csv from this, such as items-by-location2.txt

items-by-location2.txt should be:

197432  6
201021  6
214009  6
214438  5
214492  1
...

Is awk is a good solution for this? I understand that awk has a search parameter, but it is not clear how I would use awk to read through items-to-change.txt get the ID then read line by line the items-by-location.txt, compare the ids and only 2nd column of items-by-location.txt if there is a matching ID in 1st column.

Can someone provide an example?

  • awk would work well for this. You can go either by the FILENAME internal variable, or use the old NR == FNR / NR != FNR trick to run different code based on which file you're parsing. – DopeGhoti Dec 6 '18 at 20:09
0

Adding the change list into an array, and then checking that array when processing the main input file is perfect for awk:

$ awk 'FILENAME=="changeset" { replace[$1] = 1 } FILENAME=="input" {if( $1 in replace ) { $2 = 6 }; print $1,$2}' changeset input
ID Location
197432 6
201021 6
214009 6
214438 5
214492 1
  • I wonder how many times this awk code (or similar ones) have been written in answers on this site... – Kusalananda Dec 6 '18 at 20:22
  • I'd guess at least once or thrice. – DopeGhoti Dec 6 '18 at 21:23

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.