user1869481
user1869481

Reputation: 1

Writing output from a grep search into a single file

I have for loop which I'm using to iterate through a list of keywords. I then search for each word in another file using the grep function and I'm writing it to another file. The problem is the program deletes everything from the previous iteration of the loop so I'm only left with the output from the last keyword in the file. How can I write each output to a new line of the same file?

This is the code I have. I apologize for the noob question - I'm new to this. Thanks!

#!/bin/bash

for i in $(cat file1.txt); do
        grep $i file2.txt>file3.txt 
done

Upvotes: 0

Views: 2787

Answers (2)

Tom Fenech
Tom Fenech

Reputation: 74595

You are overwriting the file because you are using > (use >>) but also, I don't think that a loop is necessary at all. If all your patterns are in file1.txt you could do this instead:

grep -f file1.txt file2.txt > file3.txt

The -f switch means that patterns are obtained from file1.txt, one line at a time.

edit

I just realised that your words aren't all on separate lines, so you could stick with your loop (using something like l0b0 has suggested) or alternatively, you could get your file into the correct format (for example, using awk):

grep -f <(awk -v RS='\\s+' '1' file1.txt) file2.txt > file3.txt

The short awk command puts all the words from file1.txt onto separate lines. By default, each "record" is on a separate line but I have changed the value of RS (the record separator) to be one or more whitespace characters \s+. The 1 is a commonly used shorthand, which means that awk prints every record (because 1 evaluates to "true"). The default behaviour is to print each record on a separate line.

Upvotes: 1

l0b0
l0b0

Reputation: 58788

> creates a new file and appends to that. >> creates a new file or appends to an existing one.

By the way, the recommended way to loop over such a file is using a while loop, which avoids a useless use of cat award. Also, Use More Quotes:

while read -r pattern
do
    grep "$pattern" file2.txt >> file3.txt
done < file1.txt

Upvotes: 3

Related Questions