Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

substitution of variable in script invoking sed 1

Status
Not open for further replies.

Calator

Programmer
Feb 12, 2001
262
AU
Hi,

In a KSH script I have following code, which checks inside a file for the word "Report:" and extracts the report id which follows this tag:

reportid=`grep -i "Report:" ${in_file} | sed -e 's/.*Report:[ ]*\([A-Za-z0-9]*\).*/\1/g;;q'`

The search is repeated for various other literal "tags".

I would like to use variables instead of the literals "Report:" etc, so that the literal is not repeated and the code will be more maintanable and elegant:

tag1="Report:"
tag2= ...

reportid=`grep -i "${tag1}" ${in_file} | sed -e 's/.*"${tag1}"[ ]*\([A-Za-z0-9]*\).*/\1/g;;q'`

My problem is that while grep works ok with the variable, as long as it is enclodes by quotes as above, sed no longer worked. I have tried various ways with or without quotes, escaping the dollar sign, etc.

Any suggestions? Thanks!
 
You may try something like this:
reportid=`sed -n 's/.*'"${tag1}"'[ ]*\([A-Za-z0-9]*\).*/\1/p' ${in_file}`

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top