Results 1 to 8 of 8

Thread: Need a little help with using script output text

  1. #1
    Join Date
    Apr 2007
    Beans
    891
    Distro
    Ubuntu 10.04 Lucid Lynx

    Need a little help with using script output text

    Hi,

    I need to use an script from within a script I am writing. the script which I need to invoke as part of my script returns the following text


    Code:
    =========
    books=       123
    ------------
    magazines=        628
    ------------
    multimedia=   27
    =========
    How can I parse this output and put each one of the output values into a separate variable named books, magazines...?

    Thank you.
    Ubuntu 10.04
    ATI 3470
    4 GB RAM.

  2. #2
    Join Date
    Nov 2007
    Location
    Newry, Northern Ireland
    Beans
    1,258

    Re: Need a little help with using script output text

    Can you give a little more info on the scripting language, and whether the output from the first scrip is in a file or just as a stdout stream.

    This isn't a homework question, is it?
    Can't think of anything profound or witty.
    My Blog: http://gonzothegeek.blogspot.co.uk/

  3. #3
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Need a little help with using script output text

    Hi legolas_w.

    What language are you using? Is it a awk script? Bash, Python?

    Regards.

  4. #4
    Join Date
    Apr 2007
    Beans
    891
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Need a little help with using script output text

    Sorry, I forgot that we can do the script in many languages. I am using bash (shell scripting). So I assume that awk, sed and perl can be used inside my script to check the output. The output is in stdout stream (screen and not a file).

    No it is not homework. I am writing my first large script and some places I can figure out on my own (where we need sed, perl or awk to get the part done.)

    Thanks.
    Ubuntu 10.04
    ATI 3470
    4 GB RAM.

  5. #5
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Need a little help with using script output text

    You can get the 2nd column of line that start with a known pattern using awk:
    Code:
    awk '/^books=/{print $2}' input.txt
    Where:
    /^book=/ selects only the lines that start with 'book='
    print $2 prints the 2nd column.
    The result will be:
    Code:
    123
    In order to assign it to a variable in bash you use the $() expression:
    Code:
    books=$(awk '/^books=/{print $2}' input.txt)
    I hope that helps, and points you in the right direction.
    Regards.

  6. #6
    Join Date
    Apr 2007
    Beans
    891
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Need a little help with using script output text

    Thank you, superb. I fixed the script and can read all variables using the same technique.

    am I correct that awk assumes the space between the texts as the field separator by default?
    Ubuntu 10.04
    ATI 3470
    4 GB RAM.

  7. #7
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Need a little help with using script output text

    Quote Originally Posted by legolas_w View Post
    am I correct that awk assumes the space between the texts as the field separator by default?
    Indeed.

    You can change it easily using awk's -F option. For instance, if you want the '=' to be your separator:
    Code:
    awk -F'='  '/^books=/{print $2}' input.txt
    Note that this would put the spaces into the 2nd column now:
    Code:
           123
    (that is a string that stars with several spaces and trails the value '123').

    Regards.

  8. #8
    Join Date
    May 2010
    Location
    uk
    Beans
    9,249
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Need a little help with using script output text

    Hi

    There are a whole load of ways to do this and papibe has shown you an excellent example using awk.

    I just thought i would show another method using bash parameter expansion.

    This is only a very basic script and could be made far more robust. The important lines are highlighted in bold.

    Code:
    #!/bin/bash
    
    book_num=
    mag_num=
    media_num=
    
    while read line;
    do
            # Paramater expansion
            tmp_left=${line%=*};
            tmp_right=${line##* };
    
            # Match up and save .
            case "$tmp_left" in
                    books)
                    book_num="$tmp_right";
            ;;
                    magazines)
                    mag_num="$tmp_right";
            ;;
                    multimedia)
                    media_num="$tmp_right";
            ;;
                    *)
                    # ignore ??
                    ;;
            esac
    
    done < "$1";
    
    echo "Number of books = $book_num";
    echo "Number of magazine = $mag_num";
    echo "Number of multimedia = $media_num";
    Code:
    matthew@matthew-Aspire-7540 ~ % cat t
    =========
    books=       123
    ------------
    magazines=        628
    ------------
    multimedia=   27
    =========
    matthew@matthew-Aspire-7540 ~
    Code:
    matthew@matthew-Aspire-7540 ~ % ./tt.sh t 
    Number of books = 123
    Number of magazine = 628
    Number of multimedia = 27
    matthew@matthew-Aspire-7540 ~ %
    For a basic introduction to the commands

    http://mywiki.wooledge.org/BashGuide/Parameters

    Kind regards
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

    Thinking about becoming an Ubuntu Member?

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
  •