LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Suse/Novell
User Name
Password
Suse/Novell This Forum is for the discussion of Suse Linux.

Notices

Reply
 
Search this Thread
Old 06-15-2009, 09:04 AM   #1
sunilvadranapu
Member
 
Registered: May 2006
Posts: 64

Rep: Reputation: 15
parse comma delimited text string


in shell script, I need to know how to get from one comma delimited
text string to many strings.

from this:

main_string = "onE,Two,Three , fouR,five, six "

to these:

string1 = "one"
string2 = "two"
string3 = "three"
string4 = "four"
string5 = "five"
string6 = "six"


how to implement this in shell scipt? can anyone help?
 
Old 06-15-2009, 09:40 AM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,703
Blog Entries: 1

Rep: Reputation: 219Reputation: 219Reputation: 219
Not pretty, hopefully it'll help you started.
Code:
#~/bin/sh

string="onE,Two,Three , fouR,five, six "

string1=$(echo $string | cut -f1 -d ',')
string2=$(echo $string | cut -f2 -d ',')
string3=$(echo $string | cut -f3 -d ',')
string4=$(echo $string | cut -f4 -d ',')
string5=$(echo $string | cut -f5 -d ',')
string6=$(echo $string | cut -f6 -d ',')

exit 0
 
Old 06-15-2009, 09:55 AM   #3
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
You really probably want to use arrays instead of strings.

Code:
#/bin/sh

main_string='onE,Two,Three , fouR,five, six'

COUNTER1=1

for NUMBER in `echo ${main_string} | sed -e 's/[, ]\+/\n/g' -e 's/\(.*\)/\L\1/'` ; do
        string[$COUNTER]=${NUMBER}
        let COUNTER1=COUNTER1+1
done

COUNTER2=1

while [ ${COUNTER2} -lt ${COUNTER1} ] ; do
        echo "string[${COUNTER2}] = ${string[${COUNTER2}]}"
        let COUNTER2=COUNTER2+1
done
HTH

Forrest
 
Old 06-15-2009, 10:06 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
there is no need to use so many cuts as in one of the example solution, and depending on what you want to do, sometimes, there's no need to use arrays. anyway, in bash
Code:
main_string="onE,Two,Three , fouR,five, six "
IFS=","
set -- $main_string
echo $1
echo $2 # and so on
personally, i prefer awk
Code:
echo $main_string | awk -F"," '{print $1, $2 ...}'
 
Old 06-16-2009, 12:05 AM   #5
sunilvadranapu
Member
 
Registered: May 2006
Posts: 64

Original Poster
Rep: Reputation: 15
Hi forrestt,
thanks for your quick reply. your code suited exactly for my requirement. thanks you very much.


-SUN
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
column re-alignment - space delimited to comma delimited hattori.hanzo Linux - Newbie 9 03-05-2009 12:54 AM
Manipulating comma delimited CSV - newbie help with sed etc jonnymorris Programming 16 09-19-2008 06:14 AM
separating a comma delimited line mrobertson Programming 7 07-27-2005 01:56 PM
Comma-Delimited Website Filenames Apocalypse General 1 11-09-2003 09:05 AM
comma delimited file cdragon Programming 5 06-21-2002 07:55 PM


All times are GMT -5. The time now is 07:46 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration