Split a string

February 1, 2008 at 13:26:59
Specs: AIX, don't know
I'd like to a split a string into two parts and assign them to two different variables. The string will be passed as an argument to a Korn Shell script For example I have a string something like:

"01.Driver Report|userid@company.com"

I'd like to pass this string as an argument to the script, then the string will be parsed into two parts:
name=01.Driver Report

I've tried use awk but I can't get it to work. Please help.


See More: Split a string

Report •

February 1, 2008 at 15:32:28
I'll leave passing the string to the script to you, but I'll do the hard part. the while loop parses the two variables:


mystring="01.Driver Report|userid@company.com"
echo "$mystring"|while IFS="|" read f1 f2
echo "$name"
echo "$email"

Report •

February 18, 2008 at 04:14:19
it is really good. spliting string is very good. but i am not able to understood this.
how it is spliting with echo statement.
and what do u mean by IFS here.
can u please describe this.

thanks in advance.

mallikarjuna setty

Report •

February 28, 2008 at 12:04:41
The IFS is the shell's field seperator. By default it's white space - a space or tab character. I changed it to a pipe symbol because that is what is the new separator.

echoing the string to the pipe forces the string to become the input to the while loop. With IFS set the way it is, you end up with the proper parse.

Report •

Related Solutions

Ask Question