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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SED and variable with a slash 2

Status
Not open for further replies.

RFC1795

IS-IT--Management
Feb 13, 2003
76
US
Hi,

I rasied this problem of mine in another forum but never got an answer as yet.

My problem is, how do I get away with using a variable in sed that contains a slash as in the following example:
var1="ethernet0/0"
sed -n '/$var1/,/whatever/p'

PHV helped me out with another sed senario for replacing text by using exclamation points in place of the slashes that I'm used to.
(sed '1,$s!'"$var1!TEST!g")
I've been unable to figure out how to do it in my above example.Its probably because I don't understand the significance of the !'s ;-)

Thanks .. TJ
 
Try this:
Code:
var2=`echo $var1 | sed -e 's!/!\\/!g'`
sed -n "/$var2/,/whatever/p"
Anyway,
Code:
man sed
For the s command, you can use any char as delimiter.

Hope This Help
PH.
 
Try this :

var1="ethernet0/0"
sed -n '\!$var1!,/whatever/p'

Extract from sed man page :

You can select the character delimiter for patterns. The general form of the
expression is:
\?pattern?

where ? (question mark) is a selectable character delimiter. You can select
any character from the current locale except for the space or new-line
character. The \ (backslash) character is required only for the first
occurrence of the ? (question mark).

The default form for the pattern is the following:
/pattern/

A \ (backslash) character is not necessary.



Jean Pierre.
 
Thanks for the tips. I get the picture now on the deliminator characters now.
However ;-) performing the command works fine without using the variable substitution. As soon as I introduce the variable then it doesn't work:

eg:
cat config.txt | sed -n '\?FastEthernet0/1?,/cdp/p'
Will correctly output the following :
interface FastEthernet0/1
description Port Not Used
no ip address
shutdown
duplex full
speed 100
no cdp enable

Replacing the string above "FastEthernet0/1" with $var1 and having var1="FastEthernet0/1" at the start gives me no output.
It now looks like this as I test on command line:
var1="FastEthernet0/1" ; cat config.txt | sed -n '\?$var1?,/cdp/p' ; echo $var1

No ouput.

Any idea what I'm doing wrong here?

Thanks again .. TJ
 
yeah, you're using SINGLE quotes in sed.
use DOUBLE quotes instead.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Put your sed command in double quotes instead of single quotes. Single quotes turns off the meaning of the special character, dollar sign. This is what I always do, I dont know if there is a better answer.
 
Within simple quote ' the shell doesn't perform variable substitution. Use double quote &quot;
[tt]
var1=&quot;FastEthernet0/1&quot;
cat config.txt | sed -n &quot;\?$var1?,/cdp/p&quot;
[/tt]
or
[tt]
var1=&quot;FastEthernet0/1&quot;
sed -n &quot;\?$var1?,/cdp/p&quot; config.txt
[/tt]

Jean Pierre.
 
Wow!! It works :) .. Something as simple as a quote problem hehe

Thank you all very kindly

Cheers .. TJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top