LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-19-2005, 08:01 AM   #1
Goni
LQ Newbie
 
Registered: Sep 2005
Posts: 26

Rep: Reputation: 15
File reading line by line and compare


Hi, I am quite confused here trying to implement a scenerio in bash where I have to read a file line by line and to search that line in from another file. Basically I want to compare two files, two files as seperate inputs read each line from src_file and try to search that line in from dst_file. Case sensativity is not a problem. We can use grep -i <..> anyway, if a match is found it displays line blah = found else not found. Each line from src_file should be parsed till EOF on dst_file.

If anyone can help me out in bash, I would really appreciate.

Goni
 
Old 09-19-2005, 11:00 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Are you talking about something like diff?
diff src_file dst_file
 
Old 09-19-2005, 11:05 AM   #3
Goni
LQ Newbie
 
Registered: Sep 2005
Posts: 26

Original Poster
Rep: Reputation: 15
To some extent yes, diff works but I can't parse its output. Infact I am doing this to automate some router audits for a set of commands in my SOP which may or maynot be present in the router. The I got the router current config file in here and the SOP in the other file, just want to see the commands in SOP are present in router config or not. That all.


Goni
 
Old 09-20-2005, 02:10 AM   #4
Goni
LQ Newbie
 
Registered: Sep 2005
Posts: 26

Original Poster
Rep: Reputation: 15
no replies ... anyone ??
 
Old 09-20-2005, 02:15 AM   #5
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
Is there any reason you can't just do this:
Code:
for line in `cat src_file`; do grep -i $line dst_file; done
And BTW: Please wait a full 24 hours before bumping your thread in accordance with the LQ Rules.

Last edited by scuzzman; 09-20-2005 at 02:19 AM.
 
Old 09-20-2005, 02:48 AM   #6
Goni
LQ Newbie
 
Registered: Sep 2005
Posts: 26

Original Poster
Rep: Reputation: 15
I can do that but you see clearly it shows, for each line cat file, show the match. Means, if even a single match is from src_file, it will loop till the EOF of src_file. And the list goes on for multiple matches.

Sorry about the bumping, infact I was expecting replies a bit quicker but anyway, I'll try to follow the rules .. .. thankx

Goni
 
Old 09-20-2005, 02:52 AM   #7
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
Do you not want it to go until EOF of src_file? By this I mean, do you want it to stop the first time it finds a match?
 
Old 09-20-2005, 03:03 AM   #8
Goni
LQ Newbie
 
Registered: Sep 2005
Posts: 26

Original Poster
Rep: Reputation: 15
nop, I want it for ALL possible matches in src_file (compared in from dst_file) till EOF.
 
Old 09-20-2005, 03:08 AM   #9
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
That script I gave you should do that. An example may help. Let's assume you have 2 files: src_file and dst_file.
Contents of src_file:
Code:
orange
apple
kiwi
grapefruit
That script will get the first item in src_file ('orange' in this case) and compare it to every line in dst_file and display any matches. It will then move on to the second, third, etc doing the same.
What that is saying, essentially, is this:
Take each item from 'cat src_file' and store it in $line. Then, compare this to the entire contents of dst_file, line for line. Then, move onto the next line in src_file.

Last edited by scuzzman; 09-20-2005 at 03:11 AM.
 
Old 09-20-2005, 03:13 AM   #10
Goni
LQ Newbie
 
Registered: Sep 2005
Posts: 26

Original Poster
Rep: Reputation: 15
Logically you are correct but the script ain't doing so. I got 1 matche in both files, total 5 lines in src_file and the script is showing me the match 5 times. As you said, is should move on but its not doing so. BTW: I am prety sure the messy output does not have to do anything weather if I run it on command or script?
 
Old 09-20-2005, 03:41 AM   #11
Snowbat
Member
 
Registered: Jun 2005
Location: q3dm7
Distribution: Mandriva 2010.0 x86_64
Posts: 338

Rep: Reputation: 31
src_file contains duplicate lines? Remove them and try again:
Code:
cat src_file | sort | uniq > src_file_2
for line in `cat src_file_2`; do grep -i $line dst_file; done

Last edited by Snowbat; 09-20-2005 at 03:43 AM.
 
Old 09-20-2005, 05:28 AM   #12
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
Quote:
Originally posted by Snowbat
src_file contains duplicate lines? Remove them and try again:
Code:
cat src_file | sort | uniq > src_file_2
for line in `cat src_file_2`; do grep -i $line dst_file; done
You can actually eliminate a full step by doing this:
Code:
for line in `cat src_file | sort | uniq`; do grep -i $line dst_file; done
 
Old 09-20-2005, 05:34 AM   #13
Goni
LQ Newbie
 
Registered: Sep 2005
Posts: 26

Original Poster
Rep: Reputation: 15
No, src_file does not contains any duplicate, not atleast at the moment. There are just 5 lines,

Line1
This is lane2
Mismatch3
Someone here4
and this is line 5

Just like this, and dst_file contains some other different commands/lines but one or more than one from src_file for example "This is lane2". When I run the script, I get the output correct but it shows me 5 lines of the same output. Says "This is lane2"

What will make difference sorting out the file?

Last edited by Goni; 09-20-2005 at 05:37 AM.
 
Old 09-20-2005, 09:29 PM   #14
Snowbat
Member
 
Registered: Jun 2005
Location: q3dm7
Distribution: Mandriva 2010.0 x86_64
Posts: 338

Rep: Reputation: 31
So dst_file contains (or can contain) duplicates? You can get grep to stop after the first matching line:
Code:
for line in `cat src_file`; do grep -i -m 1 $line dst_file; done
Quote:
Originally posted by scuzzman
You can actually eliminate a full step by doing this:
Code:
for line in `cat src_file | sort | uniq`; do grep -i $line dst_file; done
Good point, though it will sort and uniq the complete file for every line... potentially slower for a large file?
 
Old 09-21-2005, 12:24 AM   #15
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
Quote:
Good point, though it will sort and uniq the complete file for every line...
It shouldn't. It's taking every line of 'cat src_file | sort | uniq' then running the loop on that, not the original file. Once it cat's sort's and uniq's the file, it forgets about it.
 
  


Reply



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
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
Reading a file and running a command for each line. mijohnst Linux - General 11 08-22-2005 06:18 PM
reading from a file line by line Menestrel Programming 5 12-15-2004 02:17 AM
reading a line at a time in a file topcat Programming 2 12-14-2004 05:49 AM
Using diff to compare file with common lines, but at different line numbers jimieee Linux - Newbie 3 05-10-2004 07:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:23 PM.

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
Open Source Consulting | Domain Registration