I think that sed can be used to do this, but I'm not sure how. I'd like sed to run through a file looking for two patterns, 'a' and 'b'. When it encounters a line that matches 'b' I want to show the last line that matched 'a'. How can this be done?


Looks like this works:

sed -n '/a/{h};/b/{g;p}' file.txt
  • 1
    Or, with standard sed: sed -n '/a/h;/b/{g;p;}'
    – Kusalananda
    Oct 5 '18 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.