How to grep for few lines before or after search string with grep command

The grep command in linux/unix allows you find the keyword you are searching, instead of return a particular line that contains the keyword, it allows you to specify how many lines to return by specifying -A  or -B option as below

$ ifconfig | grep -i eth0 -A 5  

Will give you 5 lines after the keyword eth0 was hit in the process of search.


$ ifconfig | grep -i eth1 -B 2  

Will give you 2 lines before the keyword eth1 was hit in the process of search.

Comments

Popular posts from this blog

How to disable USB ports on Mac OS X Mavericks, Yosemite or El Capitan ?

How to enable root login in gnome on Debian 6 or 7