Results 1 to 9 of 9

Thread: Need help using grep

  1. #1
    Join Date
    Mar 2008
    Beans
    88
    Distro
    Ubuntu 12.04 Precise Pangolin

    Need help using grep

    I need help greping a log. I want to pull the last line containing the word “project” in my Folding@Home log and output it to my conky.


    Code:
    cat FAHlog.txt | grep Project
    
    [08:39:28] Project: 8072 (Run 0, Clone 284, Gen 48)
    [19:31:37] Project: 8072 (Run 0, Clone 284, Gen 48)
    [01:46:05] Project: 8072 (Run 0, Clone 284, Gen 48)
    [05:47:48] Project: 8072 (Run 0, Clone 284, Ge
    [05:47:50] Project: 7660 (Run 253, Clone 0, Gen 18)

    How do I isolate just the last entry for “project” using grep?
    Your first step, learn the command line man intro man man man bash

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Need help using grep

    pipe it through 'tail -1'? also you don't need the 'cat' e.g.

    Code:
    grep 'Project' FAHlog.txt | tail -1

  3. #3
    Join Date
    Mar 2008
    Beans
    88
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Need help using grep

    WooHoo,TY steeldriver. That was fast.
    Your first step, learn the command line man intro man man man bash

  4. #4
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Need help using grep

    You're welcome - there may be a more elegant way of course

    'grep' *does* allow you to stop after the mth match so I guess you could cat it backwards and find the first match

    Code:
     tac FAHlog.txt | grep -m1 'Project'
    but I doubt it's more efficient. Also the 'cat haters' will probably er.. have kittens at 'tac'

  5. #5
    Join Date
    Nov 2010
    Location
    India
    Beans
    Hidden!

    Re: Need help using grep

    please mark your thread as solved .
    Dont miss anything even it is small. one small pin is enough to bring down a man.


  6. #6
    Join Date
    Jan 2010
    Location
    Sydney, Australia
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Need help using grep

    An awk solution below, however steeldriver's solution is easier to the eyes.

    Code:
    awk '/Project/{} END{print}' FAHlog.txt
    “Progress is made by lazy men looking for easier ways to do things”
    — Robert A. Heinlein

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

    Re: Need help using grep

    Code:
    awk '/Project/{} END{print}' FAHlog.txt
    that doesn't work
    Code:
    $ echo $'a1\nb\na2\nc' | awk '/a/{} END{ print; }'
    c
    $ echo $'a1\nb\na2\nc' | awk '/a/{ x=$0; } END{ print x; }'
    a2
    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

  8. #8
    Join Date
    Jan 2010
    Location
    Sydney, Australia
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Need help using grep

    thanks for the fix vaphell , i can see it the bug.
    “Progress is made by lazy men looking for easier ways to do things”
    — Robert A. Heinlein

  9. #9
    Join Date
    Feb 2013
    Beans
    9

    Re: Need help using grep

    I know this is marked solved, but thanks guys. As someone new to linux (but familiar with piping via Powershell), this is the kind of stuff that helps me the most.

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
  •