Computing.Net > Forums > Unix > Split a string

Split a string

Reply to Message Icon

Original Message
Name: ohuang
Date: February 1, 2008 at 13:26:59 Pacific
Subject: Split a string
OS: AIX
CPU/Ram: don't know
Model/Manufacturer: IBM
Comment:

Hello,
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
email=userid@company.com

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

thanks.


Report Offensive Message For Removal

Response Number 1
Name: nails
Date: February 1, 2008 at 15:32:28 Pacific
Subject: Split a string
Reply: (edit)

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:

#!/bin/ksh

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


Report Offensive Follow Up For Removal

Response Number 2
Name: malli.eskala
Date: February 18, 2008 at 04:14:19 Pacific
Subject: Split a string
Reply: (edit)

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.


Regards,
mallikarjuna setty


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: February 28, 2008 at 12:04:41 Pacific
Subject: Split a string
Reply: (edit)

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 Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Split a string

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software