How to change string values to index value to use in Array?

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











up vote
0
down vote

favorite












I tried to change an string to index to use for an array but I have been unable to get it to work.



This is my file



$ cat file1.txt
101,Harish,BAN
102,Srinu,HYD


And this code:



#!/bin/bash
IFS=','
while read line
do
DELIM_REMOVE=`echo $line|sed 's/,/ /g'`
V=($DEL_REMOVE)
echo $DELIM_REMOVE
for i in "$!V[@]"; do
printf 'V[%s] = %sn' "$i" "$V[i]"
echo "$V[i]"
done
done < /home/ec2-user/file1.txt
echo "$V[i]"


I also need to use the dynamically generated variables in loop to another loop.










share|improve this question



























    up vote
    0
    down vote

    favorite












    I tried to change an string to index to use for an array but I have been unable to get it to work.



    This is my file



    $ cat file1.txt
    101,Harish,BAN
    102,Srinu,HYD


    And this code:



    #!/bin/bash
    IFS=','
    while read line
    do
    DELIM_REMOVE=`echo $line|sed 's/,/ /g'`
    V=($DEL_REMOVE)
    echo $DELIM_REMOVE
    for i in "$!V[@]"; do
    printf 'V[%s] = %sn' "$i" "$V[i]"
    echo "$V[i]"
    done
    done < /home/ec2-user/file1.txt
    echo "$V[i]"


    I also need to use the dynamically generated variables in loop to another loop.










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I tried to change an string to index to use for an array but I have been unable to get it to work.



      This is my file



      $ cat file1.txt
      101,Harish,BAN
      102,Srinu,HYD


      And this code:



      #!/bin/bash
      IFS=','
      while read line
      do
      DELIM_REMOVE=`echo $line|sed 's/,/ /g'`
      V=($DEL_REMOVE)
      echo $DELIM_REMOVE
      for i in "$!V[@]"; do
      printf 'V[%s] = %sn' "$i" "$V[i]"
      echo "$V[i]"
      done
      done < /home/ec2-user/file1.txt
      echo "$V[i]"


      I also need to use the dynamically generated variables in loop to another loop.










      share|improve this question















      I tried to change an string to index to use for an array but I have been unable to get it to work.



      This is my file



      $ cat file1.txt
      101,Harish,BAN
      102,Srinu,HYD


      And this code:



      #!/bin/bash
      IFS=','
      while read line
      do
      DELIM_REMOVE=`echo $line|sed 's/,/ /g'`
      V=($DEL_REMOVE)
      echo $DELIM_REMOVE
      for i in "$!V[@]"; do
      printf 'V[%s] = %sn' "$i" "$V[i]"
      echo "$V[i]"
      done
      done < /home/ec2-user/file1.txt
      echo "$V[i]"


      I also need to use the dynamically generated variables in loop to another loop.







      linux shell-script






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 1 at 7:38









      Rui F Ribeiro

      38.5k1479128




      38.5k1479128










      asked Dec 1 at 6:13









      Harish a

      233




      233




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Don't use shell loops to process text, use a text processing utility.



          awk -F, 'for (i = 0; i < NF; i++) printf "v[%d] = %sn", i, $(i+1)' < file1.txt


          If you have to use a bash loop, then it would make more sense to write it as:



          while IFS=, read -ra v; do
          for i in "$!v[@]"; do
          printf 'v[%d] = %sn' "$i" "$v[i]"
          done
          done < file1.txt


          With the caveat that if the last field is empty, it will be skipped.






          share|improve this answer




















          • It is working fine. Thank you. Need one more help, unable to use Variables outside of the loop.
            – Harish a
            Dec 1 at 9:59











          • @Harisha, it does not make sense to use variables outside of the loop, that variable is reset at each iteration, and we get out of the loop when read fails (on end-of-file) at which point $v is set to an empty array. If you want to access the array for the last line of input, you'd need to make a copy within the loop (like last_v=("$v[@]").
            – Stéphane Chazelas
            Dec 1 at 10:02










          • Can I take any other string like $DATA_V instead of $v. Because I have to use different Variables like DATA_V or DEFAULT_V to call different rows in different files, to store file1.txt rows in $DATA_V[@] and file2.txt in $DEFAULT_V[@], unable to complete my script. Please suggest me.
            – Harish a
            Dec 1 at 11:35










          • @Harisha, I don't understand your question. If you need a two-dimensional array (one for lines, one for fields in the lines), you'll need the ksh93 shell, bash only supports one-dimensional (sparse) arrays. Or again, use a proper text-processing tool like awk or perl.
            – Stéphane Chazelas
            Dec 1 at 12:58










          • Actually, I am looking for Increment variables dynamically. Please refer the like.unix.stackexchange.com/posts/484689/revisions
            – Harish a
            Dec 1 at 16:21


















          up vote
          0
          down vote













          There are several issues with your script.



          • The name of the variable in V=($DEL_REMOVE) should be V=($DELIM_REMOVE)

          • You are setting IFS to a comma (,) but you are removing the comma with sed.

          By doing those two changes your script starts to do something reasonable.



          Making some other changes, your script becomes:



          #!/bin/bash

          IFS=' ' # Use the space to split
          set -f # Avoid globing of values with *,? or
          while read -r line # read the variable without removing backslash
          do
          v=( $line//,/ ) # Convert to an array by splitting with the shell.
          for i in "$!v[@]"; do
          printf 'v[%s] = %sn' "$i" "$v[i]"
          done
          done < file1.txt


          Which will print:



          v[0] = 101
          v[1] = Harish
          v[2] = BAN
          v[0] = 102
          v[1] = Srinu
          v[2] = HYD


          Is that what you expected it to do?






          share|improve this answer




















          • Thank you. It is working.
            – Harish a
            Dec 1 at 10:37










          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: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2funix.stackexchange.com%2fquestions%2f485303%2fhow-to-change-string-values-to-index-value-to-use-in-array%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
          1
          down vote



          accepted










          Don't use shell loops to process text, use a text processing utility.



          awk -F, 'for (i = 0; i < NF; i++) printf "v[%d] = %sn", i, $(i+1)' < file1.txt


          If you have to use a bash loop, then it would make more sense to write it as:



          while IFS=, read -ra v; do
          for i in "$!v[@]"; do
          printf 'v[%d] = %sn' "$i" "$v[i]"
          done
          done < file1.txt


          With the caveat that if the last field is empty, it will be skipped.






          share|improve this answer




















          • It is working fine. Thank you. Need one more help, unable to use Variables outside of the loop.
            – Harish a
            Dec 1 at 9:59











          • @Harisha, it does not make sense to use variables outside of the loop, that variable is reset at each iteration, and we get out of the loop when read fails (on end-of-file) at which point $v is set to an empty array. If you want to access the array for the last line of input, you'd need to make a copy within the loop (like last_v=("$v[@]").
            – Stéphane Chazelas
            Dec 1 at 10:02










          • Can I take any other string like $DATA_V instead of $v. Because I have to use different Variables like DATA_V or DEFAULT_V to call different rows in different files, to store file1.txt rows in $DATA_V[@] and file2.txt in $DEFAULT_V[@], unable to complete my script. Please suggest me.
            – Harish a
            Dec 1 at 11:35










          • @Harisha, I don't understand your question. If you need a two-dimensional array (one for lines, one for fields in the lines), you'll need the ksh93 shell, bash only supports one-dimensional (sparse) arrays. Or again, use a proper text-processing tool like awk or perl.
            – Stéphane Chazelas
            Dec 1 at 12:58










          • Actually, I am looking for Increment variables dynamically. Please refer the like.unix.stackexchange.com/posts/484689/revisions
            – Harish a
            Dec 1 at 16:21















          up vote
          1
          down vote



          accepted










          Don't use shell loops to process text, use a text processing utility.



          awk -F, 'for (i = 0; i < NF; i++) printf "v[%d] = %sn", i, $(i+1)' < file1.txt


          If you have to use a bash loop, then it would make more sense to write it as:



          while IFS=, read -ra v; do
          for i in "$!v[@]"; do
          printf 'v[%d] = %sn' "$i" "$v[i]"
          done
          done < file1.txt


          With the caveat that if the last field is empty, it will be skipped.






          share|improve this answer




















          • It is working fine. Thank you. Need one more help, unable to use Variables outside of the loop.
            – Harish a
            Dec 1 at 9:59











          • @Harisha, it does not make sense to use variables outside of the loop, that variable is reset at each iteration, and we get out of the loop when read fails (on end-of-file) at which point $v is set to an empty array. If you want to access the array for the last line of input, you'd need to make a copy within the loop (like last_v=("$v[@]").
            – Stéphane Chazelas
            Dec 1 at 10:02










          • Can I take any other string like $DATA_V instead of $v. Because I have to use different Variables like DATA_V or DEFAULT_V to call different rows in different files, to store file1.txt rows in $DATA_V[@] and file2.txt in $DEFAULT_V[@], unable to complete my script. Please suggest me.
            – Harish a
            Dec 1 at 11:35










          • @Harisha, I don't understand your question. If you need a two-dimensional array (one for lines, one for fields in the lines), you'll need the ksh93 shell, bash only supports one-dimensional (sparse) arrays. Or again, use a proper text-processing tool like awk or perl.
            – Stéphane Chazelas
            Dec 1 at 12:58










          • Actually, I am looking for Increment variables dynamically. Please refer the like.unix.stackexchange.com/posts/484689/revisions
            – Harish a
            Dec 1 at 16:21













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Don't use shell loops to process text, use a text processing utility.



          awk -F, 'for (i = 0; i < NF; i++) printf "v[%d] = %sn", i, $(i+1)' < file1.txt


          If you have to use a bash loop, then it would make more sense to write it as:



          while IFS=, read -ra v; do
          for i in "$!v[@]"; do
          printf 'v[%d] = %sn' "$i" "$v[i]"
          done
          done < file1.txt


          With the caveat that if the last field is empty, it will be skipped.






          share|improve this answer












          Don't use shell loops to process text, use a text processing utility.



          awk -F, 'for (i = 0; i < NF; i++) printf "v[%d] = %sn", i, $(i+1)' < file1.txt


          If you have to use a bash loop, then it would make more sense to write it as:



          while IFS=, read -ra v; do
          for i in "$!v[@]"; do
          printf 'v[%d] = %sn' "$i" "$v[i]"
          done
          done < file1.txt


          With the caveat that if the last field is empty, it will be skipped.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 1 at 8:03









          Stéphane Chazelas

          296k54560905




          296k54560905











          • It is working fine. Thank you. Need one more help, unable to use Variables outside of the loop.
            – Harish a
            Dec 1 at 9:59











          • @Harisha, it does not make sense to use variables outside of the loop, that variable is reset at each iteration, and we get out of the loop when read fails (on end-of-file) at which point $v is set to an empty array. If you want to access the array for the last line of input, you'd need to make a copy within the loop (like last_v=("$v[@]").
            – Stéphane Chazelas
            Dec 1 at 10:02










          • Can I take any other string like $DATA_V instead of $v. Because I have to use different Variables like DATA_V or DEFAULT_V to call different rows in different files, to store file1.txt rows in $DATA_V[@] and file2.txt in $DEFAULT_V[@], unable to complete my script. Please suggest me.
            – Harish a
            Dec 1 at 11:35










          • @Harisha, I don't understand your question. If you need a two-dimensional array (one for lines, one for fields in the lines), you'll need the ksh93 shell, bash only supports one-dimensional (sparse) arrays. Or again, use a proper text-processing tool like awk or perl.
            – Stéphane Chazelas
            Dec 1 at 12:58










          • Actually, I am looking for Increment variables dynamically. Please refer the like.unix.stackexchange.com/posts/484689/revisions
            – Harish a
            Dec 1 at 16:21

















          • It is working fine. Thank you. Need one more help, unable to use Variables outside of the loop.
            – Harish a
            Dec 1 at 9:59











          • @Harisha, it does not make sense to use variables outside of the loop, that variable is reset at each iteration, and we get out of the loop when read fails (on end-of-file) at which point $v is set to an empty array. If you want to access the array for the last line of input, you'd need to make a copy within the loop (like last_v=("$v[@]").
            – Stéphane Chazelas
            Dec 1 at 10:02










          • Can I take any other string like $DATA_V instead of $v. Because I have to use different Variables like DATA_V or DEFAULT_V to call different rows in different files, to store file1.txt rows in $DATA_V[@] and file2.txt in $DEFAULT_V[@], unable to complete my script. Please suggest me.
            – Harish a
            Dec 1 at 11:35










          • @Harisha, I don't understand your question. If you need a two-dimensional array (one for lines, one for fields in the lines), you'll need the ksh93 shell, bash only supports one-dimensional (sparse) arrays. Or again, use a proper text-processing tool like awk or perl.
            – Stéphane Chazelas
            Dec 1 at 12:58










          • Actually, I am looking for Increment variables dynamically. Please refer the like.unix.stackexchange.com/posts/484689/revisions
            – Harish a
            Dec 1 at 16:21
















          It is working fine. Thank you. Need one more help, unable to use Variables outside of the loop.
          – Harish a
          Dec 1 at 9:59





          It is working fine. Thank you. Need one more help, unable to use Variables outside of the loop.
          – Harish a
          Dec 1 at 9:59













          @Harisha, it does not make sense to use variables outside of the loop, that variable is reset at each iteration, and we get out of the loop when read fails (on end-of-file) at which point $v is set to an empty array. If you want to access the array for the last line of input, you'd need to make a copy within the loop (like last_v=("$v[@]").
          – Stéphane Chazelas
          Dec 1 at 10:02




          @Harisha, it does not make sense to use variables outside of the loop, that variable is reset at each iteration, and we get out of the loop when read fails (on end-of-file) at which point $v is set to an empty array. If you want to access the array for the last line of input, you'd need to make a copy within the loop (like last_v=("$v[@]").
          – Stéphane Chazelas
          Dec 1 at 10:02












          Can I take any other string like $DATA_V instead of $v. Because I have to use different Variables like DATA_V or DEFAULT_V to call different rows in different files, to store file1.txt rows in $DATA_V[@] and file2.txt in $DEFAULT_V[@], unable to complete my script. Please suggest me.
          – Harish a
          Dec 1 at 11:35




          Can I take any other string like $DATA_V instead of $v. Because I have to use different Variables like DATA_V or DEFAULT_V to call different rows in different files, to store file1.txt rows in $DATA_V[@] and file2.txt in $DEFAULT_V[@], unable to complete my script. Please suggest me.
          – Harish a
          Dec 1 at 11:35












          @Harisha, I don't understand your question. If you need a two-dimensional array (one for lines, one for fields in the lines), you'll need the ksh93 shell, bash only supports one-dimensional (sparse) arrays. Or again, use a proper text-processing tool like awk or perl.
          – Stéphane Chazelas
          Dec 1 at 12:58




          @Harisha, I don't understand your question. If you need a two-dimensional array (one for lines, one for fields in the lines), you'll need the ksh93 shell, bash only supports one-dimensional (sparse) arrays. Or again, use a proper text-processing tool like awk or perl.
          – Stéphane Chazelas
          Dec 1 at 12:58












          Actually, I am looking for Increment variables dynamically. Please refer the like.unix.stackexchange.com/posts/484689/revisions
          – Harish a
          Dec 1 at 16:21





          Actually, I am looking for Increment variables dynamically. Please refer the like.unix.stackexchange.com/posts/484689/revisions
          – Harish a
          Dec 1 at 16:21













          up vote
          0
          down vote













          There are several issues with your script.



          • The name of the variable in V=($DEL_REMOVE) should be V=($DELIM_REMOVE)

          • You are setting IFS to a comma (,) but you are removing the comma with sed.

          By doing those two changes your script starts to do something reasonable.



          Making some other changes, your script becomes:



          #!/bin/bash

          IFS=' ' # Use the space to split
          set -f # Avoid globing of values with *,? or
          while read -r line # read the variable without removing backslash
          do
          v=( $line//,/ ) # Convert to an array by splitting with the shell.
          for i in "$!v[@]"; do
          printf 'v[%s] = %sn' "$i" "$v[i]"
          done
          done < file1.txt


          Which will print:



          v[0] = 101
          v[1] = Harish
          v[2] = BAN
          v[0] = 102
          v[1] = Srinu
          v[2] = HYD


          Is that what you expected it to do?






          share|improve this answer




















          • Thank you. It is working.
            – Harish a
            Dec 1 at 10:37














          up vote
          0
          down vote













          There are several issues with your script.



          • The name of the variable in V=($DEL_REMOVE) should be V=($DELIM_REMOVE)

          • You are setting IFS to a comma (,) but you are removing the comma with sed.

          By doing those two changes your script starts to do something reasonable.



          Making some other changes, your script becomes:



          #!/bin/bash

          IFS=' ' # Use the space to split
          set -f # Avoid globing of values with *,? or
          while read -r line # read the variable without removing backslash
          do
          v=( $line//,/ ) # Convert to an array by splitting with the shell.
          for i in "$!v[@]"; do
          printf 'v[%s] = %sn' "$i" "$v[i]"
          done
          done < file1.txt


          Which will print:



          v[0] = 101
          v[1] = Harish
          v[2] = BAN
          v[0] = 102
          v[1] = Srinu
          v[2] = HYD


          Is that what you expected it to do?






          share|improve this answer




















          • Thank you. It is working.
            – Harish a
            Dec 1 at 10:37












          up vote
          0
          down vote










          up vote
          0
          down vote









          There are several issues with your script.



          • The name of the variable in V=($DEL_REMOVE) should be V=($DELIM_REMOVE)

          • You are setting IFS to a comma (,) but you are removing the comma with sed.

          By doing those two changes your script starts to do something reasonable.



          Making some other changes, your script becomes:



          #!/bin/bash

          IFS=' ' # Use the space to split
          set -f # Avoid globing of values with *,? or
          while read -r line # read the variable without removing backslash
          do
          v=( $line//,/ ) # Convert to an array by splitting with the shell.
          for i in "$!v[@]"; do
          printf 'v[%s] = %sn' "$i" "$v[i]"
          done
          done < file1.txt


          Which will print:



          v[0] = 101
          v[1] = Harish
          v[2] = BAN
          v[0] = 102
          v[1] = Srinu
          v[2] = HYD


          Is that what you expected it to do?






          share|improve this answer












          There are several issues with your script.



          • The name of the variable in V=($DEL_REMOVE) should be V=($DELIM_REMOVE)

          • You are setting IFS to a comma (,) but you are removing the comma with sed.

          By doing those two changes your script starts to do something reasonable.



          Making some other changes, your script becomes:



          #!/bin/bash

          IFS=' ' # Use the space to split
          set -f # Avoid globing of values with *,? or
          while read -r line # read the variable without removing backslash
          do
          v=( $line//,/ ) # Convert to an array by splitting with the shell.
          for i in "$!v[@]"; do
          printf 'v[%s] = %sn' "$i" "$v[i]"
          done
          done < file1.txt


          Which will print:



          v[0] = 101
          v[1] = Harish
          v[2] = BAN
          v[0] = 102
          v[1] = Srinu
          v[2] = HYD


          Is that what you expected it to do?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 1 at 6:42









          Isaac

          10.8k11447




          10.8k11447











          • Thank you. It is working.
            – Harish a
            Dec 1 at 10:37
















          • Thank you. It is working.
            – Harish a
            Dec 1 at 10:37















          Thank you. It is working.
          – Harish a
          Dec 1 at 10:37




          Thank you. It is working.
          – Harish a
          Dec 1 at 10:37

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485303%2fhow-to-change-string-values-to-index-value-to-use-in-array%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






          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay