1

I thing I'm too close to the problem already that I just can solve it on my own, alltough I'm sure it's easy to solve.

I'm working on a NAS with a SHELL Script for my Raspberry PI which automaticly collects data and distributes it over my other devices. So I decided to include a delete-option, since otherwise it would be a pain in the ass to delete a file, since the raspberry would always copy it right back from the other devices. While the script runs it creats a file: del_tmp_$ip.txt in which are directorys and files to delete from del_$ip.txt (Not del_TMP_$ip.txt). It looks like this:

test/delete_me.txt
test/hello/hello.txt
pi.txt

I tried to delete the lines viá awk, and this is how far I got by now:

while read r; do
 gawk -i inplace '!/^'$r'$/' del_$ip.txt
done <del_tmp_$ip.txt

If the line from del_tmp_$ip.txt tells gawk to delete pi.txt it works without problems, but if the string includes a slash like test/delete_me.txt it doesn't work:

"unexpected newline or end of string"

and it points to the last slash then.

I can't escape the forwardslash with a backwardslash manually, since I don't know whether and how many slashes there will be. Depending on the line of the file which contains the information to be deleted.

I hope you can help me!

  • I think you can simply use `grep -Fvxf 'del_tmp_$ip.txt' 'del_$ip.txt'` – Sundeep Jul 04 '17 at 10:20
  • Actually that worked. Just as I said, I was trying to work out that problem for so long that I couldnt see another solution. Thank you! – Lukas Herfeld Jul 04 '17 at 11:08
  • Possible duplicate of [Remove Lines from File which appear in another File](https://stackoverflow.com/questions/4366533/remove-lines-from-file-which-appear-in-another-file) – Sundeep Jul 04 '17 at 11:20

1 Answers1

0

Never allow a shell variable to expand to become part of the awk script text before awk evaluates it (which is what you're doing with '!/^'$r'$/') and always quote your shell variables (so the correct shell syntax would have been '!/^'"$r"'$/' IF it hadn't been the wrong approach anyway). The correct syntax to write that command would have been

awk -v r="$r" '$0 !~ "^"r"$"' file

but you said you wanted a string comparison, not regexp so then it'd be simply:

awk -v r="$r" '$0 != r' file

and of course you don't need a shell loop at all:

while read r; do
 gawk -i inplace '!/^'$r'$/' del_$ip.txt
done <del_tmp_$ip.txt

you just need 1 awk command:

gawk -i inplace 'NR==FNR{skip[$0];print;next} !($0 in skip)' "del_tmp_$ip.txt" "del_$ip.txt"
Ed Morton
  • 188,023
  • 17
  • 78
  • 185