0

I tried to loop comma separated values with space, but not able to get the exact value since it has space in the string.

I tried in different ways, but i not able to get desired results. Can anyone help me on this

#!/bin/ksh

values="('A','sample text','Mark')"

for i in `echo $values | sed 's/[)(]//g' | sed 's/,/ /g'`
do
  echo $i
done

My expected output is:

A
sample text
Mark
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Gowtham
  • 1
  • 1
  • 5
  • Unless you are getting `values` from somewhere else, you should make it a regular array: `values=(A "sample text" Mark)`. If you *are* getting it from somewhere else, have them send an easier-to-parse format, like JSON. – chepner Feb 13 '19 at 13:21
  • 1
    Your values value is not a comma separated. It's a literal string that also contains " and ( and ' and ) – nullPointer Feb 13 '19 at 13:22

4 Answers4

2

First, change values to an array. Then iterating over it is a simple matter.

values=(A "sample text" Mark)
for i in "${values[@]}"; do
  echo "$i"
done
chepner
  • 497,756
  • 71
  • 530
  • 681
  • how can I change to array ? – Gowtham Feb 13 '19 at 14:03
  • values=('A','sample text','Mark') how i will convert this comma seperated values to array ? – Gowtham Feb 13 '19 at 14:16
  • lose the comma `,`. `values=('A' 'sample text' 'Mark')` – han solo Feb 13 '19 at 14:20
  • @Gowtham Your current value is a *string* that looks an array with commas. This answer uses the array assignment syntax (note the lack of quotes; `a=(...)` is a special kind of assignment.) – chepner Feb 13 '19 at 14:44
  • i cant able to loop with this single quote value values=('A' 'sample text' 'Mark'). how can i loop this ? – Gowtham Feb 13 '19 at 14:51
  • I tired, its not working. values=('A' 'sample text' 'Mark') for i in "${values[@]}"; do echo "$i" done – Gowtham Feb 13 '19 at 17:52
  • @Gowtham Can you please be more specific? For example, instead of saying "its not working" you can say "I put it in a file `foo.sh` and ran `sh foo.sh` but it says `foo.sh: 1: foo.sh: Syntax error: "(" unexpected` instead of printing the values on separate lines" – that other guy Feb 13 '19 at 21:33
  • @Gowtham Are you sure you are actually using `ksh` to run your script? Not all shells support arrays. – chepner Feb 13 '19 at 21:44
0

This is the same as Chepner's answer, only kludgier, (variable substitution), and more dangerous, (the eval...), the better to use the OP's exact $values assignment:

values="('A','sample text','Mark')"
eval values=${values//,/ }
for i in "${values[@]}"; do
  echo "$i"
done

It works in ksh, but really, if at all possible try to use Chepner's simpler and safer $values assignment.

agc
  • 7,973
  • 2
  • 29
  • 50
0

Simply trim the quotes

#!/bin/ksh

values="('A','sample text','Mark')"
echo $values |  tr  -d "()'\"" | tr ',' '\n'

output:

A
sample text
Mark
Akhil
  • 912
  • 12
  • 23
0

You should use the single quotes for splitting the string (and quote "$values").
When your sed supports \n for replacement into a line, you can do without a loop:

echo "${values}" | sed "s/[)(]//g;s/','/\n/g;s/'//g"
# or
sed "s/[)(]//g;s/','/\n/g;s/'//g" <<< "${values}"

When the values in your string are without a comma and parentheses, you can use

grep -Eo "[^',()]*" <<< "${values}"

Better is looking for fields between 2 single quotes and remove those single quotes.

grep -Eo "'[^']*'" <<< "${values}" | tr -d "'"
Walter A
  • 19,067
  • 2
  • 23
  • 43