Your Question: How Do You Grep Two Lines in Unix?
Contents

How do you grep multiple lines after a match?

For BSD or GNU grep you can use -B num to set how many lines before the match and -A num for the number of lines after the match. If you want the same number of lines before and after you can use -C num . This will show 3 lines before and 3 lines after.

How do you grep between two patterns in Linux?

You can achieve multiline match in grep , but you need to use perl-regexp for grep ( -P – which is not supported on every platform, like OS X), so as workaround we’re replacing new lines with _ character and after grep , we’re changing them back.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest