Using awk , you can do something like this -
awk -v FS="," 'NR==FNR{a[$1]=$0;next} ($1 in a){print a[$1]}' csv_file txt_file
- Set the field separator to
, - Using the first column (word) of your csv file as an index, load the entire row into an array.
- Check if the value of the text file is in the array.
- If he then prints it
Test:
[jaypal:~/Temp] cat csv_file jack,4 rabbit,10 cat,4 red,39 [jaypal:~/Temp] cat txt_file red rabbit cat [jaypal:~/Temp] awk -v FS="," 'NR==FNR{a[$1]=$0;next} ($1 in a){print a[$1]}' csv_file txt_file red,39 rabbit,10 cat,4
source share