LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   sed block search with variables (https://www.linuxquestions.org/questions/linux-software-2/sed-block-search-with-variables-867160/)

Mario Blunk 03-08-2011 04:40 AM

sed block search with variables
 
Hi Folks,

1) extracting a block of text from file1 to file2 works fine this hardcoded way:

sed -n '/DW/,/;/p' file1.tmp > file2.tmp

2) Now I need it more flexible using a shell variable:

p=DW

sed -n '/$p/,/;/p' file1.tmp > file2.tmp

But the file2.tmp stays empty. What is wrong ? Thank you all.

colucix 03-08-2011 04:55 AM

The single quotes prevent shell substitution and the string $p is passed literally to sed and interpreted with its own rules. To let the shell make the substitution and expand the value of the variable, use double quotes.

Mario Blunk 03-09-2011 01:51 AM

double quotes
 
Colucix,

thanks for your reply. I tried several double quotes but no joy. How should that look like in my case ?

cheers from Germany :-)

colucix 03-09-2011 03:23 AM

This works on my system:
Code:

sed -n "/$p/,/;/p" file
using GNU sed version 4.1.5.

Mario Blunk 03-10-2011 07:37 AM

Thank you ! That is the solution. Have a great day. :-)


All times are GMT -5. The time now is 07:01 AM.