1

I have a value stored in a variable like

input="###key1,value1###key2,value2###key3,value3###"

I wanted to have output like

key1 has value1 values
key2 has value2 values
key3 has value3 values

Could anybody help me out how to achieve it using loops in bash scripting?

Thanks,:)

and one more doubt, what if the

input="###key1, value1###key2, value2###key3,value3###

that is with whitespace in front of values? How to trim that?

3 Answers 3

2

Here is a way by using loop in bash.

lee:~/StackOverFlow $ cat test.sh 
input="$1"

input="$(echo "$input" | sed -E 's/^#+//; s/#+$//; s/#+/\\n/g;')"

echo -e "$input" | while read item; do
    k="$(echo $item | awk -F\, '{print $1}' | sed -E 's/^[[:space:]]+//')"
    v="$(echo $item | awk -F\, '{print $2}' | sed -E 's/^[[:space:]]+//')"
    echo "$k has $v values"
done

Output

lee:~/StackOverFlow $ bash test.sh "###key1,value1###key2,value2###key3,value3###"
key1 has value1 values
key2 has value2 values
key3 has value3 values
lee:~/StackOverFlow $ bash test.sh "###key1, value1###key2, value2###key3,value3###"
key1 has value1 values
key2 has value2 values
key3 has value3 values
  1. Remove # the start and end of the line (-E Interpret regular expressions as extended)
  2. Replace # with \n
  3. Using while to read the whole line include whitespaces
  4. Split the line into the key-value pair and remove whitespaces
Sign up to request clarification or add additional context in comments.

1 Comment

7 call to sed and 6 to awk for a single string ?
1

You could use the following approach (only for grep versions with P flag):

#!/usr/bin/env bash                                                               

input="###key1,value1###key2,value2###key3,value3###"                            

eval $(grep -oP '[^#]*(?=###)' <<< $(echo "$input") | sed "s/,/=/")                

echo $key1                                                                      
echo $key2                                                                      
echo $key3

Output:

value1                                                                          
value2                                                                          
value3                                                                          

For your second case the spaces around = must be removed, so instead of sed "s/,/=/" you need sed "s/,/=/;s/ *//"

1 Comment

thank you, but seems like grep P no longer works as I am using the latest one.
1

With gnu sed

sed 's/##*/\n/g;s/^\n\|\n$//g;s/,/ has /g;s/\n\|$/ values&/g' <<<"$input"

  • s/##*/\n/g _________ replace each one or more # by \n
  • s/^\n\|\n$//g _______ replace the empty first and last line by nothing
  • s/,/ has /g_________ replace ',' by ' has '
  • s/\n\|$/ values&/g ___replace end of each line by ' values'

1 Comment

Thank you for the explanation.:) It is working as expected.:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.