0

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

3
  • 1
    Why use / as a delimiter when your pattern contains lots of slashes already? That's asking for trouble. I'd suggest you use something else, say, comma. It will make the sed invokations much more readable. Commented Apr 22, 2014 at 0:02
  • @fstd some things don't seem to work well when you don't use slashes, though. Such as @PATTERN@d doesn't seem to work. Commented Apr 22, 2014 at 0:05
  • 1
    @AlexejMagura that's be \@PATTERN@d (note the escaping of the first @). Slash seesm to be special in that the escape can be omitted, but generally sed expects it to be there. Commented Apr 22, 2014 at 0:12

2 Answers 2

1

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"'/'
Sign up to request clarification or add additional context in comments.

3 Comments

Tried that also;sed: -e expression #1, char x: unknown option to `s'
works fine for me. What version of sed are you using? NOTE some things may need to be escaped if you're using double quotes!
Ah, yes. One must be wary of that. But you guys got me sorted on this one, thx again. GNU bash, version 4.2.45(1)-release (i686-pc-linux-gnu)
0

@fstd's answer. Change the delimiter.

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

2 Comments

Whoa, why would choice of separator style affect functionality? Hunh. Anyhow, with double quotes around the whole sed, no quotes around the variable and using comma as changed seperator, this does the job. Tried to show it in here but these comments mangle code ;(
@MountainMan it only really helps when working with a pattern that contains the delimiter you're using, such as: sed 's/\/var\/log//' as sed 's_/var/log__'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.