LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash- how to compare only certain lines of text files (https://www.linuxquestions.org/questions/linux-newbie-8/bash-how-to-compare-only-certain-lines-of-text-files-729950/)

daberkow 06-01-2009 03:41 PM

bash- how to compare only certain lines of text files
 
Hi. I've got a bunch of text files that have a lot of different information in them, but I want to make sure that certain particular lines have matching information. Even if I could get it to spit out the same lines from each that would be good, but it would be nice to let the machine do the comparison.

For example, I have a line that says

FileA path1/xxx_123_Jim_yyy.zzz

but in the other file the line is

FileA path2/aaa_123_Jim_bbb.zzz

This is a good match because the 123_Jim part in both is the same.

I have several lines I want to compare, but there are many many lines in these files that are not going to be the same, so diff or comm don't seem to be the things I'm after. Also, the text I am interested in don't necessarily have to be on the same numbered line.

Code:

grep-w FileA | awk -F_ ' {print $2,$3} '
will pull the info I want to compare out of the correct lines out for me (once I get the correct files to compare in a list), but what do I do from there? I was thinking of making new files with > and comparing those with diff, but that seems to be inefficient to me.

Thanks.

rweaver 06-01-2009 04:14 PM

Honestly if you want to avoid creation of temp files and such you should move to using something a bit more robust for this type of operation... perl, ruby, python, etc. It will give you more flexibility.

Tinkster 06-01-2009 04:48 PM

Quote:

Originally Posted by daberkow (Post 3559473)
Code:

grep-w FileA | awk -F_ ' {print $2,$3} '
will pull the info I want to compare out of the correct lines out for me (once I get the correct files to compare in a list), but what do I do from there?

Code:

diff <(grep-w FileA | awk -F_ ' {print $2,$3} ') <(grep-w FileB | awk -F_ ' {print $2,$3} ')
No need for temp-files (or perl, python, ... )



Cheers,
Tink


All times are GMT -5. The time now is 09:23 AM.