LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-05-2010, 01:43 PM   #1
Raveolution
Member
 
Registered: Mar 2004
Distribution: Fedora & CentOS
Posts: 213
Blog Entries: 2

Rep: Reputation: 26
Comparing lines in 2 text files?


Is there a way, besides writing a PERL program, to read each line one by one in file A and tell if this line also exists in file B?

Can this be done via a shell script?

Thanks!
 
Old 11-05-2010, 01:55 PM   #2
pljvaldez
LQ Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Wheezy (x86)
Posts: 6,094

Rep: Reputation: 281Reputation: 281Reputation: 281
man diff
 
Old 11-05-2010, 01:59 PM   #3
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,091

Rep: Reputation: 41
you could write a shell script but it wouldnt be very efficient.. At least of the top of my head I cant think how to do it without reading a file as many times as there are lines.

awk would probably be a better solution.

shell would look something like this.

Code:
#!/bin/bash
#Source file to read from
fileA=filea.txt
#File to check against
fileB=fileb.txt

num_lines=`wc -l $fileA | sed "s/\(^[0-9]*\).*/\1/"`
let cnt=1
while [ $cnt -le $num_lines ]
do
  line=`sed -n ${cnt}p $fileA`
  if grep "$line" $fileB
  then
    echo "LINE# $cnt exists in $fileB"
  fi
  let cnt++
done
 
1 members found this post helpful.
Old 11-05-2010, 02:00 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
I agree, `diff` is the way to go. There's also `grep -f` if you like, for comparing files. Or the plain shell loop like below, which probably is not reliable if the files contain anything weird or beyond simple ascii text -- you'd maybe need a more advanced regex:
Code:
while read line; do if grep -q "$line" fileB; then echo "$line"; fi; done < fileA
 
1 members found this post helpful.
Old 11-05-2010, 02:02 PM   #5
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,091

Rep: Reputation: 41
diff would be a good choice as long as the files are not vastly different from each other.
 
Old 11-05-2010, 02:15 PM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
You could also (in Bash) use arrays and embedded loops...
Code:
 ARR1=( $(cat fileA) )
 ARR2=( $(cat fileB) )
 for ((x=0;x<${#ARR1[@]};x++)); do
     for ((y=0;y<${#ARR2[@]};y++)); do
         if [ "${ARR1[x]}" = "${ARR2[y]}" ]; then
             echo "Match: ${ARR1[x]}"
         fi
     done
 done
 
1 members found this post helpful.
Old 11-06-2010, 03:59 PM   #7
Raveolution
Member
 
Registered: Mar 2004
Distribution: Fedora & CentOS
Posts: 213

Original Poster
Blog Entries: 2

Rep: Reputation: 26
Quote:
Originally Posted by GrapefruiTgirl View Post
You could also (in Bash) use arrays and embedded loops...
Code:
 ARR1=( $(cat fileA) )
 ARR2=( $(cat fileB) )
 for ((x=0;x<${#ARR1[@]};x++)); do
     for ((y=0;y<${#ARR2[@]};y++)); do
         if [ "${ARR1[x]}" = "${ARR2[y]}" ]; then
             echo "Match: ${ARR1[x]}"
         fi
     done
 done
Ah, I didn't know this task could get that complex. I can work with this script the best, but they're all good and extremely instructional. This looks like it works even for stuff with complex data.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
deleting lines from text files caponewgp Linux - Newbie 10 09-17-2009 09:47 PM
Shell script for comparing certain lines in two files mou5e Linux - Newbie 9 06-06-2007 01:40 PM
Comparing text files... jong357 Slackware 14 03-31-2007 04:29 PM
Interlacing the lines of two text files khdubya Programming 3 02-03-2007 05:27 PM
reverse order some lines of text files Melsync Programming 4 09-20-2005 04:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:45 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