Bunion
Bunion

Reputation: 461

Grep from variable stored in a csv file

I'm try to grep for everything within "test.csv" from the contents on book.1

while IFS= read -r result
do
grep -r $result test.csv >> output.csv

echo $result

done<book1.csv

My book 1 has 1 row of data is column A.

I am getting the results of book1.csv shown on the screen so it is parsing correctly, but I'm getting nothing in the output.csv

I have manually checked the grep & there is contents of book1.csv within the test.csv

What have I done wrong?

EDIT

SAMPLE Input book1.csv

HOSTNAME
TEST1
TEST2

Sample input test.csv

blank,TEST1.abc.123,blank,date

I want to get every line from book1.csv that is in test.csv

Cat -a book1 result

TEST1^M$

Upvotes: 1

Views: 1358

Answers (2)

Charles
Charles

Reputation: 81

using fgrep should work. With the data that you provided, it gives:

fgrep -f book1.csv test.csv

Output:

blank,TEST1.abc.123,blank,date

fgrep is strictly equivalent to grep -F

from grep man page :

Matcher Selection
   -F, --fixed-strings
          Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.  (-F is specified by POSIX.)


Matching Control
   -f FILE, --file=FILE
          Obtain  patterns  from FILE, one per line.  The empty file contains zero patterns, and therefore matches nothing.  (-f is specified
          by POSIX.)

As pointed out by anubhava , you need to remove the DOS character. To do that, just use the dos2unix command:

fgrep -f <(dos2unix book1.csv) <(dos2unix test.csv)

Upvotes: 1

anubhava
anubhava

Reputation: 785316

As suspected initially your .csv files have DOS line ending with \r characters, which is causing grep -f to not match since Unix files just end with \n.

You can use this command with tr that removes \r from both .csv files before running grep:

grep -Ff <(tr -d '\r' < book1.csv) <(tr -d '\r' < test.csv)

blank,TEST1.abc.123,blank,date

tr -d '\r' < book1.csv will delete \r or carriage return character from given files.

Upvotes: 2

Related Questions