0

I have two files with contents of some words.

Example

file1 it has 10 words:

world
earth
eye
ear
near 
from 
going
want
we 
our

Second file2 has 7 words:

world
earth
eye
ear
near 
going
want

I want output as third file for the words which does not exist in file2.

for example (from , our , we) does not exist in file2.

1
  • Can you give example of desired output? Commented Oct 30, 2017 at 7:35

2 Answers 2

3

Just do

grep -vFxf file2 file1 > file3

This will return those lines that doesn't exist in file2 but in file1 and write the result to file3.

  • -v, reverse match, here means those lines if exist only in file2. Without -v it will return those lines which are exist in both files.

  • -F, this is telling grep to match the pattern as a fixed pattern string instead of regex (regular expressions)

  • -x, matches the whole line as pattern string

  • -f, reading the patterns from a file

Or per your question's title and the command you referred to that sort -u, seems you want those unique words (actually lines) either exist in the file1 or file2. then you just need.

uniq -u <(sort file1 file2) > file3
1
  • 1
    If you wanted the lines that are unique in file1 and file2, that would be sort file1 file2 | uniq -u. Note that using -w here only does what you want/say if there's only one word per line (and no non-word character), so you might as well use -x instead of -w. Commented Oct 30, 2017 at 8:23
1

Another solution:

comm -23 <(sort file1) <(sort file2) > file3

The -23 will suppress the rows contained in either only file2 or in both files.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.