logo
down
shadow

Loop through a comma-separated shell variable


Loop through a comma-separated shell variable

By : btornado
Date : November 19 2020, 12:41 AM
seems to work fine You can use the following script to dynamically traverse through your variable, no matter how many fields it has as long as it is only comma separated.
code :
variable=abc,def,ghij
for i in $(echo $variable | sed "s/,/ /g")
do
    # call your procedure/other scripts here below
    echo "$i"
done


Share : facebook icon twitter icon
store comma separated key=value pair in a string to $key, $value variable in shell

store comma separated key=value pair in a string to $key, $value variable in shell


By : Jasmin Pierre
Date : March 29 2020, 07:55 AM
To fix the issue you can do In my bash shell script I have a comma separated key,value pairs in a form of single string. How to parse and store each key and value in separate variables.
code :
#!/bin/bash

string1="key1=value1,key2=value2"

while read -d, -r pair; do
  IFS='=' read -r key val <<<"$pair"
  echo "$key = $val"
done <<<"$string1,"
key1 = value1
key2 = value2
#!/bin/bash

string1="key1=value1,key2=value2"

i=0
while read -d, -r pair; do
  IFS='=' read -r key val <<<"$pair"
  echo "$key = $val" > "key$((++i)).txt"
done <<<"$string1,"
Loop through txt file comma-separated and use as variable

Loop through txt file comma-separated and use as variable


By : Daniel Henneberger
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Another way to achieve is to read the file directly and cut the rows to get the elements directly.
code :
while read p; do
    value1=`echo $p | cut -d',' -f1`
    value2=`echo $p | cut -d',' -f2`
    if [ $value1 = "2012" ]; then
        curl "https://onlinesap.org/reports/$value1/$value2"
    fi
    # Add More conditional statements here for other value1
done < filename.txt
How to convert array list as single variable with comma separated in shell script

How to convert array list as single variable with comma separated in shell script


By : user2398714
Date : March 29 2020, 07:55 AM
help you fix your problem I have an array as look like below , If your input truly were an array, as in:
code :
data=( 1 2 3 4 5 )
testData=$(IFS=,; printf '%s\n' "${data[*]}")
printf -v testData '%s,' "${data[@]}"
testData=${testData%,}
echo "$testData"
1,2,3,4,5
Shell command to convert comma separated string to double quote comma separated string

Shell command to convert comma separated string to double quote comma separated string


By : WHCer
Date : March 29 2020, 07:55 AM
will be helpful for those in need I have a comma separated value like : , How about
code :
$ line='package1,package2'
$ printf '"%s"\n' "${line//,/\",\"}"
"package1","package2"
$ IFS=, read -ra fields <<<"$line"
$ printf '"%s"\n' "${fields[@]}" | paste -sd,
"package1","package2"
How to loop comma separated values in shell script

How to loop comma separated values in shell script


By : jacky
Date : March 29 2020, 07:55 AM
I wish this helpful for you First, change values to an array. Then iterating over it is a simple matter.
code :
values=(A "sample text" Mark)
for i in "${values[@]}"; do
  echo "$i"
done
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org