First of all, please try to print the newline separated list as
KamilCuk comments:
#!/usr/bin/python
l = ['Google USA (US Dollars, million)','Google UK (sf.nk)','Google India(rk,lp)']
for i in l:
print(i)
Then feed the output to mapfile.
If you still have some specific reason that you need to separate it in bash, please try:
#!/bin/bash
line="['Google USA (US Dollars, million)','Google UK (sf.nk)','Google India(rk,lp)']"
# equivalent to line=$(python3 /path/printlist.py)
line=${line#[} # remove leading left square bracket
line=${line%]} # remove trailing right square bracket
pat="'([^']*)(.+)"
while :; do
if [[ $line =~ $pat ]]; then
if [[ ${BASH_REMATCH[1]} != "," ]]; then
arr+=("${BASH_REMATCH[1]}")
fi
line=${BASH_REMATCH[2]} # update "line" to the remaining substring
else
break # no more matches
fi
done
# see the result
for i in "${arr[@]}"; do
echo "$i"
done
Output:
Google USA (US Dollars, million)
Google UK (sf.nk)
Google India(rk,lp)
- The pattern
([^']*)(.+) matches 0 or more string of non-singlequote
characters (captured as group1) followed by any characters (captured as group2).
- Then the group1 captures substring surrounded by the single quotes or a comma separating the list.
- the variable
line is assigned to the remaining substring and the loop
repeats till the end of the string.
I need to say the bash script above is not complete because it does not consider the case the string contains the single quote.
printlist.pypyhon program in the first place, like JSON, or output newline or zero separated list from it.I did thatWell, what does "that" refer to exactly? Did you wrote a separate python script to convert your script output to a newline separated script? Did you modifyprintlist.pyto output data in JSON format or as a newline separated list? What format is used byprintlist.pyscript to output the data?