Operating System - HP-UX
1833958 Members
2258 Online
110063 Solutions
New Discussion

Re: word splitting in shell

 
SOLVED
Go to solution
Kourla
Occasional Contributor

word splitting in shell

Hi, can u please somebody tell how to split the word.

I wrote a shell script like this
#!/bin/sh
lv=`bdf |grep /var/opt/nokia |awk '{print $1}'`

In the lv variable I will get some string like "/dev/vg01/lvol2", from this string i have get "vg01" to some other variable.

If is is silly, please don't laugh on me because i am new to shell programming (:


8 REPLIES 8
Sridhar Bhaskarla
Honored Contributor
Solution

Re: word splitting in shell

Hi,

Use the seperator "FS" to achieve what you want. For ex., if you want to get only the Logical volume, you would do

bdf |grep /var/opt/nokia |awk '{print $1}'|awk '{FS="/";print $4}'

Use 'df' instead of 'bdf'. If your lvol name is too long, then you may not get what you want.

If you df, you should grep for the second column and then filerout what you want using the above logic.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: word splitting in shell

Hi (Again),

BTW, if you want to get 'vg01' out of it, you will use '{FS="/";print $3}'

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Jeff Schussele
Honored Contributor

Re: word splitting in shell

Hi Kourla,

Try this

bdf |grep /var/opt/nokia | cut -f 3 -d "/"

This should yield the VG name w/o having to awk. Of course, you could assign the output to a var.

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
James R. Ferguson
Acclaimed Contributor

Re: word splitting in shell

Hi:

'cut' will do here:

# VAR=`echo ${lv}}cut -f3 -d/`

Regards!

...JRF...
Robert-Jan Goossens
Honored Contributor

Re: word splitting in shell

Hi,

bdf |grep /var/opt/nokia |awk '{FS="/";print $3}'

Robert-Jan.
Dietmar Konermann
Honored Contributor

Re: word splitting in shell

I like using shell built-ins, especially for scripts that run frequently. fork/exec is expensive... and things sum up.

vg=${lv#/dev/}&& vg=${vg%/*}

Best regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Al Langen_1
Advisor

Re: word splitting in shell

Nobody suggested split:

Instead of print $1, use split($1, arg, "/"]);print arg[3]

Donny Jekels
Respected Contributor

Re: word splitting in shell

Al,

split works well, another way would be

lv=$(bdf | grep /var/opt/nokia | awk -F/ '{print $1}')

6 ways to skin a cat :o)
"Vision, is the art of seeing the invisible"