Locate and remove lines with sed

Gents,

I would like to remove some lines from a big file ( file2).

The objetive is to remove all the lines in file2 containing a certain string which are in file data2delete..

file data2delete contens:

2573.0  7260.01
2893.0  7255.01
2903.0  7245.01
2897.0  7255.01
2561.0  7255.01

file2

A          2915.0  7325.01212 75   2   5248969 78 46
A          2915.0  7325.012 7 75   2   5216571 80 44
A          2555.0  7255.013 9 75   1   2163071 78 59
A          2555.0  7255.013 5 75   1  -3162871 77 52
A          2893.0  7255.011 7 75   2   6205870 77 54
A          2893.0  7255.011 1 75   1  -2142671 76 62
A          2903.0  7245.01211 75   1   3227270 77 54
A          2903.0  7245.012 4 75   1   3185571 78 64
A          2571.0  7260.01410 75   1  -2204470 77 41
A          2571.0  7260.014 6 75   2   5182570 77 37
A          2557.0  7255.013 9 75   1   5153071 77 67
A          2557.0  7255.013 5 75   1  -3162871 77 42
A          2895.0  7255.011 7 75   1  -3203071 77 41
A          2895.0  7255.011 1 75   1  -2174671 77 56
A          2573.0  7260.01410 75   1   2259968 77 38
A          2573.0  7260.014 6 75   2   5268468 77 35
A          2901.0  7245.01211 75   1   4184671 79 56
A          2901.0  7245.012 4 75   1   3195171 77 43
A          2559.0  7255.013 9 75   2   5255268 77 42
A          2559.0  7255.013 5 75   2   9309067 76 30
A          2897.0  7255.011 7 75   2   4227270 78 47
A          2897.0  7255.011 1 75   1  -2153470 76 53
A          2575.0  7260.014 6 75   2   5162670 76 40
A          2575.0  7260.01410 75   2   9279767 77 41
A          2899.0  7245.01211 75   1  -2152872 77 62
A          2899.0  7245.012 4 75   1  -2142771 78 45
A          2561.0  7255.013 9 75   3  21319165 77 46
A          2561.0  7255.013 5 75   2   9309967 78 35
A          2899.0  7255.011 7 75   1   4142771 77 55
A          2899.0  7255.011 1 75   1  -2214770 77 42

Thanks for your help

I like sed a lot, but I see ugliness in trying to get values from one file and use them to delete values in another file using sed.

I think:

grep -vFf data2delete file2

may work well.

It returns the following:

A          2915.0  7325.01212 75   2   5248969 78 46
A          2915.0  7325.012 7 75   2   5216571 80 44
A          2555.0  7255.013 9 75   1   2163071 78 59
A          2555.0  7255.013 5 75   1  -3162871 77 52
A          2571.0  7260.01410 75   1  -2204470 77 41
A          2571.0  7260.014 6 75   2   5182570 77 37
A          2557.0  7255.013 9 75   1   5153071 77 67
A          2557.0  7255.013 5 75   1  -3162871 77 42
A          2895.0  7255.011 7 75   1  -3203071 77 41
A          2895.0  7255.011 1 75   1  -2174671 77 56
A          2901.0  7245.01211 75   1   4184671 79 56
A          2901.0  7245.012 4 75   1   3195171 77 43
A          2559.0  7255.013 9 75   2   5255268 77 42
A          2559.0  7255.013 5 75   2   9309067 76 30
A          2575.0  7260.014 6 75   2   5162670 76 40
A          2575.0  7260.01410 75   2   9279767 77 41
A          2899.0  7245.01211 75   1  -2152872 77 62
A          2899.0  7245.012 4 75   1  -2142771 78 45
A          2899.0  7255.011 7 75   1   4142771 77 55
A          2899.0  7255.011 1 75   1  -2214770 77 42
1 Like

Thanks for your help..

If you know a way using awk and sed ,, please let me know... I like to learn

