How to convert CSV file of strings seperated by a new line to arrayProblem with sed on a array containing...

Thought experiment and possible contradiction between electromagnetism and special relativity

Discussing work with supervisor in an invited dinner with his family

Can I get a PhD for developing an educational software?

Why did the population of Bhutan drop by 70% between 2007 and 2008?

How can I download a file from a host I can only SSH to through another host?

What's special ammo in Destiny 2?

Why is strlen so complex in C?

How to prevent a hosting company from accessing a VM's encryption keys?

Do you pay one or two mana to bounce a transformed Delver of Secrets with Repeal?

Why is adding AC power easier than adding DC power?

What does it take for witness testimony to be believed?

about to retire but not retired yet, employed but not working any more

Why does a sticker slowly peel off, but if it is pulled quickly it tears?

Which old Technic set included large yellow motor?

Changing JPEG to RAW to use on Lightroom?

Is first Ubuntu user root?

Who was the most successful German spy against Great Britain in WWII, from the contemporary German perspective?

Dealing with stress in coding interviews

Given current technology, could TV display screens double as video camera sensors?

What does rm -rf $(!!) mean?

Cooking Scrambled Eggs

Why is sh (not bash) complaining about functions defined in my .bashrc?

Can I renew my USA passport book and passport card separately?

Half filled water bottle



How to convert CSV file of strings seperated by a new line to array


Problem with sed on a array containing strings containing spacesHow to move or copy list of files referenced in a csv file to a new folderCSV file + set CSV parameters and values in bash scriptawk not doing a new lineBash: split multi line input into arrayHow do I form a new string from a parsed CSV line?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I have a csv file with data like this:



UserName
UserName
UserName


Each piece of data is seperated by a new line.



I need that data to be converted to an array of strings and stored in a variable.



How do I achieve this with just bash or shell execution?










share|improve this question

























  • Can there be multiple strings per line (separated by space or even tab or comma)? If yes, should they be stored as one or as multiple values?

    – Freddy
    2 hours ago











  • only one string per line, theyre each stored as a single value. each string is only seperated by a new line

    – David
    2 hours ago




















0















I have a csv file with data like this:



UserName
UserName
UserName


Each piece of data is seperated by a new line.



I need that data to be converted to an array of strings and stored in a variable.



How do I achieve this with just bash or shell execution?










share|improve this question

























  • Can there be multiple strings per line (separated by space or even tab or comma)? If yes, should they be stored as one or as multiple values?

    – Freddy
    2 hours ago











  • only one string per line, theyre each stored as a single value. each string is only seperated by a new line

    – David
    2 hours ago
















0












0








0


1






I have a csv file with data like this:



UserName
UserName
UserName


Each piece of data is seperated by a new line.



I need that data to be converted to an array of strings and stored in a variable.



How do I achieve this with just bash or shell execution?










share|improve this question














I have a csv file with data like this:



UserName
UserName
UserName


Each piece of data is seperated by a new line.



I need that data to be converted to an array of strings and stored in a variable.



How do I achieve this with just bash or shell execution?







bash shell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 hours ago









DavidDavid

53 bronze badges




53 bronze badges
















  • Can there be multiple strings per line (separated by space or even tab or comma)? If yes, should they be stored as one or as multiple values?

    – Freddy
    2 hours ago











  • only one string per line, theyre each stored as a single value. each string is only seperated by a new line

    – David
    2 hours ago





















  • Can there be multiple strings per line (separated by space or even tab or comma)? If yes, should they be stored as one or as multiple values?

    – Freddy
    2 hours ago











  • only one string per line, theyre each stored as a single value. each string is only seperated by a new line

    – David
    2 hours ago



















Can there be multiple strings per line (separated by space or even tab or comma)? If yes, should they be stored as one or as multiple values?

– Freddy
2 hours ago





Can there be multiple strings per line (separated by space or even tab or comma)? If yes, should they be stored as one or as multiple values?

– Freddy
2 hours ago













only one string per line, theyre each stored as a single value. each string is only seperated by a new line

– David
2 hours ago







only one string per line, theyre each stored as a single value. each string is only seperated by a new line

– David
2 hours ago












3 Answers
3






active

oldest

votes


















2















With mapfile:



$ mapfile -t array < <(cat yourfile)

$ declare -p array # print array content
declare -a array=([0]="UserName" [1]="UserName" [2]="UserName")





share|improve this answer




























  • mapfile -t array < yourfile will do here. mapfile reads stdin, process substitution isn't needed to read an existing file (unless you need to transform it in some way first, e.g. with grep or sed or awk). this is a UUOC with a pretty bell and collar :-) but mapfile is a good answer, so +1.

    – cas
    1 hour ago





















0















$ arr=($(<file.csv))
$ declare -p arr # print array content
declare -a arr=([0]="UserName" [1]="UserName" [2]="UserName")




  • arr=(…) create array


  • $(<file.csv) read file file.csv (like $(cat file.csv))


The elements are split at newline, space or tab characters and trimmed.






