LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   how to show only unique words between two files (https://www.linuxquestions.org/questions/linux-general-1/how-to-show-only-unique-words-between-two-files-864454/)

Dr_Death_UAE 02-23-2011 01:46 AM

how to show only unique words between two files
 
Hi, i have to files
Quote:

File1:
Hi, My name is John smith
Quote:

File2:
Hi, My name is smith
I want to show only the different or unique word between the two files (John)

i was trying to get it with diff, comm, sort, uniq, grep, awk, sed

but no luck, if anyone have an idea how to do it please advice

Thanks

EricTRA 02-23-2011 02:03 AM

Hello,

Have a look at wiggle. This tool can do word based comparison. The different word will be put between <<<-- word-->>>.

Kind regards,

Eric

Dr_Death_UAE 02-23-2011 02:26 AM

thanks EricTRA, but is there a way to do it with shell commands

EricTRA 02-23-2011 02:32 AM

Hello,

I'm sure there will be but it will involve some scripting, like in the following thread I found, which has a Python and Awk example. Is there a special reason why you don't want to install a tool but prefer writing a script? Besides the obvious reason of learning?

Kind regards,

Eric

akelder 02-23-2011 03:10 AM

This is probably goofy, but you could put every word on it's own line:

Code:

$ cat file1
Hi, My name is John smith

$ cat file2
Hi, My name is smith

$ sed 's/ /\n/g' file1 > file1.tmp
$ sed 's/ /\n/g' file2 > file2.tmp

$ comm -3 file1.tmp file2.tmp
John

$ sort file1.tmp file2.tmp | uniq -u
John


Dr_Death_UAE 02-23-2011 05:18 AM

i want to include this code in a shell script that will be use in a different machines, it's better to make it run with the basic linux shell commands.

thanks akelder, this what i was looking for, i pipe the commands and it work fine:

Quote:

$ cat file1 file2 | sed 's/ /\n/g' | sort | uniq -u
john


All times are GMT -5. The time now is 12:27 PM.