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 07-01-2015, 10:12 AM   #1
gaurav_s
Member
 
Registered: Jul 2014
Posts: 43

Rep: Reputation: Disabled
find difference of file line by line


I have two files f1 and f2

f1 contains

Tom Harry Anthony

Cat Dog Camel


f2 contains

Tom Cook Anthony

Giraffe Camel Dog


i want to find difference line by line.


Such thatg if i do a diff on line 1 of both files i should get
Cook

and in line2 i should get
Cat Giraffe



I tried with comm command but its not much helpful,its expecting both file should be sorted.
 
Old 07-01-2015, 11:03 AM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Code:
diff -y f1 f2
see the man page for diff
 
Old 07-01-2015, 11:13 AM   #3
gaurav_s
Member
 
Registered: Jul 2014
Posts: 43

Original Poster
Rep: Reputation: Disabled
diff -y just prints both the lines.
 
Old 07-01-2015, 11:21 AM   #4
gaurav_s
Member
 
Registered: Jul 2014
Posts: 43

Original Poster
Rep: Reputation: Disabled
I tried with all the options of diff ,none of the options gives desired output.
 
Old 07-01-2015, 11:39 AM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by gaurav_s View Post
I tried with all the options of diff ,none of the options gives desired output.
Looks like you'll have to write a script to do this. Should just have to read both lines into an array, and compare the elements.
 
Old 07-01-2015, 08:10 PM   #6
catatom
LQ Newbie
 
Registered: Oct 2014
Posts: 23

Rep: Reputation: Disabled
If you want to compare each word instead of each line, this piece of Bash script will translate whitespace (spacebar button) to newlines (Enter button).

$text | tr " " "\n"
## The tr syntax is a bit like grep's.

You could probably write a Bash script that uses diff to find each line with two variations, record each variation by assigning the output to two variables, then compare the values of those variables by applying tr followed by diff. You'll probably need to use an until statement that keeps going down the output of diff until it recieves empty input. All these lovely bash functions can be found on the bash info page. Enter info bash.
 
Old 07-02-2015, 04:12 AM   #7
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
It is not clear to me what you are really aiming for here, since your example is somewhat ambiguous.

But, it seems as though you do NOT want to compare the files word by word, because then you would get a very different output than what you describe in your example. My guess is that you want to check if a word from file2 exists in the corresponding line of file1.

If that is indeed the case, then this will work:
Code:
#!/usr/bin/env python3

"""
http://www.linuxquestions.org/questions/linux-software-2/find-difference-of-file-line-by-line-4175546931/
"""

# Open the files
with open("file1.txt") as f1, open("file2.txt") as f2: 
    # Read both files. f1 as a list with lines,
    # f2 as a string 
    f1Lines = f1.readlines()
    f2Str = f2.read()

    # Split f2 into list based on WORDS
    f2Split = f2Str.split()

lineNum = 1 
j = 0 
# Now loop
for i in range(0, len(f2Split)):
    # Compare WORD of file2 against WORDS in file1,
    # line by line
    if f2Split[i] not in f1Lines[j]:
        print("Line " + str(lineNum) + ": " + f2Split[i])

    # If you're at the 3rd word in f2, simulate a
    # new 'line' and add 1 to index j
    if (i+1)%3 == 0:
        j+=1
        lineNum+=1
If I run this code on your example files, I get the following output:
Code:
./pydiff.py 
Line 1: Cook
Line 2: Giraffe
But then there's the odd Cat in your example:
Quote:
Such thatg if i do a diff on line 1 of both files i should get
Cook

and in line2 i should get
Cat Giraffe
I really don't understand why you should get 'Cat' as output. If you follow the logic of MY program, it looks for each word in file2, if it is NOT in the same line in file1 it prints it.
So...
file2, line2: Giraffe Camel Dog
file1, line2: Cat Dog Camel


There is no 'Giraffe' in line 2 of file1, therefore it prints.

But by YOUR logic, you should not only get:
Quote:
and in line2 i should get
Cat Giraffe
But you should ALSO get the following output from line1:
Cook Harry

Why? because there are no 'Harry's in line1 of file2.

So... if you want to do THAT kind of a check, well, it can be implemented with a couple of more lines of code, but as I said above, it is far from clear to me what it is you actually want to achieve here.

Best regards
 
  


Reply

Tags
diff, file



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
search folders, find a line in a file, and replace it with a new line ithirdeye Programming 2 04-07-2012 04:27 PM
C++ text file line by line/each line to string/array guru11 Programming 5 12-29-2011 09:34 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM

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

All times are GMT -5. The time now is 06:25 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
Open Source Consulting | Domain Registration