Re: change character , when a string occurs ( possible one liner ) (351 Views)
Reply
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 1 of 5 (387 Views)
Accepted Solution

change character , when a string occurs ( possible one liner )

hello,

 

i want to remove a string in a variable only when variable occurs  like "/dev" .

maybe the input for a variable include "/dev/vgname" or only "vgname".

 

vg_name=/dev/vgname"

..

vg_name=$( echo  "${vg_name}" | sed "s|^/dev/||g" )

 

or it is better to do every time ?

 

is ksh command like "${parameter#pattern}" an option ?

i only know a check like  if [ $( echo "${vg_name}" |grep "^/dev" > /dev/null ; echo $? ) -eq 0 ] ..

 

[ $( echo "${vg_name}" |grep "^/dev" > /dev/null ; echo $? ) -eq 0 ] && sed ....

 

regards

Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 5 (384 Views)

Re: change character , when a string occurs ( possible one liner )

[ Edited ]

Hi:

 

As always, TMTOWTDI.

 

For pure efficiency, I'd use the shell:

 

$ vg_name=/dev/vg99
$ echo ${vg_name##*/}
vg99

$ vg_name=vg88
$ echo ${vg_name##*/}
vg88


 Regards!

 

...JRF...

Please use plain text.
Honored Contributor
Doug O'Leary
Posts: 812
Registered: ‎05-25-2005
Message 3 of 5 (371 Views)

Re: change character , when a string occurs ( possible one liner )

Hey;

 

The previous post got it, but just for completeness:

 

Var="Abc/Def/Ghi"

echo ${Var#*/}   = Def/Ghi

echo ${Var##*/} = Ghi

 

One hash is the shortest possible match from the beginning of the string; Two hashes is the biggest possible match.

 

A similar thing can be done from the other side of the string:

 

echo ${Var%/*}     = Abc/Def

echo ${Var%%/*} = Abc

 

You need 2 hashes in your case because one hash would only match the first '/':

 

Vg=/dev/vg01

echo ${Vg#*/} = dev/vg01

echo ${Vg##*/} = vg01

 

Those are really useful constructs; I use them in my scripts quite a bit.

 

Hope that helps.

 

Doug O'Leary


------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Please use plain text.
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 4 of 5 (354 Views)

Re: change character , when a string occurs ( possible one liner )

thx ,

last question :

how can i detect , if in a string is maybe "/dev" , maybe in a "if" statement

 

only so

[ $( echo "${vg_name}" |grep "^/dev" > /dev/null ; echo $? ) -eq 0 ] && sed ....

Please use plain text.
Honored Contributor
Doug O'Leary
Posts: 812
Registered: ‎05-25-2005
Message 5 of 5 (351 Views)

Re: change character , when a string occurs ( possible one liner )

Hey;

 

>>how can i detect , if in a string is maybe "/dev" ,

 

"grep -q" is really useful for that:

 

echo "${var}" | grep -qi "/dev" && echo "${var} has /dev in it" || echo "${var} is missing /dev"

 

HTH;

 

Doug


------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation