How can I store each separate entire line in a text file into an 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 have a file called "threewords". It contains the information:



#gray speedy bee

gr-A | sp-E-d-E | b-E

#gray greedy pea

gr-A | gr-E-d-E | p-E


When I run the command:



cat threewords | grep ^# | cut -c2-


The command pulls the two lines beginning with #. It then removes the # and returns this as output:



gray speedy bee

gray greedy pea


When I run my command:



array=($(cat threewords | grep ^# | cut -c2-))


It creates the array but it separates all the words into separate array positions like this:



array[0] = gray,
array[1] = speedy,
array[2] = bee,
array[3] = gray,
array[4] = greedy,
array[5] = pea


I can figure out the code to make it put the output of each line into an array like so:



array[0] = gray speedy bee, 
array[1] = gray greedy pea









share|improve this question



















  • 1




    this is a terrible idea.
    – mikeserv
    Dec 14 '14 at 1:18














up vote
0
down vote

favorite












I have a file called "threewords". It contains the information:



#gray speedy bee

gr-A | sp-E-d-E | b-E

#gray greedy pea

gr-A | gr-E-d-E | p-E


When I run the command:



cat threewords | grep ^# | cut -c2-


The command pulls the two lines beginning with #. It then removes the # and returns this as output:



gray speedy bee

gray greedy pea


When I run my command:



array=($(cat threewords | grep ^# | cut -c2-))


It creates the array but it separates all the words into separate array positions like this:



array[0] = gray,
array[1] = speedy,
array[2] = bee,
array[3] = gray,
array[4] = greedy,
array[5] = pea


I can figure out the code to make it put the output of each line into an array like so:



array[0] = gray speedy bee, 
array[1] = gray greedy pea









share|improve this question



















  • 1




    this is a terrible idea.
    – mikeserv
    Dec 14 '14 at 1:18












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a file called "threewords". It contains the information:



#gray speedy bee

gr-A | sp-E-d-E | b-E

#gray greedy pea

gr-A | gr-E-d-E | p-E


When I run the command:



cat threewords | grep ^# | cut -c2-


The command pulls the two lines beginning with #. It then removes the # and returns this as output:



gray speedy bee

gray greedy pea


When I run my command:



array=($(cat threewords | grep ^# | cut -c2-))


It creates the array but it separates all the words into separate array positions like this:



array[0] = gray,
array[1] = speedy,
array[2] = bee,
array[3] = gray,
array[4] = greedy,
array[5] = pea


I can figure out the code to make it put the output of each line into an array like so:



array[0] = gray speedy bee, 
array[1] = gray greedy pea









share|improve this question















I have a file called "threewords". It contains the information:



#gray speedy bee

gr-A | sp-E-d-E | b-E

#gray greedy pea

gr-A | gr-E-d-E | p-E


When I run the command:



cat threewords | grep ^# | cut -c2-


The command pulls the two lines beginning with #. It then removes the # and returns this as output:



gray speedy bee

gray greedy pea


When I run my command:



array=($(cat threewords | grep ^# | cut -c2-))


It creates the array but it separates all the words into separate array positions like this:



array[0] = gray,
array[1] = speedy,
array[2] = bee,
array[3] = gray,
array[4] = greedy,
array[5] = pea


I can figure out the code to make it put the output of each line into an array like so:



array[0] = gray speedy bee, 
array[1] = gray greedy pea






grep array cut






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 13 at 11:00









Jeff Schaller

32.5k849110




32.5k849110










asked Dec 13 '14 at 22:52









Ben

413




413







  • 1




    this is a terrible idea.
    – mikeserv
    Dec 14 '14 at 1:18












  • 1




    this is a terrible idea.
    – mikeserv
    Dec 14 '14 at 1:18







1




1




this is a terrible idea.
– mikeserv
Dec 14 '14 at 1:18




this is a terrible idea.
– mikeserv
Dec 14 '14 at 1:18










2 Answers
2






active

oldest

votes

















up vote
2
down vote













The splitting is done with IFS as the delimiter (which contains a space, newline and tab by default). Set the IFS to only the newline:



$ IFS=$'n' a=($(printf "1 2n2 3n"))
$ echo $a[0]
1 2
$ echo $a[1]
2 3


This will change IFS for the shell, so best save it before and restore it:



OLD_IFS="$IFS"
IFS=$'n' array=($(grep '^#' threewords | cut -c2-))
IFS="$OLD_IFS"


And there's absolutely no reason to do:



cat threewords | grep '^#'


grep is perfectly capable of reading files:



grep '^#' threewords


As Stephane notes, when subjecting the output of a command to further shell expansion, one should disable globbing using set -f:



$ help set 
...
-f Disable file name generation (globbing).


Otherwise:



$ cd /; a=( $(printf "*n") )
$ echo $a[@]
bin boot cdrom dev etc home ...





share|improve this answer


















  • 2




    You also need set -f.
    – Stéphane Chazelas
    Dec 13 '14 at 23:10

















up vote
2
down vote













If you have bash 4



mapfile -t array < <(grep ^# threewords | cut -c2-)


Will populate array, one line per element



printf "%sn" "$array[@]"
gray speedy bee
gray greedy pea





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%2f174111%2fhow-can-i-store-each-separate-entire-line-in-a-text-file-into-an-array%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    The splitting is done with IFS as the delimiter (which contains a space, newline and tab by default). Set the IFS to only the newline:



    $ IFS=$'n' a=($(printf "1 2n2 3n"))
    $ echo $a[0]
    1 2
    $ echo $a[1]
    2 3


    This will change IFS for the shell, so best save it before and restore it:



    OLD_IFS="$IFS"
    IFS=$'n' array=($(grep '^#' threewords | cut -c2-))
    IFS="$OLD_IFS"


    And there's absolutely no reason to do:



    cat threewords | grep '^#'


    grep is perfectly capable of reading files:



    grep '^#' threewords


    As Stephane notes, when subjecting the output of a command to further shell expansion, one should disable globbing using set -f:



    $ help set 
    ...
    -f Disable file name generation (globbing).


    Otherwise:



    $ cd /; a=( $(printf "*n") )
    $ echo $a[@]
    bin boot cdrom dev etc home ...





    share|improve this answer


















    • 2




      You also need set -f.
      – Stéphane Chazelas
      Dec 13 '14 at 23:10














    up vote
    2
    down vote













    The splitting is done with IFS as the delimiter (which contains a space, newline and tab by default). Set the IFS to only the newline:



    $ IFS=$'n' a=($(printf "1 2n2 3n"))
    $ echo $a[0]
    1 2
    $ echo $a[1]
    2 3


    This will change IFS for the shell, so best save it before and restore it:



    OLD_IFS="$IFS"
    IFS=$'n' array=($(grep '^#' threewords | cut -c2-))
    IFS="$OLD_IFS"


    And there's absolutely no reason to do:



    cat threewords | grep '^#'


    grep is perfectly capable of reading files:



    grep '^#' threewords


    As Stephane notes, when subjecting the output of a command to further shell expansion, one should disable globbing using set -f:



    $ help set 
    ...
    -f Disable file name generation (globbing).


    Otherwise:



    $ cd /; a=( $(printf "*n") )
    $ echo $a[@]
    bin boot cdrom dev etc home ...





    share|improve this answer


















    • 2




      You also need set -f.
      – Stéphane Chazelas
      Dec 13 '14 at 23:10












    up vote
    2
    down vote










    up vote
    2
    down vote









    The splitting is done with IFS as the delimiter (which contains a space, newline and tab by default). Set the IFS to only the newline:



    $ IFS=$'n' a=($(printf "1 2n2 3n"))
    $ echo $a[0]
    1 2
    $ echo $a[1]
    2 3


    This will change IFS for the shell, so best save it before and restore it:



    OLD_IFS="$IFS"
    IFS=$'n' array=($(grep '^#' threewords | cut -c2-))
    IFS="$OLD_IFS"


    And there's absolutely no reason to do:



    cat threewords | grep '^#'


    grep is perfectly capable of reading files:



    grep '^#' threewords


    As Stephane notes, when subjecting the output of a command to further shell expansion, one should disable globbing using set -f:



    $ help set 
    ...
    -f Disable file name generation (globbing).


    Otherwise:



    $ cd /; a=( $(printf "*n") )
    $ echo $a[@]
    bin boot cdrom dev etc home ...





    share|improve this answer














    The splitting is done with IFS as the delimiter (which contains a space, newline and tab by default). Set the IFS to only the newline:



    $ IFS=$'n' a=($(printf "1 2n2 3n"))
    $ echo $a[0]
    1 2
    $ echo $a[1]
    2 3


    This will change IFS for the shell, so best save it before and restore it:



    OLD_IFS="$IFS"
    IFS=$'n' array=($(grep '^#' threewords | cut -c2-))
    IFS="$OLD_IFS"


    And there's absolutely no reason to do:



    cat threewords | grep '^#'


    grep is perfectly capable of reading files:



    grep '^#' threewords


    As Stephane notes, when subjecting the output of a command to further shell expansion, one should disable globbing using set -f:



    $ help set 
    ...
    -f Disable file name generation (globbing).


    Otherwise:



    $ cd /; a=( $(printf "*n") )
    $ echo $a[@]
    bin boot cdrom dev etc home ...






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 13 '17 at 12:36









    Community♦

    1




    1










    answered Dec 13 '14 at 22:59









    muru

    33.6k577144




    33.6k577144







    • 2




      You also need set -f.
      – Stéphane Chazelas
      Dec 13 '14 at 23:10












    • 2




      You also need set -f.
      – Stéphane Chazelas
      Dec 13 '14 at 23:10







    2




    2




    You also need set -f.
    – Stéphane Chazelas
    Dec 13 '14 at 23:10




    You also need set -f.
    – Stéphane Chazelas
    Dec 13 '14 at 23:10












    up vote
    2
    down vote













    If you have bash 4



    mapfile -t array < <(grep ^# threewords | cut -c2-)


    Will populate array, one line per element



    printf "%sn" "$array[@]"
    gray speedy bee
    gray greedy pea





    share|improve this answer
























      up vote
      2
      down vote













      If you have bash 4



      mapfile -t array < <(grep ^# threewords | cut -c2-)


      Will populate array, one line per element



      printf "%sn" "$array[@]"
      gray speedy bee
      gray greedy pea





      share|improve this answer






















        up vote
        2
        down vote










        up vote
        2
        down vote









        If you have bash 4



        mapfile -t array < <(grep ^# threewords | cut -c2-)


        Will populate array, one line per element



        printf "%sn" "$array[@]"
        gray speedy bee
        gray greedy pea





        share|improve this answer












        If you have bash 4



        mapfile -t array < <(grep ^# threewords | cut -c2-)


        Will populate array, one line per element



        printf "%sn" "$array[@]"
        gray speedy bee
        gray greedy pea






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 14 '14 at 0:44









        iruvar

        11.6k62959




        11.6k62959



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f174111%2fhow-can-i-store-each-separate-entire-line-in-a-text-file-into-an-array%23new-answer', 'question_page');

            );

            Post as a guest













































































            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