I have a CSV file, formatted like this:
0001 @ word @ some information @ other information @
The last column is left empty. I want to tag each line and put the tags in the last column.
I also have a directory, filled with plain text files containing nothing more than lists of words (1 per line). The filename of each list is identical to the tag I want to apply.
Lists/
Lists/fruit1
Lists/fruit2
Lists/vegetables1
Lists/vegetables2
How can I add the name of the every file which contains an exact match for the word in column 2 of the CSV to the end of the CSV? For e.g.:
A line in the CSV is like this, and the word “banana” appears in both of the lists in fruit1 and fruit2.
0004 @ banana @ some information @ other information @
The line above would have those two file names added to the last column of the CSV:
0004 @ banana @ some information @ other information @ fruit1 fruit2
You can do this using a loop to read each line. Extract the second field using
cut, and then usegrepto find the files containing this field. Thenechoout your results.This is shown below: