jask
jask

Reputation: 263

Compare two files and get only the differences - shell script

I have two files. The first one has a lot of ID and the second one has some ID too. I want to know which ID of the second file doesn't exist in the first one.

I've used this command:

diff -u total.log second.log | grep -E "^\+"

But I'm not really sure if it's the best way to do that.

Total.log

1087
1119
1121
1944
1951
1966
2148
2162
2169
2188
2216
2322
2393
2533
2748
2763
2766
2772
2779
2783
2787
2789
2793
2795
2798
2801
2842
2858
2868
2871
2873
2875
2887
2892
2897
2900
2901
2912
2918
2951
2957
2974
2975
2991
2993
3006
3007
3050
3066
3089
3102
3106
3119
3122
3124
3125
3151
3153
3164
3165
3171
3173
3174
3175
3229
3234
3244
3253
3265
3283
3302
3304
3305

Second.log

1116
1119
1121
1700
1928
1942
1947
1961
1968
1969
2170
2171
2752
2776
2801
2807
2808
2818
2829
2853
2884
2889
2897
2899
2901
2902
2913
2936
2970
2973
2974
3045
3167
3183
3185
3186
3235
3244
3247
3303
3306

Can anybody help me?

Thanks :)

Upvotes: 1

Views: 67

Answers (2)

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14945

comm is a nice tool if files are sorted.

comm -23 second.log total.log

Upvotes: 1

hek2mgl
hek2mgl

Reputation: 157927

grep can read patterns from a file using the option -f:

grep -vFf total.log second.log

-F tells grep that patterns aren't regex patterns - they are fixed length pattern. -v negates the match.

Upvotes: 2

Related Questions