LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-21-2017, 02:29 PM   #1
hack3rcon
Senior Member
 
Registered: Jan 2015
Posts: 1,432

Rep: Reputation: 10
Post diff command.


Hello.
I like to compare two .txt files, but I like "diff" command just show deleted lines and not modified lines. When I use "diff file1 file2" it show all lines that are modified but I like it just show deleted lines.
Any idea?

Thank you.
 
Old 10-21-2017, 02:41 PM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,112
Blog Entries: 3

Rep: Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003
I'm not sure about using diff for that. I guess it could be done. However, I would try comm for that instead.

Code:
comm -23 one.txt two.txt
See "man comm" for the details.

Last edited by Turbocapitalist; 10-21-2017 at 02:43 PM.
 
1 members found this post helpful.
Old 10-21-2017, 02:51 PM   #3
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,112
Blog Entries: 3

Rep: Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003
On second though, maybe you mean something like this?

Code:
diff --unchanged-line-format= \
     --old-line-format='%L' \
     --new-line-format= \
     one.txt two.txt
As expected, see "man diff" for the formatting options.
 
Old 03-02-2018, 08:53 AM   #4
hack3rcon
Senior Member
 
Registered: Jan 2015
Posts: 1,432

Original Poster
Rep: Reputation: 10
Not worked. I just want to see deleted lines and not modified.
 
Old 03-02-2018, 10:16 AM   #5
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,631

Rep: Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205
Where file2 contains the deleted lines:
Code:
diff -y file1 file2 | grep "<"
Presumably, if the deleted lines are in file one, replace < with >, but I didn't test that.

Although I see that Turbocapitalist's solution works great!

Last edited by scasey; 03-02-2018 at 10:27 AM.
 
Old 03-02-2018, 12:25 PM   #6
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,112
Blog Entries: 3

Rep: Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003
Quote:
Originally Posted by hack3rcon View Post
Not worked.
Which one? The diff one above works for me using my data. Please explain in more detail using some sample data.
 
Old 03-03-2018, 10:30 AM   #7
hack3rcon
Senior Member
 
Registered: Jan 2015
Posts: 1,432

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by Turbocapitalist View Post
Which one? The diff one above works for me using my data. Please explain in more detail using some sample data.
File format is "First Name,Last Name,Address,Email Address". When the email address changed, then "diff" command consider it, but when a line removed then I want "diff" command show it to me.
 
Old 03-03-2018, 10:33 AM   #8
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,112
Blog Entries: 3

Rep: Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003
As mentioned, the methods above work for my sample data. If it is not working for your data then you'll have to provide a sanitized sample. Preferably also include how you are invoking diff or other program.
 
Old 03-03-2018, 11:21 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 12,995

Rep: Reputation: 4097Reputation: 4097Reputation: 4097Reputation: 4097Reputation: 4097Reputation: 4097Reputation: 4097Reputation: 4097Reputation: 4097Reputation: 4097Reputation: 4097
diff has a slightly different approach. It does not display deleted lines as deleted, but it displays lines exist in A, in B or in both.
So if the line was found only in A then it is deleted from B. The line is existing in B was deleted in A (or probably it was never there, just was inserted into B. Who knows?).

I think diff itself cannot distinguish between deleted and modified lines, so it will display lines exist only in A, only in B or in both (but in that case that given line is identical in both files). Or you may say there is no "partial match" or something like that.

If you wish to see use meld instead of diff, it has a very good GUI. If you need to process those differences you need specify exactly how to differentiate between modified and deleted lines. I think it cannot be specified easily, and depends on the situation. Just a simple example:
Code:
file A:
aaaaaabbbb
aaaaaabbbb
aaaaaabbbb
aaaaabbbbb
aaaaabbbbb
aaaabbbbbb
aaabbbbbbb
aaabbbbbbb
abbbbbbbbb
file B:
aaaaaaabbb
aaaaabbbbb
aaabbbbbbb
aabbbbbbbb
So which line was modified here and which line was deleted? (from my point of view it is unclear, therefore I cannot really tell you how should we "evaluate" it).
From the other hand what diff will report is ok.
 
1 members found this post helpful.
Old 03-03-2018, 07:35 PM   #10
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 15,328
Blog Entries: 25

Rep: Reputation: 4391Reputation: 4391Reputation: 4391Reputation: 4391Reputation: 4391Reputation: 4391Reputation: 4391Reputation: 4391Reputation: 4391Reputation: 4391Reputation: 4391
There's a GUI front end for diff called Meld. It works very nicely; you may want to check it out.
 
Old 03-03-2018, 08:13 PM   #11
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,513

Rep: Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004
Diff is used to show differences between files. It really can't know, nor can any program, whether a line was deleted or was never there in the first place.
 
Old 03-04-2018, 10:21 AM   #12
hack3rcon
Senior Member
 
Registered: Jan 2015
Posts: 1,432

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by frankbell View Post
There's a GUI front end for diff called Meld. It works very nicely; you may want to check it out.
Can "Meld" do it for me?
 
Old 03-04-2018, 10:29 AM   #13
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 19.1 MATE
Posts: 8,016
Blog Entries: 5

Rep: Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859
Quote:
Originally Posted by hack3rcon View Post
Can "Meld" do it for me?
If it can, I don't see it in the settings.

Did you try the comm command suggested in the first reply?

See the top answer in:

https://serverfault.com/questions/68...-cant-do-it-wh
 
Old 03-04-2018, 11:14 AM   #14
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 12,995

Rep: Reputation: 4097Reputation: 4097Reputation: 4097Reputation: 4097Reputation: 4097Reputation: 4097Reputation: 4097Reputation: 4097Reputation: 4097Reputation: 4097Reputation: 4097
Quote:
Originally Posted by hack3rcon View Post
Can "Meld" do it for me?
Looks like you did not understand the problem, there is no answer to your question. There is no way to tell if a line was modified/added/deleted, because there is no way to know how the line was actually gotten into that file, it was only known by the one who made that change, but probably (s)he has already forgotten that.

Therefore (almost) all the diff tools trying to guess something. diff itself will not try to guess, just reports what was found.

Instead of asking
Quote:
Originally Posted by hack3rcon View Post
Can "Meld" do it for me?
please just test if the guess meld made is good enough for you. Usually yes, it is good enough, but there are cases...
 
Old 03-05-2018, 09:49 AM   #15
hack3rcon
Senior Member
 
Registered: Jan 2015
Posts: 1,432

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by pan64 View Post
Looks like you did not understand the problem, there is no answer to your question. There is no way to tell if a line was modified/added/deleted, because there is no way to know how the line was actually gotten into that file, it was only known by the one who made that change, but probably (s)he has already forgotten that.

Therefore (almost) all the diff tools trying to guess something. diff itself will not try to guess, just reports what was found.

Instead of asking please just test if the guess meld made is good enough for you. Usually yes, it is good enough, but there are cases...
Thank you so much.
"Meld" is good and solved my problem
 
  


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
[SOLVED] need some help on using diff command kingston Linux - Newbie 5 01-13-2011 04:37 AM
Diff Command is possible for me? admod Linux - Virtualization and Cloud 1 11-21-2010 09:42 AM
Question on diff command just.srad Programming 4 04-05-2008 04:01 AM
How to use 'diff' command ursusman Linux - Newbie 1 03-08-2007 04:29 AM
help on diff command anandss2004 Programming 2 11-01-2005 12:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:02 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration