1

I'm trying to grep strings from file2.csv using existing strings from file1.csv and write matched lines to result.csv file. A have a following bash script:

cat file1.csv | while read line; do
    grep $line ./file2.csv > result.csv
done

But afterall the result.csv is always empty. When I do manual grep from file2.csv everything works fine. What I do wrong?

file1.csv:

15098662745072
15098662745508

file2.csv:

";"0";"15098662745072";"4590";"4590";"
";"0";"15098662745508";"6400";"6400";"
";"0";"15098662745515";"6110";"6110";"
";"0";"15098662745812";"7970";"7970";"

expected result (result.csv):

";"0";"15098662745072";"4590";"4590";"
";"0";"15098662745508";"6400";"6400";"
7
  • Please provide small examples of file1.csv and file2.csv and your expected result. Commented Aug 22, 2013 at 15:30
  • @svante pls see the updated post with file examples Commented Aug 22, 2013 at 15:35
  • @dogbane's solutions(s) works on my system. Check your spelling/linefeeds/file permissions. Commented Aug 22, 2013 at 15:44
  • @svante i've just created duplicates of file1 and file2 and everything works fine now. but i have no idea why this doesn't work with my old files.. Commented Aug 22, 2013 at 15:47
  • Most likely you have \r\n at the end of file1.csv (you can check using od -bc < file1.csv) Commented Aug 22, 2013 at 15:48

2 Answers 2

4

> keeps overwriting the file. Use >> to append to it.

Instead of using a loop, you can simply use the -f option in grep to make grep read patterns from the file.

grep -f file1.csv file2.csv > result.csv

If you have to use a loop, use the following approach:

while read line; do
    grep "$line" ./file2.csv
done < file1.csv > result.csv
Sign up to request clarification or add additional context in comments.

1 Comment

If you have to use a bash loop always add IFS= and use read -r unless you have a specific functionality you require out of not using them.
1

You should be using awk for this, not grep, because:

a) grep does not by default look for strings, it looks for regular expressions. You need to use fgrep or grep -F or awk instead of grep to search for strings.

b) You really only want to match the numbers from file1.csv when they appear as a full specific field in file2.csv, not wherever they occur on the line.

awk -F'";"' 'NR==FNR{a[$0];next} $3 in a' file1.csv file2.csv > result.csv

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.