Find the answer to your Linux question:
Results 1 to 4 of 4
Hi! I'm trying to grab som info that is spat out when I run a certain command. I'm doing this in a script.. At the moment I'm putting the output ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2012
    Posts
    10

    Finding a number in a .txt file


    Hi!

    I'm trying to grab som info that is spat out when I run a certain command. I'm doing this in a script..

    At the moment I'm putting the output in a text file, output.txt and I'm trying to analyze it in the next commands.

    So, if I run:
    ListInfo > output.txt

    The number I need to catch in the textfile will sort of look like this:

    Lots and lots of information
    -
    -
    -
    THIS_IS_THE_FIRST_STRING_IM_LOOKING_FOR
    Something: Foo
    SomethingElse: Bar
    THIS_IS_THE_SECOND: 045 <--Number I want

    My question is; how can I search through output.txt, look for the first string, get the third after that, and extract the last three characters (three digit number) at the end of the line?

    Can this be done without first saving to a txt file?

    Not sure if that made sense at all..

    Thanks alot for your help!

  2. #2
    Just Joined!
    Join Date
    Oct 2012
    Location
    Delhi, India
    Posts
    19
    you could try

    listinfo | grep THIS_IS_THE_SECOND | tail -c -4

    the last char is null or newline i don't know which

  3. #3
    Just Joined!
    Join Date
    Sep 2007
    Posts
    19
    listinfo | grep -A 4 THIS_IS_THE_FIRST | tail -c 4
    idea:
    grep: use context
    tail: print last "N" bytes

    eg:
    Code:
    $ echo "Lots and lots of information
    -
    -
    -
    THIS_IS_THE_FIRST_STRING_IM_LOOKING_FOR
    Something: Foo
    SomethingElse: Bar
    THIS_IS_THE_SECOND: 045" | grep -A 4 THIS_IS_THE_FIRST_STRING_IM_LOOKING_FOR | tail -c 4    <return>
    045

  4. #4
    Linux Newbie
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    115
    I'm starting to feel like I'm the AWK pimp on this website, but it's really well suited for this sort of scripting. So assuming you want to do the following,

    - parse the output from the command "ListInfo"
    - search for "THIS_IS_THE_FIRST_STRING_IM_LOOKING_FOR"
    - once you find that, search for "THIS_IS_THE_SECOND"
    - print the last word on that line
    - then ignore the rest of the output

    then this command line might help.

    Code:
    ListInfo | awk '/THIS_IS_THE_FIRST_STRING_IM_LOOKING_FOR/ { trigger = 1; } trigger && /THIS_IS_THE_SECOND/ { print $NF; trigger = 0; }'
    If you really want the last three character of the line then replace "$NF" with "substr($0,length($0)-2" in the command line.

Posting Permissions

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