Find the answer to your Linux question:
Results 1 to 3 of 3
Hello, I have a bash script that runs two programs. I take the output of the first program, and re-direct it to an ouput file: Code: ns net.tcl $currentCAC > ...
  1. #1
    Just Joined!
    Join Date
    Feb 2007
    Location
    Winnipeg, MB
    Posts
    14

    Bash script manipulate output

    Hello, I have a bash script that runs two programs.

    I take the output of the first program, and re-direct it to an ouput file:
    Code:
    ns net.tcl $currentCAC > output
    Then I do a grep on the output file to get the relevant info:
    Code:
    grep KEYWORD output
    This outputs a line to the console:
    KEYWORD So that's a buffering time of 90.9121 ms
    I want to get the '90' part of that line, and store it in a bash variable. How can I do this?

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    assuming that the line where KEYWORD is found is always the same number of fields. The only changing part is the '90' part which is field 8
    Code:
    awk '/KEYWORD/{print $8 }' output

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Modern versions of grep can do this kind of extraction:
    Code:
    #!/bin/sh
    
    # @(#) s1       Demonstrate grep selection of only the matching text.
    
    set -o nounset
    echo " sh version: $BASH_VERSION"
    
    echo " KEYWORD So that's a buffering time of 90.9121 ms" |
    egrep -o '[0-9.]+'
    
    exit 0
    producing:
    Code:
    % ./s1
     sh version: 2.05b.0(1)-release
    90.9121
    cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

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