1

file1 contains:

someword0
someword2
someword4
someword6
someword8
someword9
someword1

file2 contains:

someword2
someword3
someword4
someword5
someword7
someword11
someword1

So I wan't to have only lines from file1 which file2 doesn't contains. How can I do this in bash ?

That's the answer:

grep -v -x -f file2 file1

    -v for select non-matching lines
    -x for matching whole lines only
    -f f2 to get patterns from f2.
devbgs
  • 67
  • 2
  • 7
  • possible duplicate of [Remove Lines from File which appear in another File](http://stackoverflow.com/questions/4366533/remove-lines-from-file-which-appear-in-another-file) – tripleee Oct 05 '14 at 17:44
  • Better duplicate: http://stackoverflow.com/questions/4780203/deleting-lines-from-one-file-which-are-in-another-file – tripleee Oct 05 '14 at 17:47
  • Yeah. I searching in google but I haven't found this. Thanks. – devbgs Oct 05 '14 at 17:50

3 Answers3

1

You can use grep -vf:

grep -vwFf file2 file1
someword0
someword6
someword8
someword9

Check man grep for detailed info on all the grep options used here.

anubhava
  • 761,203
  • 64
  • 569
  • 643
1

You could use the comm command as well:

comm -23 file1 file2

Explanation:

comm compares two files and prints, in 3 columns, lines unique to file1, file2, and lines in both.

Using the options -2 and -3 (or simply -23) suppresses printing of these columns, so you just get the lines unique to file1.

Travis
  • 2,579
  • 18
  • 19
0

If your lines are unique, do a left join and filter out lines that exist in both tables.

join <(sort file1) <(sort file2) -o0,2.1 -a1 | awk '!$2'
Vytenis Bivainis
  • 2,308
  • 21
  • 28