- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- change character , when a string occurs ( possible...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2011 05:37 AM
09-30-2011 05:37 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2011 05:50 AM - edited 09-30-2011 05:59 AM
09-30-2011 05:50 AM - edited 09-30-2011 05:59 AM
Re: change character , when a string occurs ( possible one liner )
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2011 09:50 AM
10-02-2011 09:50 AM
SolutionHey;
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2011 08:14 AM
10-07-2011 08:14 AM
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 ....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2011 08:49 AM
10-07-2011 08:49 AM
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