Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This is the output:

3,aac-lc, 93.8, aaclc, 77.3, h.264, 1024.6, h.264, 1029.1, 31, 31, 0,0,0.000000,31,31,0,0,0.000000,7,0,0,0.000000,30,1280 720,10,0,0,0.000000,30,1280 720

I tried with 2 scenario:

  1. Storing in an array

      @arr=split(',',$stats);
      echo "statistics: $stats"
    
  2. Storing in a variable

     echo $stats | cut -d ',' -f | read s1
     echo $s1
    

But neither scenario is working.

share|improve this question
    
awk '{ split("3,aac-lc, 93.8, aaclc, 77.3, h.264, 1024.6, h.264, 1029.1, 31, 31, 0,0,0.000000,31,31,0,0,0.000000,7,0,0,0.000000,30,1280 720,10,0,0,0.000000,30,1280 720 ",arr,","); print arr[1]; }' OR echo "3,aac-lc, 93.8, aaclc, 77.3, h.264, 1024.6, h.264, 1029.1, 31, 31, 0,0,0.000000,31,31,0,0,0.000000,7,0,0,0.000000,30,1280 720,10,0,0,0.000000,30,1280 720" | awk '{ split($0,arr,","); print arr[1]; }' This should work. –  user14039 Feb 29 '12 at 22:01

3 Answers 3

You can use something like this, for a single line of input:

IFS="," read -ra arr <<< "$foo"

Demo:

$ cat t.sh 
foo="3,aac-lc, 93.8, aaclc, 77.3, h.264, 1024.6, ..." # snipped

IFS="," read -ra arr <<< "$foo"
echo ${#arr[@]}
echo ${arr[0]}
echo ${arr[30]}
$ ./t.sh 
31
3
1280 720

Credits: Split string based on delimiter in bash? answer by Johannes Schaub. Check the other answers there too.

share|improve this answer
$ set -f  # turn off globbing, so as not to expand wildcards
$ arr=($(echo "$stats" | sed 's/,/ /g'))
$ echo ${arr[1]}
aac-lc
share|improve this answer
    
Note that this will treat whitespace as separators as well as commas. –  Gilles Feb 29 '12 at 20:14

Your first snippet is nothing like shell syntax. It is correct Perl syntax.
Your second snippet doesn't use cut correctly; I don't know what you intended.

The shell has a built-in string splitting construct: when you write $somevar without any quotes, the shell first looks up the value of the variable somevar, then splits that value into separate words on the characters specified by IFS, and finally interprets each word as a glob pattern (file wildcard). So you can split a string by setting IFS to the separator character and temporarily turning off globbing.

set -f; IFS=,
arr=($stats)
set +f; unset IFS

Note that if a field contains whitespace, the array element will retain that whitespace. If you want to split at all whitespace as well as commas, set IFS=', '. Note that IFS is not a string to split at but a set of characters to split at; either a space or a comma will constitute a separator. Furthermore, there are special rules for whitespace: any sequence of zero or more spaces followed by a comma followed by zero or more spaces will constitute one separator, and any sequence of one or more spaces will also constitute a separator.

If you only want to strip whitespace at the beginning or end of a field, you'll have to do it element by element.

shopt -s extglob
for ((i=0; i<${#arr[@]}; i++)); do
  arr[i]=${arr[i]#+( )}   # strip one or more spaces at the beginning
  arr[i]=${arr[i]%+( )}   # strip one or more spaces at the end
done
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.