conversion of space separated string to coma separated in bash









up vote
0
down vote

favorite












I am new to shell scripting



I am indenting to convert a string like:



abc def ghi


to



"abc","def","ghi"


This is what I have tried:



testvar= "abc def ghi"

a='"';

res="";
coma=","
for i in $testvar
do
vals=($i//__/ )
if [ -z "$res" ]; then
$res= $res$a$vals$a
else
$res=$res$coma$a$vals$a
fi
done

echo $res


Its giving this error:



$bash -f main.sh

main.sh: line 4: abc def ghi: command not found


What wrong am I doing?
Is there any better way to do this?










share|improve this question























  • Check out the answer to a similar questions here: stackoverflow.com/questions/918886/…
    – Eric Jorgensen
    Nov 10 at 15:23










  • Thanks for the pointer, but I am trying to do a bit diff thing, I have already used the split syntax from the link in vals=($i//__/ )
    – Samayra Goyal
    Nov 10 at 15:34














up vote
0
down vote

favorite












I am new to shell scripting



I am indenting to convert a string like:



abc def ghi


to



"abc","def","ghi"


This is what I have tried:



testvar= "abc def ghi"

a='"';

res="";
coma=","
for i in $testvar
do
vals=($i//__/ )
if [ -z "$res" ]; then
$res= $res$a$vals$a
else
$res=$res$coma$a$vals$a
fi
done

echo $res


Its giving this error:



$bash -f main.sh

main.sh: line 4: abc def ghi: command not found


What wrong am I doing?
Is there any better way to do this?










share|improve this question























  • Check out the answer to a similar questions here: stackoverflow.com/questions/918886/…
    – Eric Jorgensen
    Nov 10 at 15:23










  • Thanks for the pointer, but I am trying to do a bit diff thing, I have already used the split syntax from the link in vals=($i//__/ )
    – Samayra Goyal
    Nov 10 at 15:34












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am new to shell scripting



I am indenting to convert a string like:



abc def ghi


to



"abc","def","ghi"


This is what I have tried:



testvar= "abc def ghi"

a='"';

res="";
coma=","
for i in $testvar
do
vals=($i//__/ )
if [ -z "$res" ]; then
$res= $res$a$vals$a
else
$res=$res$coma$a$vals$a
fi
done

echo $res


Its giving this error:



$bash -f main.sh

main.sh: line 4: abc def ghi: command not found


What wrong am I doing?
Is there any better way to do this?










share|improve this question















I am new to shell scripting



I am indenting to convert a string like:



abc def ghi


to



"abc","def","ghi"


This is what I have tried:



testvar= "abc def ghi"

a='"';

res="";
coma=","
for i in $testvar
do
vals=($i//__/ )
if [ -z "$res" ]; then
$res= $res$a$vals$a
else
$res=$res$coma$a$vals$a
fi
done

echo $res


Its giving this error:



$bash -f main.sh

main.sh: line 4: abc def ghi: command not found


What wrong am I doing?
Is there any better way to do this?







bash shell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 15:58









Cyrus

44.2k43375




44.2k43375










asked Nov 10 at 15:21









Samayra Goyal

12




12











  • Check out the answer to a similar questions here: stackoverflow.com/questions/918886/…
    – Eric Jorgensen
    Nov 10 at 15:23










  • Thanks for the pointer, but I am trying to do a bit diff thing, I have already used the split syntax from the link in vals=($i//__/ )
    – Samayra Goyal
    Nov 10 at 15:34
















  • Check out the answer to a similar questions here: stackoverflow.com/questions/918886/…
    – Eric Jorgensen
    Nov 10 at 15:23










  • Thanks for the pointer, but I am trying to do a bit diff thing, I have already used the split syntax from the link in vals=($i//__/ )
    – Samayra Goyal
    Nov 10 at 15:34















Check out the answer to a similar questions here: stackoverflow.com/questions/918886/…
– Eric Jorgensen
Nov 10 at 15:23




Check out the answer to a similar questions here: stackoverflow.com/questions/918886/…
– Eric Jorgensen
Nov 10 at 15:23












Thanks for the pointer, but I am trying to do a bit diff thing, I have already used the split syntax from the link in vals=($i//__/ )
– Samayra Goyal
Nov 10 at 15:34




Thanks for the pointer, but I am trying to do a bit diff thing, I have already used the split syntax from the link in vals=($i//__/ )
– Samayra Goyal
Nov 10 at 15:34












2 Answers
2






active

oldest

votes

















up vote
0
down vote













Alternate way by creating an array and using IFS. Loop through every value and add double qoutes around it.



array=($testvar)

declare item
for idx in "$!array[@]"; do
item="$array[$idx]"
array[$idx]=""$item"" # Add double qoute
done

(IFS=, ; echo "$array[*]") # prevents IFS from changing.





share|improve this answer



























    up vote
    0
    down vote













    maybe you can use sed command like below:
    (notice that there are multi spaces in def and ghi)



    $ echo 'abc def ghi' | sed -E 's/s+/,/g'
    abc,def,ghi





    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240366%2fconversion-of-space-separated-string-to-coma-separated-in-bash%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      Alternate way by creating an array and using IFS. Loop through every value and add double qoutes around it.



      array=($testvar)

      declare item
      for idx in "$!array[@]"; do
      item="$array[$idx]"
      array[$idx]=""$item"" # Add double qoute
      done

      (IFS=, ; echo "$array[*]") # prevents IFS from changing.





      share|improve this answer
























        up vote
        0
        down vote













        Alternate way by creating an array and using IFS. Loop through every value and add double qoutes around it.



        array=($testvar)

        declare item
        for idx in "$!array[@]"; do
        item="$array[$idx]"
        array[$idx]=""$item"" # Add double qoute
        done

        (IFS=, ; echo "$array[*]") # prevents IFS from changing.





        share|improve this answer






















          up vote
          0
          down vote










          up vote
          0
          down vote









          Alternate way by creating an array and using IFS. Loop through every value and add double qoutes around it.



          array=($testvar)

          declare item
          for idx in "$!array[@]"; do
          item="$array[$idx]"
          array[$idx]=""$item"" # Add double qoute
          done

          (IFS=, ; echo "$array[*]") # prevents IFS from changing.





          share|improve this answer












          Alternate way by creating an array and using IFS. Loop through every value and add double qoutes around it.



          array=($testvar)

          declare item
          for idx in "$!array[@]"; do
          item="$array[$idx]"
          array[$idx]=""$item"" # Add double qoute
          done

          (IFS=, ; echo "$array[*]") # prevents IFS from changing.






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 16:47









          Vivek Akupatni

          67729




          67729






















              up vote
              0
              down vote













              maybe you can use sed command like below:
              (notice that there are multi spaces in def and ghi)



              $ echo 'abc def ghi' | sed -E 's/s+/,/g'
              abc,def,ghi





              share|improve this answer
























                up vote
                0
                down vote













                maybe you can use sed command like below:
                (notice that there are multi spaces in def and ghi)



                $ echo 'abc def ghi' | sed -E 's/s+/,/g'
                abc,def,ghi





                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  maybe you can use sed command like below:
                  (notice that there are multi spaces in def and ghi)



                  $ echo 'abc def ghi' | sed -E 's/s+/,/g'
                  abc,def,ghi





                  share|improve this answer












                  maybe you can use sed command like below:
                  (notice that there are multi spaces in def and ghi)



                  $ echo 'abc def ghi' | sed -E 's/s+/,/g'
                  abc,def,ghi






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 13:59









                  GerryLon

                  444




                  444



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240366%2fconversion-of-space-separated-string-to-coma-separated-in-bash%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Barbados

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3