Operating System - HP-UX
1828366 Members
2861 Online
109976 Solutions
New Discussion

Re: Help with SED and variable for regexpr

 
SOLVED
Go to solution
dev44
Regular Advisor

Help with SED and variable for regexpr

Hi,

I want to be able to find a string and then print the next line. The string is a variable. If I hard code it in the below line, it works but it won't take a variable. I have tried double quotes; single quotes; back slashes, etc. Maybe not the right combo or syntax though.

cat file|sed -n '/$VAR/{n;p;}'
whatever
3 REPLIES 3
James R. Ferguson
Acclaimed Contributor
Solution

Re: Help with SED and variable for regexpr

Hi:

You need double quotes to allow the shell to expand the variable:

# VAR=whatever
# sed -n "/$VAR/{n;p;}" file

Notice that the 'cat' into a pipe is superfluous. The 'sed' reads STDIN just fine, saving you a process.

Regards!

...JRF...
dev44
Regular Advisor

Re: Help with SED and variable for regexpr

Yay...thanks!!
whatever
Dennis Handly
Acclaimed Contributor

Re: Help with SED and variable for regexpr

Note: GNU grep has a -A # option to print context around your match.