Issue
I have a string [u'SOMEVALUE1', u'SOMEVALUE2', u'SOMEVALUE3']
, I would like to parse every element matched by my sed command. The element matched are in the single quote. Here is my script
#!/bin/bash
ARR="[u'SOMEVALUE1', u'SOMEVALUE1', u'SOMEVALUE1']"
for id in $(sed -n "s/^.*'\(.*\)'.*$/\1/ p" <<< ${ARR});
do
echo "$id"
done
I have only the first value returned.
Solution
The wildcard .*
will match the longest leftmost possible string. If your intention is to match the individual substrings which are in single quotes, try
grep -o "'[^']*'" <<<"$ARR"
To remove the single quotes around the values, simply pipe to sed "s/'//g"
and to loop over the lines printed by a pipe, do
... commands ... |
while read -r id; do
: things with "$id"
done
Answered By - tripleee