---------- Post updated 11-06-12 at 02:47 AM ---------- Previous update was 11-05-12 at 01:38 PM ----------

Some one can help me with sed

With awk:

awk 'FNR==NR{a[$0];next}{p=1;for(i in a) if($0~i) {p=0;break}}p' data2delete file2
2 Likes

Or:

awk 'FNR==NR{A[$0];next} !(substr($0,12,15) in A)' data2delete file2
1 Like

Many Thanks to all

---------- Post updated at 06:30 AM ---------- Previous update was at 03:28 AM ----------

Dear alls,

All codes works fine, but I notice that they deleted only one file in file2. I mean one time the value found in file data2delete?.

I would like to delete all the lines not only one line?,

FYI, the file2 has 2 time the same value repetead.. for that I would like to delete both lines

Example

data2delete

2915.0  7325.01
2893.0  7255.01

file2

A          2915.0  7325.01212 75   2   5248969 78 46
A          2915.0  7325.012 7 75   2   5216571 80 44
A          2555.0  7255.013 9 75   1   2163071 78 59
A          2555.0  7255.013 5 75   1  -3162871 77 52
A          2893.0  7255.011 7 75   2   6205870 77 54
A          2893.0  7255.011 1 75   1  -2142671 76 62
A          2903.0  7245.01211 75   1   3227270 77 54
A          2903.0  7245.012 4 75   1   3185571 78 64

output

A          2555.0  7255.013 9 75   1   2163071 78 59
A          2555.0  7255.013 5 75   1  -3162871 77 52
A          2903.0  7245.01211 75   1   3227270 77 54
A          2903.0  7245.012 4 75   1   3185571 78 64

Thanks for your help

---------- Post updated at 06:30 AM ---------- Previous update was at 06:30 AM ----------

Dear alls,

All codes works fine, but I notice that they deleted only one file in file2. I mean one time the value found in file data2delete?.

I would like to delete all the lines not only one line?,

FYI, the file2 has 2 time the same value repetead.. for that I would like to delete both lines

Example

data2delete

2915.0  7325.01
2893.0  7255.01

file2

A          2915.0  7325.01212 75   2   5248969 78 46
A          2915.0  7325.012 7 75   2   5216571 80 44
A          2555.0  7255.013 9 75   1   2163071 78 59
A          2555.0  7255.013 5 75   1  -3162871 77 52
A          2893.0  7255.011 7 75   2   6205870 77 54
A          2893.0  7255.011 1 75   1  -2142671 76 62
A          2903.0  7245.01211 75   1   3227270 77 54
A          2903.0  7245.012 4 75   1   3185571 78 64

output

A          2555.0  7255.013 9 75   1   2163071 78 59
A          2555.0  7255.013 5 75   1  -3162871 77 52
A          2903.0  7245.01211 75   1   3227270 77 54
A          2903.0  7245.012 4 75   1   3185571 78 64

Thanks for your help

All the solution's provided works perfect.

but i prefer rwuerth's solution. grep -vf

$ cat file2
A          2915.0  7325.01212 75   2   5248969 78 46
A          2915.0  7325.012 7 75   2   5216571 80 44
A          2555.0  7255.013 9 75   1   2163071 78 59
A          2555.0  7255.013 5 75   1  -3162871 77 52
A          2893.0  7255.011 7 75   2   6205870 77 54
A          2893.0  7255.011 1 75   1  -2142671 76 62
A          2903.0  7245.01211 75   1   3227270 77 54
A          2903.0  7245.012 4 75   1   3185571 78 64

$ cat file1
2915.0  7325.01
2893.0  7255.01

$ grep -vf file1 file2
A          2555.0  7255.013 9 75   1   2163071 78 59
A          2555.0  7255.013 5 75   1  -3162871 77 52
A          2903.0  7245.01211 75   1   3227270 77 54
A          2903.0  7245.012 4 75   1   3185571 78 64
1 Like