LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-05-2009, 04:30 PM   #1
leeharris
LQ Newbie
 
Registered: Feb 2009
Posts: 10

Rep: Reputation: 0
Split a delimited string into multiple strings...


I might be overlooking something really obvious here but...

I have a single string of text, with numerous commas in it. I want to parse the string, and use the comma as a delimeter to separate it into multiple strings hopefully just in shell scripting.

Example:

Input String...

value1,value2,value3,value4

Ouput Strings...

value1
value2
value3
value4

Any ideas?

Thanks - Lee
 
Old 02-05-2009, 05:00 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
Code:
set `echo value1,value2,value3,value4 |awk -F, '{print $1,$2,$3,$4}'`
VAL1=$1
VAL2=$2
VAL3=$3
VAL4=$4
echo $VAL1
echo $VAL2
echo $VAL3
echo $VAL4
You use the -F flag in awk to tell it what to use for field separator (comma in this case).
The print statement with commas in it causes the output to have spaces instead of commas (odd isn't it?)
The set command populates the positional parameters ($1 through $n).
You then initialize arbitrary variable names (I chose VAL1 through VAL4 - you could choose bob, judy, ralph and david or whatever) by assigning each of the positional parameters to one of the arbitrary ones.
Lastly you simply echo each of the arbitrary variables which will show the value it now contains.
 
Old 02-05-2009, 05:57 PM   #3
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

if you're within a bash, you can split the input using arrays:
Code:
jan@jack:~> IFS=',
> '
jan@jack:~> echo "value1,value2,value3,value4" | while read -a inp; do
> for member in ${inp[@]}; do
> echo $member
> done
> done
value1
value2
value3
value4
IFS defines the Input Field Separator, read -a reads all values of a line into an array. The for loop iterates over all elements of the array. It works also for multiple line input:
Code:
jan@jack:~> echo -e "value1,value2,value3,value4\nv5,v6\nval7,val8,val9" |
> while read -a inp; do 
> echo "new line"
> for member in ${inp[@]}; do
> echo $member
> done
> done
new line
value1
value2
value3
value4
new line
v5
v6
new line
val7
val8
val9
Jan
 
Old 02-06-2009, 12:29 AM   #4
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
For The Win...

echo value1,value2,value3 | tr "," "\n"
 
Old 02-06-2009, 08:18 AM   #5
leeharris
LQ Newbie
 
Registered: Feb 2009
Posts: 10

Original Poster
Rep: Reputation: 0
Sorry should have explained this better... those options are all fine if I know how many variables there are. however, I want to do this on a string of an undetermined length.

One time it could be 1 string to 10 strings or 1 string to 500 strings or more.
 
Old 02-06-2009, 08:55 AM   #6
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
my solution works with any number of fields in the input string. tr or "translate" simply replaces all commas with carriage returns.

If the input strings are in a file, and you have multiple string (one per line), then you can do this:

cat inputStrings.txt | tr "," "\n"

If your inputString.txt file contains:

Quote:
value1,value2
value3,value4,value5,value6
value7
value8,value9,value10
The output would be:

Quote:
value1
value2
value3
value4
value5
value6
value7
value8
value9
value10
 
Old 02-06-2009, 09:49 AM   #7
leeharris
LQ Newbie
 
Registered: Feb 2009
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks all who replied... Admiral Beotch, your solution is genius! I just tried it and it works perfectly. As always, simplicity is the way to go :-)

A colleague of mine knocked this up in perl which also works, but isn't as eloquent as your single command approach...

#!/usr/bin/perl
$string=$ARGV[0];
@list= split(/,/,$string);
foreach $line (@list)
{
print "$line\n";
}


Nice.

Thanks - Lee
 
Old 02-06-2009, 12:44 PM   #8
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
A perl one-liner. Replace 'file' with the name of the file:
Code:
perl -nle 'print "$_" foreach split /,/' file
 
Old 02-06-2009, 03:30 PM   #9
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

just for justice ;-) As you see in my example, the array method works with different number of values too.

Jan
 
Old 02-06-2009, 05:04 PM   #10
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90

Quote:
Originally Posted by In Russian accent:
In Soviet Russia, interpreted jan61 perl script is inferior speed to compiled 'tr' binary executable.
 
Old 02-07-2009, 07:10 AM   #11
shyamkumar1986
LQ Newbie
 
Registered: Feb 2009
Posts: 19

Rep: Reputation: 1
If you are planning on using Java - here is an example you can leverage:

http://www.exampledepot.com/egs/java.../Tokenize.html

Last edited by shyamkumar1986; 02-09-2009 at 09:39 AM.
 
  


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
Efficient use of C string libraries with C++ strings? R00ts Programming 4 04-08-2008 12:43 PM
How to concatenate two strings into one string in B-shell? jimmyjiang Red Hat 5 01-08-2008 02:15 PM
string split ovince Programming 4 06-10-2007 06:45 PM
split string prob izza_azhar Programming 3 02-08-2005 01:11 AM
split string izza_azhar Programming 6 01-18-2005 09:24 PM


All times are GMT -5. The time now is 01:40 PM.

Main Menu
Advertisement
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