Skip to main content
edited tags
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 264
deleted 17 characters in body; deleted 21 characters in body
Source Link
Rui F Ribeiro
  • 58k
  • 28
  • 156
  • 237

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
...

I am new to awk. 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? Thanks for any help.

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
...

I am new to awk. 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? Thanks for any help.

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?

Source Link

awk or bash to read IDs from CSV, match rows in second CSV, change value in 2nd column

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
...

I am new to awk. 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? Thanks for any help.