awk find and delete line from list

good day

i have a list of numbers in input.txt , i would like to compare to file.txt and delete the line that number appears in file.txt .

input.txt :

4558980
5525628
3595233
2650083
2219411
3529741
4675897
3070869
0014685
6365902

file.txt :

one-two-three-4558980.txt
one-two-three-5525628.txt
one-two-three-3595233.txt
one-two-three-2650083.txt
one-two-three-2219411.txt
one-two-three-3529741.txt
one-two-three-4675897.txt
one-two-three-3070869.txt
one-two-three-0014685.txt
one-two-three-6365902.txt

this works but i would like pattern to be each line of input.txt i would like to put it in a bash file

#!/bin/bash
#
#

awk '!/pattern/' file.txt > temp && mv temp file

thank you in advance

Why is awk a requirement here? Why not just use:

grep -f input.txt -v file.txt

thanks Don Cragun;
i was just coming back to fix that

---------- Post updated at 08:56 PM ---------- Previous update was at 08:44 PM ----------

sorry Don Cragun
that did not work , it ran in bash with out any errors but did not change
file.txt

---------- Post updated at 09:06 PM ---------- Previous update was at 08:56 PM ----------

sorry Don Cragun
that did not work , it ran in bash without any errors but did not change
file.txt

I was just talking about the awk part. You still have to capture the output produced and copy it back to the target file. I just didn't understand why part of your code was using file and part of it was using file.txt .

You could either use:

grep -f input.txt -v file.txt > file

if you want what your script was doing, or you could use:

grep -f input.txt -v file.txt > file && mv file file.txt

if you wanted to replace file.txt with its original contents minus the deleted lines.

Of course, having an example where all of your input is removed by the operation means that you could get the same results just using the command:

> file.txt
1 Like

Don
both lines of code produce the fie but it is blank. i deleted a couple #'s in the input.txt file so that the file should have lines in it and just not blank .

grep -f input.txt -v file.txt > file

produces a blank file called file and

grep -f input.txt -v file.txt > file && mv file file.txt

produces a blank file called file it doesn't mv file to file.txt
that is a little confusing to me , it should .
ubuntu mate 16.04 64bit

When I copy and paste your samples into a file it works fine. When I remove one of the lines of input.txt , the corresponding line in file.txt gets printed. Could you try that yourself?

Also, make sure there are no empty lines in input.txt

1 Like

Scrutinizer and Don Cragun i'm not sure what i did but after rebooting i tried it again and it did work. thank you

I'm glad it's working for you now.

Note that if you expect all lines in file.txt to be matched by a line in input.txt and you want file.txt to be an empty file in that case; change the command list to be:

grep -f input.txt -v file.txt > file; [ $? -le 1 ] && mv file file.txt

You need to do this because the exit code from grep is 1 if no input lines are copied to the output file, but no errors occurred. (This is why file wasn't moved to file.txt when file was empty.)

1 Like