shell - how to grep the last occurrence of the line pattern -


i have file contents,

x x b x c 

i want grep last occurrence,

x c 

when try

sed -n  "/x/,/b/p" file 

it lists lines. beginning x c

i'm not sure if got question right, here answers:

  • print last occurence of x:

    grep x file | tail -1 
  • print input lines beginning first matching line:

    awk "/x/ { flag = 1 }; flag" txt 
  • print lines beginning last matching line (prints lines if no match found):

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

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -