0

I have a universe file /tmp/universe.txt

aaa
bbb
ccc

I have another csv file, and I only wants to print a certain fields of a row of this csv file if the first field exists in /tmp/universe.txt

for example for this file

aaa,1
ddd,3
ccc,2

I want to print

aaa,1
ccc,2

How do I achieve it? I could write a python/perl script, but i suspect there are more elegant solution in awk/bash etc.

CuriousMind
  • 15,168
  • 20
  • 82
  • 120

1 Answers1

3

This could be as simple as:

grep -f universe.txt file

But this solution assumes that you list complete words which can't match partially in universe.txt. E.g.: listing aa or 1 would also match aaa,1.

For complete matches you need:

grep -f <(sed 's/.*/^\0,/' universe.txt) file

assuming there aren't any special characters in universe.txt.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176