LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 08-13-2006, 12:53 AM   #1
magnum81
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?

Nevermind.
-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
thedonkdonk
Member
 
Registered: Oct 2005
Location: San Jose, CA
Distribution: Fedora, RHEL, CentOS
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
witkacy26
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
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
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.
 
  


Reply

Tags
grep



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:39 PM.

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