LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 05-12-2008, 07:13 AM   #1
kkpal
Member
 
Registered: Oct 2007
Posts: 101

Rep: Reputation: 15
Thumbs up bash command for make difference between two txt file


hi all


I have two txt file (let test1.txt and test2.txt. These txt file contains names. I want make a 3rd txt file which contain names(those name which are exists only in file test1.txt) and 4th txt file which contain (those name which are exists only in file test2.txt).
what is solution of this problem?


Thanks & Regards
KKPal
 
Old 05-12-2008, 07:22 AM   #2
elthox
Member
 
Registered: Oct 2006
Posts: 41

Rep: Reputation: 15
Quote:
Originally Posted by kkpal View Post
hi all


I have two txt file (let test1.txt and test2.txt. These txt file contains names. I want make a 3rd txt file which contain names(those name which are exists only in file test1.txt) and 4th txt file which contain (those name which are exists only in file test2.txt).
what is solution of this problem?


Thanks & Regards
KKPal
Check with this: diff text1 text2
and manipulate the output using > and < which indicate wo thich file the lines belong
 
Old 05-12-2008, 07:24 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Take a look at "cmp" and "diff"
 
Old 05-12-2008, 07:28 AM   #4
kkpal
Member
 
Registered: Oct 2007
Posts: 101

Original Poster
Rep: Reputation: 15
Thumbs up

Quote:
Originally Posted by elthox View Post
Check with this: diff text1 text2
and manipulate the output using > and < which indicate wo thich file the lines belong
I had use diff.
diff test1.txt test2.txt > text.txt

but it wasn't work.

I need only difference not details

Thanks & Regards
KKPal
 
Old 05-12-2008, 07:31 AM   #5
elthox
Member
 
Registered: Oct 2006
Posts: 41

Rep: Reputation: 15
Quote:
Originally Posted by kkpal View Post
I had use diff.
diff test1.txt test2.txt > text.txt

but it wasn't work.

I need only difference not details

Thanks & Regards
KKPal
diff test1 test2|grep ">"|cut -d' ' -f2 > test3
diff test2 test1|grep "<"|cut -d' ' -f2 > test4
 
Old 05-12-2008, 07:40 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
elthox gave you the answer....
Quote:
and manipulate the output using > and < which indicate wo thich file the lines belong
Example: To get everything in file2 that is not in file1:
diff file1 file2 | grep ">"

Then use SED to remove the leading ">"

EDIT: Elthox beat me to the punch, AND with a better answer...

Last edited by pixellany; 05-12-2008 at 07:42 AM.
 
Old 05-12-2008, 07:52 AM   #7
elthox
Member
 
Registered: Oct 2006
Posts: 41

Rep: Reputation: 15
Quote:
Originally Posted by pixellany View Post
elthox gave you the answer....

Example: To get everything in file2 that is not in file1:
diff file1 file2 | grep ">"

Then use SED to remove the leading ">"

EDIT: Elthox beat me to the punch, AND with a better answer...
Is this better or my ignorance is just persisting

diff test1 test2|awk '$1=="<"&&$2!=""{print $2}' > test3 (the lines in test 1 and not in test2)

Last edited by elthox; 05-12-2008 at 07:58 AM.
 
Old 05-12-2008, 08:07 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Quote:
Originally Posted by elthox View Post
Is this better or my ignorance is just persisting

diff test1 test2|awk '$1=="<"&&$2!=""{print $2}' > test3 (the lines in test 1 and not in test2)
Now we're going to have an AWK vs SED debate???

"Persistent Ignorance".....I LIKE it...I think I can use this.....
 
  


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 numbers in a txt file leopard86 Programming 6 09-11-2012 01:10 AM
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 01:13 AM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 06:11 PM
Read a line in a txt file with bash orgazmo Programming 5 05-03-2005 08:10 AM
Read a line in a txt file with bash orgazmo Linux - Newbie 3 05-03-2005 05:16 AM


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