share|improve this answer



































    0















    i=0
    while read line
    do
    var[$i]="$line"
    i=$((i+1))
    done < filename



    Here var array variable stores all names. And can be accessed by



    echo ${var[0]}
    echo ${var[1]}
    ...
    ...






    share|improve this answer








    New contributor



    Navi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.
























      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',
      autoActivateHeartbeat: false,
      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%2f537566%2fhow-to-convert-csv-file-of-strings-seperated-by-a-new-line-to-array%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2















      With mapfile:



      $ mapfile -t array < <(cat yourfile)

      $ declare -p array # print array content
      declare -a array=([0]="UserName" [1]="UserName" [2]="UserName")





      share|improve this answer




























      • mapfile -t array < yourfile will do here. mapfile reads stdin, process substitution isn't needed to read an existing file (unless you need to transform it in some way first, e.g. with grep or sed or awk). this is a UUOC with a pretty bell and collar :-) but mapfile is a good answer, so +1.

        – cas
        1 hour ago


















      2















      With mapfile:



      $ mapfile -t array < <(cat yourfile)

      $ declare -p array # print array content
      declare -a array=([0]="UserName" [1]="UserName" [2]="UserName")





      share|improve this answer




























      • mapfile -t array < yourfile will do here. mapfile reads stdin, process substitution isn't needed to read an existing file (unless you need to transform it in some way first, e.g. with grep or sed or awk). this is a UUOC with a pretty bell and collar :-) but mapfile is a good answer, so +1.

        – cas
        1 hour ago
















      2














      2










      2









      With mapfile:



      $ mapfile -t array < <(cat yourfile)

      $ declare -p array # print array content
      declare -a array=([0]="UserName" [1]="UserName" [2]="UserName")





      share|improve this answer















      With mapfile:



      $ mapfile -t array < <(cat yourfile)

      $ declare -p array # print array content
      declare -a array=([0]="UserName" [1]="UserName" [2]="UserName")






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 1 hour ago

























      answered 2 hours ago









      guillermo chamorroguillermo chamorro

      50112 bronze badges




      50112 bronze badges
















      • mapfile -t array < yourfile will do here. mapfile reads stdin, process substitution isn't needed to read an existing file (unless you need to transform it in some way first, e.g. with grep or sed or awk). this is a UUOC with a pretty bell and collar :-) but mapfile is a good answer, so +1.

        – cas
        1 hour ago





















      • mapfile -t array < yourfile will do here. mapfile reads stdin, process substitution isn't needed to read an existing file (unless you need to transform it in some way first, e.g. with grep or sed or awk). this is a UUOC with a pretty bell and collar :-) but mapfile is a good answer, so +1.

        – cas
        1 hour ago



















      mapfile -t array < yourfile will do here. mapfile reads stdin, process substitution isn't needed to read an existing file (unless you need to transform it in some way first, e.g. with grep or sed or awk). this is a UUOC with a pretty bell and collar :-) but mapfile is a good answer, so +1.

      – cas
      1 hour ago







      mapfile -t array < yourfile will do here. mapfile reads stdin, process substitution isn't needed to read an existing file (unless you need to transform it in some way first, e.g. with grep or sed or awk). this is a UUOC with a pretty bell and collar :-) but mapfile is a good answer, so +1.

      – cas
      1 hour ago















      0















      $ arr=($(<file.csv))
      $ declare -p arr # print array content
      declare -a arr=([0]="UserName" [1]="UserName" [2]="UserName")




      • arr=(…) create array


      • $(<file.csv) read file file.csv (like $(cat file.csv))


      The elements are split at newline, space or tab characters and trimmed.






      share|improve this answer
































        0















        $ arr=($(<file.csv))
        $ declare -p arr # print array content
        declare -a arr=([0]="UserName" [1]="UserName" [2]="UserName")




        • arr=(…) create array


        • $(<file.csv) read file file.csv (like $(cat file.csv))


        The elements are split at newline, space or tab characters and trimmed.






        share|improve this answer






























          0














          0










          0









          $ arr=($(<file.csv))
          $ declare -p arr # print array content
          declare -a arr=([0]="UserName" [1]="UserName" [2]="UserName")




          • arr=(…) create array


          • $(<file.csv) read file file.csv (like $(cat file.csv))


          The elements are split at newline, space or tab characters and trimmed.






          share|improve this answer















          $ arr=($(<file.csv))
          $ declare -p arr # print array content
          declare -a arr=([0]="UserName" [1]="UserName" [2]="UserName")




          • arr=(…) create array


          • $(<file.csv) read file file.csv (like $(cat file.csv))


          The elements are split at newline, space or tab characters and trimmed.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 hours ago

























          answered 2 hours ago









          FreddyFreddy

          6,8481 gold badge6 silver badges24 bronze badges




          6,8481 gold badge6 silver badges24 bronze badges


























              0















              i=0
              while read line
              do
              var[$i]="$line"
              i=$((i+1))
              done < filename



              Here var array variable stores all names. And can be accessed by



              echo ${var[0]}
              echo ${var[1]}
              ...
              ...






              share|improve this answer








              New contributor



              Navi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.


























                0















                i=0
                while read line
                do
                var[$i]="$line"
                i=$((i+1))
                done < filename



                Here var array variable stores all names. And can be accessed by



                echo ${var[0]}
                echo ${var[1]}
                ...
                ...






                share|improve this answer








                New contributor



                Navi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.
























                  0














                  0










                  0









                  i=0
                  while read line
                  do
                  var[$i]="$line"
                  i=$((i+1))
                  done < filename



                  Here var array variable stores all names. And can be accessed by



                  echo ${var[0]}
                  echo ${var[1]}
                  ...
                  ...






                  share|improve this answer








                  New contributor



                  Navi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  i=0
                  while read line
                  do
                  var[$i]="$line"
                  i=$((i+1))
                  done < filename



                  Here var array variable stores all names. And can be accessed by



                  echo ${var[0]}
                  echo ${var[1]}
                  ...
                  ...







                  share|improve this answer








                  New contributor



                  Navi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.








                  share|improve this answer



                  share|improve this answer






                  New contributor



                  Navi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.








                  answered 2 hours ago









                  NaviNavi

                  1




                  1




                  New contributor



                  Navi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.




                  New contributor




                  Navi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.



































                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f537566%2fhow-to-convert-csv-file-of-strings-seperated-by-a-new-line-to-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

                      Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                      Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                      Ciclooctatetraenă Vezi și | Bibliografie | Meniu de navigare637866text4148569-500570979m