LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 06-01-2009, 04:41 PM   #1
daberkow
LQ Newbie
 
Registered: May 2009
Posts: 11

Rep: Reputation: 0
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.
 
Old 06-01-2009, 05:14 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
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.
 
Old 06-01-2009, 05:48 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by daberkow View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to compare two files swatward Linux - Newbie 17 04-18-2012 07:14 PM
Script to compare numbers inside two text files bugg_deccan Programming 3 10-17-2007 10:53 PM
Interlacing the lines of two text files khdubya Programming 3 02-03-2007 06:27 PM
bash script that can read lines of text palceksmuk Programming 1 12-25-2005 04:49 AM
how to compare 2 text files by using php code antony_csf Programming 3 10-14-2004 06:52 AM


All times are GMT -5. The time now is 07:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration