0

Start with this script:

#!/bin/bash
line1="value1"
line2="value2"
< input_file sed -p "/$line1/,/$line2/p"

And it works fine. It prints out only those lines between $line1 and $line2.

But then make this change:

line1="value1/something"

And sed complains with an error because of the $line1 containing a slash.

What I would like to be able to do is simply change the delimiter:

#!/bin/bash
line1="value1"
line2="value2"
< input_file sed -p "%$line1%,%$line2%p"

And NOT have to do any Bash scripting on $line1 or $line2 such as this hackaround:

line1=$(echo "value1/something" | sed 's%/%\\/%g')

I want to do this all within sed, not with anything else such as awk, tr, etc, as I already know how to do that. If that is simply not possible within sed syntax, then that is a viable answer.

Update: sed version: sed (GNU sed) 4.5

bgoodr
  • 2,744
  • 1
  • 30
  • 51
  • Yes indeed the answer is here: https://stackoverflow.com/a/5864155/257924. e.g.,: `(echo 'foo'; echo 'bar/baz') | sed '\%/%{ s%^%X%g; }'` returns `foo` and then `Xbar/baz`. The key is using the backslash in front of the delimiter in the range specifier. – bgoodr Mar 13 '23 at 00:01

0 Answers0