Page 4 of 4 FirstFirst ... 234
Results 31 to 36 of 36

Thread: Searching a string, output it to an array then delete it based on user input

  1. #31
    Join Date
    Apr 2013
    Beans
    23

    Re: Searching a string, output it to an array then delete it based on user input

    Quote Originally Posted by schragge View Post
    I guess a better solution would be using mapfile aka readarray:
    Code:
    readarray -t searchOutput < <(grep "$searchTerm" "$ADDRESSBOOK" | sort)
    This also works perfectly, and also thank you for the above comments, its a lot to take in when learning new commands. The code is much clearer now.

  2. #32
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,438
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Searching a string, output it to an array then delete it based on user input

    cool, i didn't know about readarray, i knew only about somewhat similar read -a array. I think i have to read about all builtins bash has.

    Code:
    IFS=$'\n' read -d'' -a arr < <( ... )
    unfortunately in case of null-delimited inputs which are sometimes useful/necessary i think the only way is to fall back on while read and array+=( elem )
    Last edited by Vaphell; April 13th, 2013 at 10:27 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  3. #33
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: [Bash] Searching a string, output it to an array then delete it based on user inp

    Quote Originally Posted by Vaphell View Post
    Code:
    IFS=$'\n' array=( $output )
    IFS is changed locally only for the creation of array, so you don't have to save it and then restore it
    Only after you posted the example with read -a, it occured to me that the sentence above is wrong.

    First, in this case two variables just get assigned, but there's no command word. IFS keeps the new value afterwards. Contrast it with
    Code:
    IFS=$'\n' read ...
    where read is the command word (regular builtin, this won't work with special builtins either), and the scope of IFS is restricted.

    And secondly, setting IFS won't affect the array assignment since all variables on the line get assigned first, and only then the new IFS value takes effect.Edit That is incorrect, see the next post by Vaphell.

    See POSIX for reference.
    Last edited by schragge; April 23rd, 2013 at 02:01 PM.

  4. #34
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,438
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: [Bash] Searching a string, output it to an array then delete it based on user inp

    yup, i failed hard quite ironic considering i warned against it this only shows how playing with IFS can backfire.
    I didn't know one can do multiple assignments with only whitespace separating expressions... though when i think about it it makes sense, after all you can do IFS= LANG= SOMETHING_ELSE= some_command

    array itself seems to work fine, despite that IFS blunder
    Code:
    $ x=$'a a\nb b\nc c'
    $ unset IFS
    $ array=( $x )
    $ printf "%s\n" "${array[@]}"
    a
    a
    b
    b
    c
    c
    $ IFS=$'\n' array=( $x )
    $ printf "%s\n" "${array[@]}"
    a a
    b b
    c c
    Last edited by Vaphell; April 14th, 2013 at 12:00 AM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #35
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: Searching a string, output it to an array then delete it based on user input

    @OP

    You might want to check out BashFAQ 005 (link in my signature). It explains the differences between the different methods posted in this thread.

  6. #36
    Join Date
    Apr 2013
    Beans
    23

    Re: Searching a string, output it to an array then delete it based on user input

    that is a mighty FAQ, bookmarked! I honestly didn't foresee so many ways to solve this, though as I am fairly new to performing these kinds of functions I'm just happy to get a result.

    I did read that it's imperative to UNSET IFS after use and I'm still unclear as to how it can backfire? how bad is bad in this case? As mentioned above the preferred route is to use readArray, keeps the code looking clean too!!

Page 4 of 4 FirstFirst ... 234

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •