sed with variables

Im trying to use sed to print value that matches the value in variable and all lines after that.

grep "Something" test.txt | sed -e '/{$variable}/,$b' -e 'd'

I cant get it work, if I replace the $variable with the value it contains, it works fine...

Use double quotes instead of single ones to expand variables to it's value.

Regards
Peasant.

---------- Post updated at 02:15 AM ---------- Previous update was at 02:15 AM ----------

Use double quotes instead of single ones to expand variables to it's value.

Regards
Peasant.

Now I got error saying
expected newer version of sed

btw, is there way doing this with awk?

Can you post your operating system ?

Debian Squeeze

Can you please post input (test.txt) and $variable and $b values and desired output ?

Regards
Peasant.

use the below instead of sed

 
awk -v v="$variable" 'a;$0~v{print;a=1}'
1 Like

Hi,

Try this one,

awk -v pat="$var" '$0~pat{a=1}a' file

Cheers,
Ranga:)

Your basic problem is, that variables inside single quotes ' will not be substituted by the shell. Additionally the grep could be done by the sed as well.

Here someone had the same problem:

Thanks, this works fine. I have one more question:

Is it possible to post all the lines after the match but not the matching line?

just remove the print;

1 Like