unix

How do I print the line containing a pattern and 2 lines before it?

To print the line containing a pattern and 2 lines before it, you can use this grep command:

grep -B2 “^Drive” /path/to/my/file

If your input file looks like this:

Backup
Policy
Information
Data
Drive
Size

grep -B2 “^Drive” /path/to/my/file will print this:

Information
Data
Drive

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

To Top