Operating System - HP-UX
1832285 Members
2361 Online
110041 Solutions
New Discussion

Re: is it possible to split the srting...

 
Ramana.Sv
Frequent Advisor

is it possible to split the srting...

is it possible to split the string.
and also in my shell

read name

can i send the this name sting to some where to in script? how to use this.


4 REPLIES 4
Luk Vandenbussche
Honored Contributor

Re: is it possible to split the srting...

spex
Honored Contributor

Re: is it possible to split the srting...

Hello,

I'm not sure that I'm understanding you correctly, but I'll give it a shot.

The 'read' built-in takes input from the command line and stores it into environmental variables. To elaborate on your example:

$ read name
Ramana
$ echo $name
Ramana

In this case, "Ramana" is stored in the variable $name. To recall the value of $name to the command line, simply 'echo' it. Similarly, to use it in a script, simply reference the variable in the appropriate place.

To store values to multiple variables using a single 'read' command, provide multiple variable names as parameters:

$ read name1 name2
Ramana Joe
$ echo name1
Ramana
$ echo name2
Joe

PCS
Steven E. Protter
Exalted Contributor

Re: is it possible to split the srting...

Shalom,

Lets say I had a string that looked like this.

abc:def
variable name is stingbean

var1=$(echo $stringbean | awk -F: '{print $1}')
var2=$(echo $stringbean | awk -F: '{print $2}')

echo $var1
abc
echo $var2
def

You see, we just used awk to split a string.

F: means : is the separator.

Many characters, or even spaces can be used in this method to take data out of the string.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
rmueller58
Valued Contributor

Re: is it possible to split the srting...

you can use Awk to substring a string as well.

for example:

mysub=substr(mystring,startpos,maxlen)

awk '{print substr(1, 1, 9)

in a string with
987654321

the about will pull the value 9