-1

$ echo "${BASH_VERSION}" 5.1.4(1)-release
Linux 5.18.0-17.1-liquorix-amd64 x86_64 GNU/Linux

I have a string export LIBVIRT_DEFAULT_URI='qemu:///system' that I want to find in a user's ~/.profile and delete the entire line. I don't know a good way to do it. Normal use of sed or awk fails with errors and sadly I'm either too dumb or too lazy (or too both) to divine how to fix it.

Where $1 would be the filename and $2 is the string to search for. These things below normally work but seem to crap out in certain situations.

This doesn't work: awk "!/$2/" "$1" > "$1".tmp && mv "$1".tmp "1"
Neither does this: sed -i "/$2/d" $1
Nor this: awk -vLine="$2" "!index($0,$2)" file

Any advice and / or direction is appreciated. I really don't wanna use Perl if I don't have to but at this point I'll take whatever bitter pill that works. Thanks.

tripleee
  • 7,699
  • You need to escape the literal slashes, or use a different delimiter. grep -Fvx "$2" "$1" would also work. – tripleee Oct 31 '22 at 19:21
  • @tripleee, grep -Fvx "$2" "$1" doesn't work for values of $2 that start with - (also for values of $1 with GNU grep). – Stéphane Chazelas Oct 31 '22 at 19:25
  • 3
    Please don't add "update" sections since those are just confusing to everyone who hadn't read the original question. Instead, whenever you update your question, just write it as though the added information had been there since the beginning. All posts here have a history that is visible to everyone so if we need to see the original version we can go there. – terdon Oct 31 '22 at 22:00
  • That can be fixed by adding an -e option, as such. I think we can require the user to supply a path in $1 (like ./-filename) if the relative path starts with a dash. – tripleee Nov 01 '22 at 05:11
  • "Inputting a variable with string export LIBVIRT_DEFAULT_URI='qemu:///system' into a sourced function only returns the word export." -- this doesn't make any sense without knowing what the function does, and you haven't shown that here. Nor what it is you're trying to do anyway. – ilkkachu Nov 01 '22 at 14:19
  • Changing your question to ask about a completely different problem after you have received answers is not really acceptable. – tripleee Nov 01 '22 at 14:35
  • @tripleee I realized what my problem was so the question was no good. I tried and tried but couldn't delete the question. Sorry dude. – Wilhelm de Fheur Gorm Nov 01 '22 at 14:38
  • But you are vandalizing it by replacing the question which belongs with the answer so that it cannot be useful for future visitors; that's not doing anyone a service. – tripleee Nov 01 '22 at 14:38
  • @tripleee The original question itself was of no use, as indicated by the downvotes and commentary. I was asking the wrong question anyway and only realized later what the problem was. Trying to get the question deleted. – Wilhelm de Fheur Gorm Nov 01 '22 at 14:40

1 Answers1

1
#! /bin/sh -
if [ "$#" -ge 2 ]; then
  export LINE="${1?}"
  shift
  perl -ni -e 'print unless index($_, $ENV{LINE}) >= 0' -- "$@"
fi

Do be used as:

that-script "export LIBVIRT_DEFAULT_URI='qemu:///system'" file1 file2...

To remove the lines containing export LIBVIRT_DEFAULT_URI='qemu:///system' from file1, file2...

Instead of index($_, $ENV{LINE}) >= 0, you could use m{^\s*\Q$ENV{LINE}\E\s*$} to match on lines that consist of export LIBVIRT_DEFAULT_URI='qemu:///system' optionally preceded or followed by whitespace.

  • I just discovered that my problem lies with the word export itself, as when I pass the string to a sourced function as a variable it only returns the string export. – Wilhelm de Fheur Gorm Oct 31 '22 at 20:00
  • No, that just means you forgot to quote your variable somewhere. – tripleee Nov 01 '22 at 05:09
  • Not related to the question or answer but I've noticed that this site is not working properly. Actually there are more recent questions but they are not shown in the Questions Section. question 1, question 2 (and there are more questions) – Edgar Magallon Nov 01 '22 at 07:21
  • @EdgarMagallon, yes, I noticed it also says no active Q&A in the last 10 hours. https://stackstatus.net/ also seems to be down for me. – Stéphane Chazelas Nov 01 '22 at 07:31
  • @StéphaneChazelas It was weird that nobody asked in the last 10-11 hours. Only sometimes the questions appear in Q&A but after reloading the page these are not anymore (but I noticed that can be accessed by changing in the url questions/723169 for another number 723192 for example) – Edgar Magallon Nov 01 '22 at 07:50
  • @StéphaneChazelas would you delete your answer so I can delete my question? I realized I asked the wrong question. Thanks. – Wilhelm de Fheur Gorm Nov 01 '22 at 14:39