Substraction of matching lines from a file.

I have 2 files:
file1.txt contains

/html/mybook/Charts/143712/reptiles.pdf
/html/mybook/Charts/198459/spices.pdf
/html/mybook/Charts/198459/fresh_nuts.pdf
/html/mybook/Charts/123457/dome_anim.pdf
/html/mybook/Charts/123457/vegetables.pdf
/html/content/3DInteractive/174091/CSPSGGB.html

file2.txt contains

/html/mybook/Charts/123457/
/html/content/3DInteractive/174091/

I need out put file1.txt as (without creating a new file).

/html/mybook/Charts/143712/reptiles.pdf
/html/mybook/Charts/198459/spices.pdf
/html/mybook/Charts/198459/fresh_nuts.pdf

file1.txt should contain the lines after the subtraction of lines in file2.txt, It will be very helpful for me, if you have a solution for this.

Try:

grep -vf file2 file1

To put the changes in the original file, create a new file and if the result is OK replace the original with the new file..

1 Like

But this doesn't meets my requirement.
In my case the file1.txt file should be the out put after the subtraction of the lines as (file1.txt-file2.txt=file1.txt).

You can probably do this if it is okay to create another file and then move it to original file

grep -vf file2 file1 >file3
mv file3 file1
1 Like

But my intention is with out creating a new file like file3.txt.

You could copy the input file first and use copy as input and create the original file as output..

also, try something like:

awk 'NR==FNR {a[$0]=$0; next; } {s=$0; sub("[^/]*$","", s); if (!a) print}' file2.txt file1.txt |&
wait
while read -p x ; do echo "$x" ; done > file1.txt