Results 1 to 8 of 8

Thread: How to read the first 3 lines of text into separate vars from a variable in shell?

  1. #1
    Join Date
    Nov 2013
    Beans
    3

    How to read the first 3 lines of text into separate vars from a variable in shell?

    Hi,

    I am receiving a text variable inside my shell script from the user. The variable is stored into $1. Now I want to read the first 3 lines of this variable and place them into 3 separate variables named v1, v2 and v3. Any hint on how can I do this?

    Thanks.

  2. #2
    Join Date
    Sep 2006
    Beans
    7,540
    Distro
    Lubuntu Development Release

    Re: How to read the first 3 lines of text into separate vars from a variable in shell

    You could pipe head into tail or use the variable NR in awk. Maybe there are better ways, too.

    What are you trying to do?

  3. #3
    Join Date
    Nov 2013
    Beans
    3

    Re: How to read the first 3 lines of text into separate vars from a variable in shell

    Thanks for the reply. After getting each line I will need to check for presence of specific words in each of those lines and that helps me decide whether the script should exit 1 or it should exit 0.

  4. #4
    Join Date
    Sep 2006
    Beans
    7,540
    Distro
    Lubuntu Development Release

    Re: How to read the first 3 lines of text into separate vars from a variable in shell

    I think you can save a few steps by using awk. What kind of task is this for, school or work?

  5. #5
    Join Date
    Nov 2013
    Beans
    3

    Re: How to read the first 3 lines of text into separate vars from a variable in shell

    It is for work.

    I thought head and tail only operate on files; in my case the input is text that is being passed to my script so I cannot do it with tail or head unless I save the text into a file and then perform tail and head on the file.

  6. #6
    Join Date
    Sep 2006
    Beans
    7,540
    Distro
    Lubuntu Development Release

    Re: How to read the first 3 lines of text into separate vars from a variable in shell

    Can you show a sample of the 3 lines and point out the text to look for and what factors are in the decision?

    head, tail and the others work on streams of text. Whether that stream comes from a file or from stdin does not matter so much.

    Code:
    for i in {1..4};do echo $i;done | head -n 3 | tail -n 1
    But awk is probably the way to do it all in one go.

  7. #7
    Join Date
    Apr 2012
    Beans
    5,524

    Re: How to read the first 3 lines of text into separate vars from a variable in shell

    If you're not hung up on using 3 separate variables v1, v2, v3 you could use a bash associative array

    Code:
    for i in {1..3}; do IFS=$'\n' read line; v[i]="$line"; done < myfile
    and then access them as v[1], v[2], v[3] e.g.

    Code:
    echo "${v[1]}"

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

    Re: How to read the first 3 lines of text into separate vars from a variable in shell

    If you are going to use an array, then you can do something like:
    Code:
    unset array
    array=( $1 )
    Or in Bash >= 4 you can use mapfile (readarray) and a here string:
    Code:
    mapfile -c 3 array <<< "$1"
    printf '%s\n' "${array[0]}" "${array[1]}" "${array[2]}"

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
  •