MountainMan
MountainMan

Reputation: 785

BASH scripting, proper quoting of variable inside sed?

This variable, Btr="/home/BB/PL/DDr/"; is successfully echoed so I know it should be available, but none of these sed work properly.

Variants of syntax I have tried, none of which work.

1.

    Lg=$(echo "${Prg}" | sed 's/\(\/home\/in\/PL\/\)\(.*_Data.txt\)$/'$Btr'\2/');

2.

    Lg=$(echo "${Prg}" | sed 's/\(\/home\/in\/PL\/\)\(.*_Data.txt\)$/'${Btr}'\2/');  

3.

    Lg=$(echo "${Prg}" | sed 's/\(\/home\/in\/PL\/\)\(.*_Data.txt\)$/"$Btr"\2/');  

4.

    Lg=$(echo "${Prg}" | sed 's/\(\/home\/in\/PL\/\)\(.*_Data.txt\)$/"${Btr}"\2/');  

5.

    Lg=$(echo "${Prg}" | sed 's/\(\/home\/in\/PL\/\)\(.*_Data.txt\)$/'"$Btr"'\2/');  

6.

    Lg=$(echo "${Prg}" | sed 's/\(\/home\/in\/PL\/\)\(.*_Data.txt\)$/"'$Btr'"\2/');  

7.

    Lg=$(echo "${Prg}" | sed -e "s/\(\/home\/in\/PL\/\)\(.*_Data.txt\)$/'${Btr}'\2/");  

8.

    Lg=$(echo "${Prg}" | sed -e 's/\(\/home\/in\/PL\/\)\(.*_Data.txt\)$/'${Btr}'\2/');  

Objective is to change a line like this one:

| tee -a /home/in/PL/SomeFile_Data.txt

to this:

| tee -a /home/BB/PL/DDr/SomeFile_Data.txt

Upvotes: 0

Views: 92

Answers (2)

Brad Lanam
Brad Lanam

Reputation: 5723

@fstd's answer. Change the delimiter.

  Lg=$(echo ${Prg} | sed 's,/home/in/PL/\(.*_Data\.txt\)$,'$Btr'\1,')

Upvotes: 0

Alexej Magura
Alexej Magura

Reputation: 5119

Without double quotes:

a=1
echo 'b' | sed 's/b/'$a'/'

With double quotes:

a=1
echo 'b' | sed "s/b/$a/"

With single quoted pattern and double quoted variable:

a=1
echo 'b' | sed 's/b/'"$a"'/'

Upvotes: 1

Related Questions