Find the answer to your Linux question:
Results 1 to 5 of 5
Hey all, I'm doing a grep on a bunch of files which always results in 2 lines that match. meaning that when i do : grep "x" filename, the result ...
  1. #1
    Just Joined!
    Join Date
    Feb 2005
    Posts
    37

    Pretty basic Bash question

    Hey all,

    I'm doing a grep on a bunch of files which always results in 2 lines that match. meaning that when i do :

    grep "x" filename, the result will always be

    1: x byebye
    2: x hello

    How do I only grab the second results. I want to save "x hello" but not "x byebye" How can I do this? Thanks!

  2. #2
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422
    So you only want to save the last Result found?
    Since I do not really 100% get your Question.
    New Users, please read this..
    Google first, then ask..

  3. #3
    Just Joined!
    Join Date
    Feb 2005
    Posts
    37
    Hi RobinVossen

    Yes! I would be saving only the last result found in a variable. Thanks!

    Jon

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Try this:

    Code:
    awk '$1=="x"{f++} f==2{print;exit}' file
    Regards

  5. #5
    Linux Newbie
    Join Date
    Nov 2007
    Location
    Planet Earth
    Posts
    152
    or just simply:

    Code:
    myvar=`grep "x" your_file | tail -1`
    EOF

Posting Permissions

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