bash + read variables & values from file by bash script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












I have the following file variable and values



# more file.txt
export worker01="sdg sdh sdi sdj sdk"
export worker02="sdg sdh sdi sdj sdm"
export worker03="sdg sdh sdi sdj sdf"


I perform source in order to read the variable



# source file.txt


example:



echo $worker01
sdg sdh sdi sdj sdk


until now every thing is perfect



but now I want to read the variables from the file and print the values
by simple bash loop I will read the second field and try to print value of the variable



# for i in ` sed s'/=/ /g' /tmp/file.txt | awk 'print $2' `
do
echo $i
declare var="$i"
echo $var
done


but its print only the variable and not the values



worker01
worker01
worker02
worker02
worker03
worker03


expected output:



worker01
sdg sdh sdi sdj sdk
worker02
sdg sdh sdi sdj sdm
worker03
sdg sdh sdi sdj sdf






share|improve this question


























    up vote
    6
    down vote

    favorite












    I have the following file variable and values



    # more file.txt
    export worker01="sdg sdh sdi sdj sdk"
    export worker02="sdg sdh sdi sdj sdm"
    export worker03="sdg sdh sdi sdj sdf"


    I perform source in order to read the variable



    # source file.txt


    example:



    echo $worker01
    sdg sdh sdi sdj sdk


    until now every thing is perfect



    but now I want to read the variables from the file and print the values
    by simple bash loop I will read the second field and try to print value of the variable



    # for i in ` sed s'/=/ /g' /tmp/file.txt | awk 'print $2' `
    do
    echo $i
    declare var="$i"
    echo $var
    done


    but its print only the variable and not the values



    worker01
    worker01
    worker02
    worker02
    worker03
    worker03


    expected output:



    worker01
    sdg sdh sdi sdj sdk
    worker02
    sdg sdh sdi sdj sdm
    worker03
    sdg sdh sdi sdj sdf






    share|improve this question
























      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      I have the following file variable and values



      # more file.txt
      export worker01="sdg sdh sdi sdj sdk"
      export worker02="sdg sdh sdi sdj sdm"
      export worker03="sdg sdh sdi sdj sdf"


      I perform source in order to read the variable



      # source file.txt


      example:



      echo $worker01
      sdg sdh sdi sdj sdk


      until now every thing is perfect



      but now I want to read the variables from the file and print the values
      by simple bash loop I will read the second field and try to print value of the variable



      # for i in ` sed s'/=/ /g' /tmp/file.txt | awk 'print $2' `
      do
      echo $i
      declare var="$i"
      echo $var
      done


      but its print only the variable and not the values



      worker01
      worker01
      worker02
      worker02
      worker03
      worker03


      expected output:



      worker01
      sdg sdh sdi sdj sdk
      worker02
      sdg sdh sdi sdj sdm
      worker03
      sdg sdh sdi sdj sdf






      share|improve this question














      I have the following file variable and values



      # more file.txt
      export worker01="sdg sdh sdi sdj sdk"
      export worker02="sdg sdh sdi sdj sdm"
      export worker03="sdg sdh sdi sdj sdf"


      I perform source in order to read the variable



      # source file.txt


      example:



      echo $worker01
      sdg sdh sdi sdj sdk


      until now every thing is perfect



      but now I want to read the variables from the file and print the values
      by simple bash loop I will read the second field and try to print value of the variable



      # for i in ` sed s'/=/ /g' /tmp/file.txt | awk 'print $2' `
      do
      echo $i
      declare var="$i"
      echo $var
      done


      but its print only the variable and not the values



      worker01
      worker01
      worker02
      worker02
      worker03
      worker03


      expected output:



      worker01
      sdg sdh sdi sdj sdk
      worker02
      sdg sdh sdi sdj sdm
      worker03
      sdg sdh sdi sdj sdf








      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 6 at 1:30









      Drakonoved

      674518




      674518










      asked Dec 25 '17 at 16:40









      yael

      2,0091145




      2,0091145




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          You have export worker01="sdg sdh sdi sdj sdk", then you replace = with a space to get export worker01 "sdg sdh sdi sdj sdk". The space separated fields in that are export, worker01, "sdg, sdh, etc.



          It's probably better to split on =, and remove the quotes, so with just the shell:



          $ while IFS== read -r key val ; do
          val=$val%"; val=$val#"; key=$key#export ;
          echo "$key = $val";
          done < vars
          worker01 = sdg sdh sdi sdj sdk
          worker02 = sdg sdh sdi sdj sdm
          worker03 = sdg sdh sdi sdj sdf


          key contains the variable name, val the value. Of course this doesn't actually parse the input, it just removes the double quotes if they happen to be there.






          share|improve this answer






















          • what in case if I want to print the parameter before value line?
            – yael
            Dec 25 '17 at 16:51










          • see my update question , sorry
            – yael
            Dec 25 '17 at 16:55










          • @yael, updated..
            – ilkkachu
            Dec 25 '17 at 16:58

















          up vote
          2
          down vote













          With awk alone:



          awk -F'"' 'print $2' file.txt
          # To print the variable name as well:
          awk 'gsub(/[:=]/," "); gsub(/[:"]/,""); if ($1 = "export") $1=""; print $0' file.txt


          to loop it you can:



          for i in "$(awk -F" 'print $2' file.txt)"; do
          var="$i"
          echo "$var"
          done
          my_array=($(awk -F" 'print $2' file.txt))
          for element in "$my_var[@]"; do
          another_var="$element"
          echo "$another_var"
          done


          If you also want to print the variable name in your loop you can do this:



          #! /usr/bin/env bash -
          while read -r line; do
          if [[ "$(awk 'print $1' <<<"$line")" == 'export' ]]; then
          var_name="$(awk 'print $2' <<<"$line" | awk -F'=' 'print $1')"
          var_value="$(awk -F" 'print $2' <<<"$line")"
          echo -e "$var_namen$var_value"
          else
          continue
          fi
          done<file.txt


          Output:



          $ ./script.sh
          worker01
          sdg sdh sdi sdj sdk
          worker02
          sdg sdh sdi sdj sdm
          worker03
          sdg sdh sdi sdj sdf





          share|improve this answer





























            up vote
            2
            down vote













            First, you can get the variables names with this GNU grep command, using a Perl-compat regex:



            grep -oP 'export K[^=]+' file.txt


            Then, you can read the output of that into a bash array with:



            mapfile -t variables < <(grep -oP 'export K[^=]+' file.txt)


            That uses the bash builtin mapfile command and a process substitition.



            Last, iterate over the variable names and use indirect parameter expansion to get the values:



            for v in "$variables[@]"; do 
            printf "varname=%stvalue=%sn" "$v" "$!v"
            done




            varname=worker01 value=sdg sdh sdi sdj sdk
            varname=worker02 value=sdg sdh sdi sdj sdm
            varname=worker03 value=sdg sdh sdi sdj sdf





            share|improve this answer



























              up vote
              0
              down vote













              You can use this sed



              sed -E 's/[^ ]* ([^=]*)="([^"]*)"/1n2/' file.txt


              Or this awk



              awk -F '"|=' 'split($1,a," ");print a[2]"n"$3' file.txt





              share|improve this answer




















                Your Answer







                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "106"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                convertImagesToLinks: false,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );








                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412980%2fbash-read-variables-values-from-file-by-bash-script%23new-answer', 'question_page');

                );

                Post as a guest






























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                5
                down vote



                accepted










                You have export worker01="sdg sdh sdi sdj sdk", then you replace = with a space to get export worker01 "sdg sdh sdi sdj sdk". The space separated fields in that are export, worker01, "sdg, sdh, etc.



                It's probably better to split on =, and remove the quotes, so with just the shell:



                $ while IFS== read -r key val ; do
                val=$val%"; val=$val#"; key=$key#export ;
                echo "$key = $val";
                done < vars
                worker01 = sdg sdh sdi sdj sdk
                worker02 = sdg sdh sdi sdj sdm
                worker03 = sdg sdh sdi sdj sdf


                key contains the variable name, val the value. Of course this doesn't actually parse the input, it just removes the double quotes if they happen to be there.






                share|improve this answer






















                • what in case if I want to print the parameter before value line?
                  – yael
                  Dec 25 '17 at 16:51










                • see my update question , sorry
                  – yael
                  Dec 25 '17 at 16:55










                • @yael, updated..
                  – ilkkachu
                  Dec 25 '17 at 16:58














                up vote
                5
                down vote



                accepted










                You have export worker01="sdg sdh sdi sdj sdk", then you replace = with a space to get export worker01 "sdg sdh sdi sdj sdk". The space separated fields in that are export, worker01, "sdg, sdh, etc.



                It's probably better to split on =, and remove the quotes, so with just the shell:



                $ while IFS== read -r key val ; do
                val=$val%"; val=$val#"; key=$key#export ;
                echo "$key = $val";
                done < vars
                worker01 = sdg sdh sdi sdj sdk
                worker02 = sdg sdh sdi sdj sdm
                worker03 = sdg sdh sdi sdj sdf


                key contains the variable name, val the value. Of course this doesn't actually parse the input, it just removes the double quotes if they happen to be there.






                share|improve this answer






















                • what in case if I want to print the parameter before value line?
                  – yael
                  Dec 25 '17 at 16:51










                • see my update question , sorry
                  – yael
                  Dec 25 '17 at 16:55










                • @yael, updated..
                  – ilkkachu
                  Dec 25 '17 at 16:58












                up vote
                5
                down vote



                accepted







                up vote
                5
                down vote



                accepted






                You have export worker01="sdg sdh sdi sdj sdk", then you replace = with a space to get export worker01 "sdg sdh sdi sdj sdk". The space separated fields in that are export, worker01, "sdg, sdh, etc.



                It's probably better to split on =, and remove the quotes, so with just the shell:



                $ while IFS== read -r key val ; do
                val=$val%"; val=$val#"; key=$key#export ;
                echo "$key = $val";
                done < vars
                worker01 = sdg sdh sdi sdj sdk
                worker02 = sdg sdh sdi sdj sdm
                worker03 = sdg sdh sdi sdj sdf


                key contains the variable name, val the value. Of course this doesn't actually parse the input, it just removes the double quotes if they happen to be there.






                share|improve this answer














                You have export worker01="sdg sdh sdi sdj sdk", then you replace = with a space to get export worker01 "sdg sdh sdi sdj sdk". The space separated fields in that are export, worker01, "sdg, sdh, etc.



                It's probably better to split on =, and remove the quotes, so with just the shell:



                $ while IFS== read -r key val ; do
                val=$val%"; val=$val#"; key=$key#export ;
                echo "$key = $val";
                done < vars
                worker01 = sdg sdh sdi sdj sdk
                worker02 = sdg sdh sdi sdj sdm
                worker03 = sdg sdh sdi sdj sdf


                key contains the variable name, val the value. Of course this doesn't actually parse the input, it just removes the double quotes if they happen to be there.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 25 '17 at 17:16

























                answered Dec 25 '17 at 16:48









                ilkkachu

                49.9k674137




                49.9k674137











                • what in case if I want to print the parameter before value line?
                  – yael
                  Dec 25 '17 at 16:51










                • see my update question , sorry
                  – yael
                  Dec 25 '17 at 16:55










                • @yael, updated..
                  – ilkkachu
                  Dec 25 '17 at 16:58
















                • what in case if I want to print the parameter before value line?
                  – yael
                  Dec 25 '17 at 16:51










                • see my update question , sorry
                  – yael
                  Dec 25 '17 at 16:55










                • @yael, updated..
                  – ilkkachu
                  Dec 25 '17 at 16:58















                what in case if I want to print the parameter before value line?
                – yael
                Dec 25 '17 at 16:51




                what in case if I want to print the parameter before value line?
                – yael
                Dec 25 '17 at 16:51












                see my update question , sorry
                – yael
                Dec 25 '17 at 16:55




                see my update question , sorry
                – yael
                Dec 25 '17 at 16:55












                @yael, updated..
                – ilkkachu
                Dec 25 '17 at 16:58




                @yael, updated..
                – ilkkachu
                Dec 25 '17 at 16:58












                up vote
                2
                down vote













                With awk alone:



                awk -F'"' 'print $2' file.txt
                # To print the variable name as well:
                awk 'gsub(/[:=]/," "); gsub(/[:"]/,""); if ($1 = "export") $1=""; print $0' file.txt


                to loop it you can:



                for i in "$(awk -F" 'print $2' file.txt)"; do
                var="$i"
                echo "$var"
                done
                my_array=($(awk -F" 'print $2' file.txt))
                for element in "$my_var[@]"; do
                another_var="$element"
                echo "$another_var"
                done


                If you also want to print the variable name in your loop you can do this:



                #! /usr/bin/env bash -
                while read -r line; do
                if [[ "$(awk 'print $1' <<<"$line")" == 'export' ]]; then
                var_name="$(awk 'print $2' <<<"$line" | awk -F'=' 'print $1')"
                var_value="$(awk -F" 'print $2' <<<"$line")"
                echo -e "$var_namen$var_value"
                else
                continue
                fi
                done<file.txt


                Output:



                $ ./script.sh
                worker01
                sdg sdh sdi sdj sdk
                worker02
                sdg sdh sdi sdj sdm
                worker03
                sdg sdh sdi sdj sdf





                share|improve this answer


























                  up vote
                  2
                  down vote













                  With awk alone:



                  awk -F'"' 'print $2' file.txt
                  # To print the variable name as well:
                  awk 'gsub(/[:=]/," "); gsub(/[:"]/,""); if ($1 = "export") $1=""; print $0' file.txt


                  to loop it you can:



                  for i in "$(awk -F" 'print $2' file.txt)"; do
                  var="$i"
                  echo "$var"
                  done
                  my_array=($(awk -F" 'print $2' file.txt))
                  for element in "$my_var[@]"; do
                  another_var="$element"
                  echo "$another_var"
                  done


                  If you also want to print the variable name in your loop you can do this:



                  #! /usr/bin/env bash -
                  while read -r line; do
                  if [[ "$(awk 'print $1' <<<"$line")" == 'export' ]]; then
                  var_name="$(awk 'print $2' <<<"$line" | awk -F'=' 'print $1')"
                  var_value="$(awk -F" 'print $2' <<<"$line")"
                  echo -e "$var_namen$var_value"
                  else
                  continue
                  fi
                  done<file.txt


                  Output:



                  $ ./script.sh
                  worker01
                  sdg sdh sdi sdj sdk
                  worker02
                  sdg sdh sdi sdj sdm
                  worker03
                  sdg sdh sdi sdj sdf





                  share|improve this answer
























                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    With awk alone:



                    awk -F'"' 'print $2' file.txt
                    # To print the variable name as well:
                    awk 'gsub(/[:=]/," "); gsub(/[:"]/,""); if ($1 = "export") $1=""; print $0' file.txt


                    to loop it you can:



                    for i in "$(awk -F" 'print $2' file.txt)"; do
                    var="$i"
                    echo "$var"
                    done
                    my_array=($(awk -F" 'print $2' file.txt))
                    for element in "$my_var[@]"; do
                    another_var="$element"
                    echo "$another_var"
                    done


                    If you also want to print the variable name in your loop you can do this:



                    #! /usr/bin/env bash -
                    while read -r line; do
                    if [[ "$(awk 'print $1' <<<"$line")" == 'export' ]]; then
                    var_name="$(awk 'print $2' <<<"$line" | awk -F'=' 'print $1')"
                    var_value="$(awk -F" 'print $2' <<<"$line")"
                    echo -e "$var_namen$var_value"
                    else
                    continue
                    fi
                    done<file.txt


                    Output:



                    $ ./script.sh
                    worker01
                    sdg sdh sdi sdj sdk
                    worker02
                    sdg sdh sdi sdj sdm
                    worker03
                    sdg sdh sdi sdj sdf





                    share|improve this answer














                    With awk alone:



                    awk -F'"' 'print $2' file.txt
                    # To print the variable name as well:
                    awk 'gsub(/[:=]/," "); gsub(/[:"]/,""); if ($1 = "export") $1=""; print $0' file.txt


                    to loop it you can:



                    for i in "$(awk -F" 'print $2' file.txt)"; do
                    var="$i"
                    echo "$var"
                    done
                    my_array=($(awk -F" 'print $2' file.txt))
                    for element in "$my_var[@]"; do
                    another_var="$element"
                    echo "$another_var"
                    done


                    If you also want to print the variable name in your loop you can do this:



                    #! /usr/bin/env bash -
                    while read -r line; do
                    if [[ "$(awk 'print $1' <<<"$line")" == 'export' ]]; then
                    var_name="$(awk 'print $2' <<<"$line" | awk -F'=' 'print $1')"
                    var_value="$(awk -F" 'print $2' <<<"$line")"
                    echo -e "$var_namen$var_value"
                    else
                    continue
                    fi
                    done<file.txt


                    Output:



                    $ ./script.sh
                    worker01
                    sdg sdh sdi sdj sdk
                    worker02
                    sdg sdh sdi sdj sdm
                    worker03
                    sdg sdh sdi sdj sdf






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 25 '17 at 17:27

























                    answered Dec 25 '17 at 16:45









                    Jesse_b

                    10.5k22659




                    10.5k22659




















                        up vote
                        2
                        down vote













                        First, you can get the variables names with this GNU grep command, using a Perl-compat regex:



                        grep -oP 'export K[^=]+' file.txt


                        Then, you can read the output of that into a bash array with:



                        mapfile -t variables < <(grep -oP 'export K[^=]+' file.txt)


                        That uses the bash builtin mapfile command and a process substitition.



                        Last, iterate over the variable names and use indirect parameter expansion to get the values:



                        for v in "$variables[@]"; do 
                        printf "varname=%stvalue=%sn" "$v" "$!v"
                        done




                        varname=worker01 value=sdg sdh sdi sdj sdk
                        varname=worker02 value=sdg sdh sdi sdj sdm
                        varname=worker03 value=sdg sdh sdi sdj sdf





                        share|improve this answer
























                          up vote
                          2
                          down vote













                          First, you can get the variables names with this GNU grep command, using a Perl-compat regex:



                          grep -oP 'export K[^=]+' file.txt


                          Then, you can read the output of that into a bash array with:



                          mapfile -t variables < <(grep -oP 'export K[^=]+' file.txt)


                          That uses the bash builtin mapfile command and a process substitition.



                          Last, iterate over the variable names and use indirect parameter expansion to get the values:



                          for v in "$variables[@]"; do 
                          printf "varname=%stvalue=%sn" "$v" "$!v"
                          done




                          varname=worker01 value=sdg sdh sdi sdj sdk
                          varname=worker02 value=sdg sdh sdi sdj sdm
                          varname=worker03 value=sdg sdh sdi sdj sdf





                          share|improve this answer






















                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            First, you can get the variables names with this GNU grep command, using a Perl-compat regex:



                            grep -oP 'export K[^=]+' file.txt


                            Then, you can read the output of that into a bash array with:



                            mapfile -t variables < <(grep -oP 'export K[^=]+' file.txt)


                            That uses the bash builtin mapfile command and a process substitition.



                            Last, iterate over the variable names and use indirect parameter expansion to get the values:



                            for v in "$variables[@]"; do 
                            printf "varname=%stvalue=%sn" "$v" "$!v"
                            done




                            varname=worker01 value=sdg sdh sdi sdj sdk
                            varname=worker02 value=sdg sdh sdi sdj sdm
                            varname=worker03 value=sdg sdh sdi sdj sdf





                            share|improve this answer












                            First, you can get the variables names with this GNU grep command, using a Perl-compat regex:



                            grep -oP 'export K[^=]+' file.txt


                            Then, you can read the output of that into a bash array with:



                            mapfile -t variables < <(grep -oP 'export K[^=]+' file.txt)


                            That uses the bash builtin mapfile command and a process substitition.



                            Last, iterate over the variable names and use indirect parameter expansion to get the values:



                            for v in "$variables[@]"; do 
                            printf "varname=%stvalue=%sn" "$v" "$!v"
                            done




                            varname=worker01 value=sdg sdh sdi sdj sdk
                            varname=worker02 value=sdg sdh sdi sdj sdm
                            varname=worker03 value=sdg sdh sdi sdj sdf






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 27 '17 at 15:43









                            glenn jackman

                            46.7k265103




                            46.7k265103




















                                up vote
                                0
                                down vote













                                You can use this sed



                                sed -E 's/[^ ]* ([^=]*)="([^"]*)"/1n2/' file.txt


                                Or this awk



                                awk -F '"|=' 'split($1,a," ");print a[2]"n"$3' file.txt





                                share|improve this answer
























                                  up vote
                                  0
                                  down vote













                                  You can use this sed



                                  sed -E 's/[^ ]* ([^=]*)="([^"]*)"/1n2/' file.txt


                                  Or this awk



                                  awk -F '"|=' 'split($1,a," ");print a[2]"n"$3' file.txt





                                  share|improve this answer






















                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    You can use this sed



                                    sed -E 's/[^ ]* ([^=]*)="([^"]*)"/1n2/' file.txt


                                    Or this awk



                                    awk -F '"|=' 'split($1,a," ");print a[2]"n"$3' file.txt





                                    share|improve this answer












                                    You can use this sed



                                    sed -E 's/[^ ]* ([^=]*)="([^"]*)"/1n2/' file.txt


                                    Or this awk



                                    awk -F '"|=' 'split($1,a," ");print a[2]"n"$3' file.txt






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Dec 25 '17 at 20:31









                                    ctac_

                                    1,016116




                                    1,016116






















                                         

                                        draft saved


                                        draft discarded


























                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412980%2fbash-read-variables-values-from-file-by-bash-script%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Popular posts from this blog

                                        Peggy Mitchell

                                        Palaiologos

                                        The Forum (Inglewood, California)