Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-13-2006, 12:53 AM   #1
LQ Newbie
Registered: Apr 2006
Posts: 13

Rep: Reputation: 0
grep lines before and after

Is there a way to make grep display a certain number or lines before and after the targer search?

-B n for before
-N n for after
where n is the number of lines

Last edited by magnum81; 08-13-2006 at 01:00 AM.
Old 08-13-2006, 01:03 AM   #2
Registered: Oct 2005
Location: San Antonio, TX
Distribution: Fedora, RHEL 6, CentOS 6, Debian 6, Arch, Gentoo
Posts: 136

Rep: Reputation: 16
Yes. Use -A for number of lines before matching line and -B for number of line after matching line. Example.
ifconfig | grep -B 2 inet will outupt lines that will show you all of your interface names that have a IPv4 address.
Old 07-29-2011, 02:57 PM   #3
LQ Newbie
Registered: Sep 2005
Posts: 2

Rep: Reputation: 0
grep lines before and after - correction

-A is for [A]fter and -B is for [B]efore
Old 07-29-2011, 04:52 PM   #4
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
There is also -C (Context), which shows 'n' lines before & after found text.

--- rod.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
sed or grep : delete lines containing matching text raj000 Linux - General 18 09-08-2012 09:38 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
How to grep similar lines in bash? bruno buys Linux - Software 2 12-02-2005 11:56 PM
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 02:03 PM
Grep lines gsibble Linux - General 1 04-12-2004 01:30 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:38 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration