ayasha
ayasha

Reputation: 1251

delete lines with specific pattern

Hi I have to delete some lines in a file:

file 1
1 2 3
4 5 6

file 2
1 2 3 6
5 7 8 7
4 5 6 9

I have to delete all the lines of file 1 that i find in file 2:

output
5 7 8 7

I used sed:

for sample_index in $(seq 1 3)
do
  sample=$(awk 'NR=='$sample_index'' file1)
  sed "/${sample}/d" file2 > tmp
done

but it doesnt work.it doesn't print anything. do you have any idea?It gives me error of 'sed: -e expression #1, char 0: precedent regular expression needed'

Upvotes: 3

Views: 137

Answers (3)

captcha
captcha

Reputation: 3756

And the code for GNU

sed -r 's#(.*)#/\1/d#' file1 | sed -f - file2

Upvotes: 2

jaypal singh
jaypal singh

Reputation: 77095

This should work if your real data as 3 columns:

awk 'NR==FNR{a[$1$2$3]++;next}!($1$2$3 in a)' file{1,2}

For variable columns:

awk 'NR==FNR{a[$0]++;next}{for(x in a) if(index($0,x)>0) next}1' file{1,2}

Upvotes: 3

Lev Levitsky
Lev Levitsky

Reputation: 65791

This could be a start:

$ grep -vf file1 file2
5 7 8 7

One potential pitfall here is that the output won't change if you put 5 6 9 as the second line of file1. I'm not sure if if you want that or not. If not, you can try

grep -vf <(sed 's/^/^/' file1) file2

Upvotes: 4

Related Questions