Search code examples
bashvariablessedmultilinepacman-package-manager

Deleting multiline variable content from a file


I am trying to write a Bash script handling Pacman repositories in /etc/pacman.conf usable within a script without depending on the user interaction required by a text editor.

I am having troubles figuring out the remove-option. How do I delete multiple lines or a paragraph from a file based on the content of a multiline variable?

So I have this multiline variable

echo "$repo_query_result"

and its content looks for example like this

[repo-name]
Server = https://host.domain/$arch

or for example like this for a disabled repo

#[repo-testing]
#SigLevel = PackageRequired
#Include = ./repo-2-mirrorlist

When I try the following command which works with single-line variables

sed "/$repo_query_result/d" /etc/pacman.conf

I get the following error

sed: -e expression #1, char 6: unterminated address regex

All I could find regarding sed or awk and multiline variables where solutions on how to delete a single line from the variable. Or when it comes to deleting paragraphs it was always a fixed pattern, e.g. the same start and end word, which is not suitable for my case.

EDIT1: The variable is the result of a function within the script and not set manually or derived from another file. So altering the variable is not an option

repo_query()
{
sed "/$name/,/+$/!d" $conff | sed '/^$/,/+$/d'
}
repo_query_result="$(repo_query | cat /dev/stdin)"

Solution

  • Pure bash, using pattern substitution:

    conf=$(<"in.cf")
    echo "${conf/"$repo_query_result"}" >"out.cf"