How can i delete the contents of one file based on another file

Hi all,

I have two log files A and B. Where files A have 10 lines and file b has 5 lines.

Out of them 3 lines are common to both of them. I want to compare both the files and want to delete the common data from file A(file B should remain as it is)

example :

file A :
 
1
2
3
4
5
6
7
8
9
11
 
file B :
A
2
25
1
7

AND THE OUTPUT SHOULD BE

file A :
 
3
4
5
6
8
9
11
 
file B :
A
2
25
1
7

Something like this :

awk 'NR==FNR { a[$0]=$0 ;next } { if($0 in a) {a[$0]="";}} END { for ( i in a) print a }' file1 file2 

or use grep :

grep -v -f fileB fileA

Yes ..Your right , no need to go for awk , if we can do in a simple grep statement.

My mind always strikes to work using awk .. :slight_smile: :slight_smile:

Hi Panyam/thanhdatI am unable to use grep -v -fas it is giving me the error below.

grep: illegal option -- fUsage: grep -hblcnsviw pattern file . . .

And the awk that has been specified is giving a blank space once the lines are deleted and the data is not coming in the same order as it is originally. For example, last line is coming inthe middle and vice versa. In my script, it should not change the order of the lines in the output.Please help. And let me know if you need more details.I am using SUN OS box.

Not sure , why grep -v -f option is not working .

Try some thing like this :

awk 'NR==FNR { a[$0]=$0;b[++n]=$0;next } { if($0 in a) {a[$0]="";}} END { for (i=1;i<=n;i++) print a [b]}' file1 file2 | sed '/^$/d'