How to grep the last occurrence of a line pattern

I’m not sure if I got your question right, so here are some shots in the dark:

  • Print last occurence of x (regex):

    grep x file | tail -1
    
  • Alternatively:

    tac file | grep -m1 x
    
  • Print file from first matching line to end:

    awk '/x/{flag = 1}; flag' file
    
  • Print file from last matching line to end (prints all lines in case of no match):

    tac file | awk '!flag; /x/{flag = 1};' | tac
    

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)