Artificial intelligent assistant

awk replace field value based on matching value in another file I have 2 files, `Zipcode.txt` and `Address.csv`: ZipCode.txt 12345 23456 34567 45678 Address.csv 12345,3587 main st,apt j1,city,new jersey 23456,4215 1st st. s.,suite a2,city,new jersey 65432,115 main st,,city,new jersey 45678,654 2nd st n.,city,new jersey If the zipcode field in `Zipcode.txt` matches the zipcode field in `Address.csv`, I want to change the 4th field from `city` to `found`. Here's what I want: 12345,3587 main st,apt j1,found,new jersey 23456,4215 1st st. s.,suite a2,found,new jersey 65432,115 main st,,city,new jersey 45678,654 2nd st n.,found,new jersey This is what I have tried: awk -F',' 'BEGIN{OFS=FS}NR==FNR{a[$1]=1;next}a[$1]{$4="found"}1' Address.csv ZipCode.txt

This will search whether the zip code in the first field of `Address.csv` is found anywhere in `Zipcode.txt`:


awk -F, -v OFS="," 'NR==FNR {a[$1]++;next} $1 in a {$4="found"} 1' Zipcode.txt Address.csv


Output:


12345,3587 main st,apt j1,found,new jersey
23456,4215 1st st. s.,suite a2,found,new jersey
65432,115 main st,,city,new jersey
45678,654 2nd st n.,city,found


Note that the last line is not as expected, because `city` is not the fourth field in the input: a comma is likely missing in the last line of `Address.csv`.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 3408147c13282effa1de33ff442d3982