Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'

like image 523
ayasha Avatar asked May 20 '26 15:05

ayasha


1 Answers

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
like image 112
Lev Levitsky Avatar answered May 22 '26 04:05

Lev Levitsky