Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print lines from one file that are not contained in another file

Tags:

I wish to print lines that are in one file but not in another file. However, neither files are sorted, and I need to retain the original order in both files.

 contents of file1:  string2  string1  string3   contents of file2:  string3  string1   Output:  string2 

Is there a simple script that I can accomplish this in?

like image 840
j.lee Avatar asked Apr 28 '11 01:04

j.lee


People also ask

How do you print the lines from a file which does not contain the search string?

You can add the -l option to print just the file name; but this still prints the names of any file which contains any line which does not contain the pattern.

How do you get 3rd element from each line from a file?

To get third column in tab delimited file, you can simply call it as cut -f3 <file> . Different delimiter can be passed via -d parameter, e.g.: cut -f3 -d: . You can even get multiple columns, or characters at fixed positions on the line.

How do I grep all lines in a file?

To search multiple files with the grep command, insert the filenames you want to search, separated with a space character. The terminal prints the name of every file that contains the matching lines, and the actual lines that include the required string of characters. You can append as many filenames as needed.


1 Answers

fgrep -x -f file2 -v file1 

-x match whole line

-f FILE takes patterns from FILE

-v inverts results (show non-matching)

like image 157
Charles Brunet Avatar answered Oct 17 '22 19:10

Charles Brunet