1752567 Members
5098 Online
108788 Solutions
New Discussion юеВ

Variable manipulating

 
Ralf Buchhold
Regular Advisor

Variable manipulating

I hope somebody can help me:
My Problem:
from a variable with this content:
/dev/dsk/c0t0d0 /cdrom cdfs ro,suid 0 0
i need the:
/dev/dsk/c0t0d0
and:
/cdrom
in to different variables.
How can i do this?
Thank you

8 REPLIES 8
john korterman
Honored Contributor

Re: Variable manipulating

Hi,
# VAR1="dev/dsk/c0t0d0 /cdrom cdfs ro,suid 0 0"

# DEV=$(echo $VAR1 | awk '{print $1}')
# echo $DEV
# CDR=$(echo $VAR1 | awk '{print $2}')
# echo $CDR


regards,
John K.
it would be nice if you always got a second chance
SureshKumar_2
Valued Contributor

Re: Variable manipulating

Hi

Is that file containing only one line or multiple line

here i am giving example for file containing one line.
*)to get /dev/dsk/c0t0d0:
cut -f 1 -d " "
*)to get /cdrom
cut -f 2 -d " "

If u have more than one line use,
*) extract the line comes with cdrom
cat | grep cdrom >
And use above cut to get from tmpfile

ALL THE BEST
Things are very easy, when u know about it...
Jean-Luc Oudart
Honored Contributor

Re: Variable manipulating

echo /dev/dsk/c0t0d0 /cdrom cdfs ro,suid 0 0
| read v1 v2 v3 v4 v5 v6

echo $v1
/dev/dsk/c0t0d0

echo $v2
/cdrom

Regards,
Jean-Luc
fiat lux
SureshKumar_2
Valued Contributor

Re: Variable manipulating

Hi

Try this, if u want to extract Entire Col.

for first col.
#cut -f 1 -d " "

for 2nd col.
#cut -f 2 -d " "


If u want to extract only Both col. use character count option in cut respectively.

#cut -f 1,2 -d " "

d option for delimitor.
f option for field
Things are very easy, when u know about it...
KapilRaj
Honored Contributor

Re: Variable manipulating

echo $VAR |read VAR1 VAR2 VAR3 VAR4 VAR5 VAR6

VAR1 ---- /dev/dsk/c0t0d0
VAR2 ---- /cdrom
...
...


Regds,

Kaps
Nothing is impossible
Sanjay Kumar Suri
Honored Contributor

Re: Variable manipulating

Exam Question. Do the following way:

var="/dev/dsk/c0t0d0 /cdrom cdfs ro,suid 0 0"
var1=`echo $var | cut -f1 -d " "`
var2=`echo $var | cut -f2 -d " "`

echo $var1
echo $var2

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Nicolas Dumeige
Esteemed Contributor

Re: Variable manipulating

var="/dev/dsk/c0t0d0 /cdrom cdfs ro,suid 0 0"
echo $var | read var1 var2 trash

echo $var1
/dev/dsk/c0t0d0

echo $var2
/cdrom
All different, all Unix
Rodney Hills
Honored Contributor

Re: Variable manipulating

ksh and posix shell can have array variables. So if you do the following-

str="/dev/dsk/c0t0d0 /cdrom cdfs ro,suid 0 0"
set -A ary $str
dev=${ary[0]}
mnt=${ary[1]}

HTH

-- Rod Hills
There be dragons...