LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash command for make difference between two txt file (https://www.linuxquestions.org/questions/linux-newbie-8/bash-command-for-make-difference-between-two-txt-file-641614/)

kkpal 05-12-2008 06:13 AM

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

elthox 05-12-2008 06:22 AM

Quote:

Originally Posted by kkpal (Post 3150673)
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

pixellany 05-12-2008 06:24 AM

Take a look at "cmp" and "diff"

kkpal 05-12-2008 06:28 AM

Quote:

Originally Posted by elthox (Post 3150682)
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

elthox 05-12-2008 06:31 AM

Quote:

Originally Posted by kkpal (Post 3150685)
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

pixellany 05-12-2008 06:40 AM

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...

elthox 05-12-2008 06:52 AM

Quote:

Originally Posted by pixellany (Post 3150698)
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)

pixellany 05-12-2008 07:07 AM

Quote:

Originally Posted by elthox (Post 3150704)
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.....;)


All times are GMT -5. The time now is 02:10 AM.