split string into array in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting split string into array in shell
# 1  
Old 05-16-2012
split string into array in shell

Hi all,

I want to split a string into array based on given delimiter, for example:

String:
Code:
 "foo|bar|baz"

with delimiter "|"
into array:
strArr[0] to strArr[2] with values foo, bar and baz.

Thanks a lot.
Roy987
# 2  
Old 05-16-2012
Code:
san@~ :> declare -a myarr=(`echo "foo|bar|baz" |sed 's/|/ /g'`)
san@~ :> echo ${myarr[@]}

foo bar baz
# 3  
Old 05-16-2012
Code:
$ cat x
#!/bin/ksh

IFS="|"
STRING="foo|bar|baz"

set -A array $STRING

print ${array[0]}
print ${array[1]}
print ${array[2]}

exit 0
$ x
foo
bar
baz
$

# 4  
Old 05-16-2012
Quote:
Originally Posted by ningy
Code:
san@~ :> declare -a myarr=(`echo "foo|bar|baz" |sed 's/|/ /g'`)
san@~ :> echo ${myarr[@]}

foo bar baz
This led to an error "Syntax error: "(" unexpected" on my ubuntu,
I executed it in a script started with:
Code:
 #!/bin/sh

---------- Post updated at 07:27 PM ---------- Previous update was at 07:19 PM ----------

Quote:
Originally Posted by gary_w
Code:
$ cat x
#!/bin/ksh

IFS="|"
STRING="foo|bar|baz"

set -A array $STRING

print ${array[0]}
print ${array[1]}
print ${array[2]}

exit 0
$ x
foo
bar
baz
$

This led to an error "Illegal option -A" on my ubuntu,
I execute it in a script started with:
Code:
 #!/bin/sh

Maybe it is related to the mismatch of the syntax and the shell version on my machine.

Thanks a lot
Roy987
# 5  
Old 05-16-2012
Yes I believe the Bourne shell (sh) does not support arrays. Are you able to run my complete example? Make sure to include the first line, it tells whatever your login shell is to use the korn shell to execute the script.

You may have better luck using bash (bourne again shell) heh on Linux.
This User Gave Thanks to gary_w For This Post:
# 6  
Old 05-16-2012
Thanks.
Actually I will ship the code to an embedded device after writing it on ubuntu, on which it is required to use Bourne Shell instead of other options, then I will try other method to pass on data.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to split the string value to an array?

Test1.txt Tom is hot Test.sh filename="/directory/Test1.txt" set - A store while IFS= read value do awk '{split($value,store," ")}' done < "$filename" echo ${#sore} From the code in the executing file, I would like each... (8 Replies)
Discussion started by: TestKing
8 Replies

2. UNIX for Beginners Questions & Answers

How to split a string into array?

value=malayalam # i need to store the value in an array by splitting the character #the output i need is m a l a y a l a m Please use CODE tags for output data as well as required by forum rules! (5 Replies)
Discussion started by: Meeran Rizvi
5 Replies

3. Shell Programming and Scripting

Split string into map (Associative Array)

Hi Input: { committed = 782958592; init = 805306368; max = 1051394048; used = 63456712; } Result: A map (maybe Associative Array) where I can iterate through the key/value. Something like this: for key in $map do echo key=$key value=$map done Sample output from the map: ... (2 Replies)
Discussion started by: chitech
2 Replies

4. Shell Programming and Scripting

Oneliner ---split string to character by piping shell output to perl

Hello, I was trying to split a string to characters by perl oneliner. echo "The quick brown fox jumps over the lazy dog" | perl -e 'split // ' But did not work as with bash script pipe: echo "The quick brown fox jumps over the lazy dog" | fold -w1 | sort | uniq -ic 8 1 T 1... (6 Replies)
Discussion started by: yifangt
6 Replies

5. Shell Programming and Scripting

Shell string array

Hi all, I want to create an array variable in shell, for example: #!/bin/sh name="foo" name="bar" name="baz" But above code didn't work, I also tried with: name=(foo bar baz) and set -A name foo bar baz but none of these worked. Another question is how to know the shell... (6 Replies)
Discussion started by: Roy987
6 Replies

6. Shell Programming and Scripting

SPLIT STRING in bash shell script

i need one help.... if i have a string like aaaaa,bbbbb,ccccc,aaaaa How to to split the string and check howmany times aaaaa will be in that string? Thanks (7 Replies)
Discussion started by: karthinvk
7 Replies

7. Shell Programming and Scripting

Shell script to parse/split input string and display the tokens

Hi, How do I parse/split lines (strings) read from a file and display the individual tokens in a shell script? Given that the length of individual lines is not constant and number of tokens in each line is also not constant. The input file could be as below: ... (3 Replies)
Discussion started by: yajaykumar
3 Replies

8. Shell Programming and Scripting

split files by specifying a string (bash shell)

Hi all, I have a file of around 300 lines in which string "SERVER" occurs around 32 times. for eg. I need to split files like, for eg I am using this code awk '/SERVER/{n++}{print > f n}' f=/vikas/list /vikas/final But the problem is that it makes maximum of 10 files, but I... (12 Replies)
Discussion started by: vikas027
12 Replies

9. Shell Programming and Scripting

split varibles and store fields into shell varible array

I need to split a long varible which is a whole line read from a file into fields and store them in an array, the fields are delimited by pipe and a field may contain white spaces. I tried the following concept test and it has problem with field 5 which contain a space, appearently so because... (3 Replies)
Discussion started by: gratus
3 Replies

10. Shell Programming and Scripting

[KSH] Split string into array

Hi, Is there any way to convert a string into an array in KSH? In other words I want to split the string like this: STRING="one two three four" into an array of 4 values splitting on white space. The array should be similar to the one that would be created with the following command: ... (3 Replies)
Discussion started by: piooooter
3 Replies
Login or Register to Ask a Question