Find the answer to your Linux question:
Results 1 to 9 of 9
Hi I'm referring to the following thread: Grep last pattern output My question is: Which code has to be used, if we wanted to show the last pattern of each ...
  1. #1
    Just Joined!
    Join Date
    Nov 2011
    Posts
    5

    Grep last pattern output of 2 (or more) input files

    Hi
    I'm referring to the following thread:
    Grep last pattern output

    My question is:
    Which code has to be used, if we wanted to show the last pattern of each two (or more) input files?

    Example:

    File 1:
    linux 56
    slackware 32
    debian 34
    linux 2
    redhat 9

    File 2:
    linux 11
    slackware xx
    debian 2
    linux 3
    redhat 7

    The output should be:

    linux 2
    linux 3



    Thanks for your help.

  2. #2
    Trusted Penguin Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,393
    If you can't do your homework you should be discussing this with your tutor or teacher rather than asking us to do it for you.

    Take a look at the man page for grep, it tells you all you need to know.
    Linux user #126863 - see http://linuxcounter.net/

  3. #3
    Just Joined!
    Join Date
    Nov 2011
    Posts
    5
    Its not part of a exercise, but thanks anyway. I don't know if you understand my problem. The referred problem was simpler and even that was solved by using additional programs to grep. Thats why I was not expecting much from going through the grep manual.

  4. #4
    Linux Enthusiast Mudgen's Avatar
    Join Date
    Feb 2007
    Location
    Virginia
    Posts
    623
    I don't think it's possible to understand your problem from your description. For the input/output specified
    grep -h "linux [2,3]" *
    would do it. But those are not the last patterns in the files, so your explanation of your homework problem doesn't make sense.

  5. #5
    Just Joined!
    Join Date
    Nov 2011
    Posts
    5
    Thanks. I think you got my problem. But now lets I assume, the file would be much larger and it we would not know the LAST LINE in the file, containing "linux", so we dont know, that these lines containing 2 and 3. I am looking for a code, which gives from both files the last of all lines, containing "linux", independently from the other contents of the line.
    And I must insist, that this is not a homework problem. I just try to learn sth which is useful for my studies but NOT ASKED BY ANY PROFESSOR!!!

  6. #6
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    To grab the last file that matches, I'd pipe the output of grep to tail. If you want the number of the line in question, check out the '-n' flag.

  7. #7
    Just Joined!
    Join Date
    Nov 2011
    Posts
    5
    Well thanks. The problem is, if I use
    tail --lines=1
    it replies
    file2:linux 3
    if I use
    tail --lines=2
    it replies
    file2:linux 11
    file2:linux 3


    But I am looking for
    file1:linux 2
    file2:linux3

  8. #8
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    i was assuming you were iterating over each file, my bad. do something like:
    Code:
    #!/bin/bash
    # pass files as arguments to script
    files=$@
    # loop thru files
    for file in $files; do
      grep -H linux $file|tail -n1
    done

  9. #9
    Just Joined!
    Join Date
    Nov 2011
    Posts
    5
    It works!!! Thats what I was looking forč

    Thanks for your help.




    ?Do I have to do sth to close the thread, or show that my problem is solved? Did not find a directive.

Posting Permissions

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