How to grep in order based on the input file

Here is an answer rather than a question that I thought I would share.

In my first attempt, I was using grep to find a list from file.1 within another file, file.2 which I then needed to paste the output from file.3 to file.1 in the same order. However, the results weren't what I wanted.

At first, I did it using the following line:

grep -f file.1 file2 > file.3

However, this didn't give the output in the same order as the input file. Rather it gave me the output using the order of the file.2 that was being searched. So file.3 was output using the sort order of file.2 not file.1 as I had wanted. :eek:

My problem was that I am wanting to compare the output of file.3 to the original file file.1 and wanted the output in the same order as the search file file.1.

************
This lead me to the following solution. I know it seems simple, but it is very effective to output in the same order as the search file.

for name in `cat file.1 `
do
grep $name file.2
done > file.3

This gives me file.3 in the same order as file.1 is in. Which I can then paste to file.1 for a report.

I offer this to anyone who needs something like this.

Hope it helps!!!

:cool: :wink:

1 Like

Does the "> file.3" on the outside of the done statement actually work? When I first saw the segment of code I thought, "Hey! He's gonna overwrite all those responses with an "echo done!"" Then I looked at it closer.

Yes it works. I actually stumbled upon this a few months ago.

I once pondered why this was. I get the same output whether I use > or >> to write out the file if it is outside the loop.

I believe that it is because the "> file.3" is outside of the loop so ALL of the grepped data goes out together and is written ONCE and not each time the loop runs.

I believe that this is a little faster than writing each time the loop finishes, especially with 2 large files. I only had about 200 lines for file1, but file.2 had almost 1000 lines to be searched.

You would be right if I had put the > file.3 inside the loop, it would overwrite the file each time instead of appending as with >> file.3 would do.

So both of these are the same.

_________________________
for name in `cat file.1 `
do
grep $name file.2
done > file.3

_________________________
for name in `cat file.1 `
do
grep $name file.2 >> file.3
done
_________________________

:cool: :smiley: