LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 01-05-2008, 03:04 AM   #1
yawe_frek
Member
 
Registered: Sep 2005
Distribution: feather 0.72-usb, DSL,CentOS,Ubuntu, Redhat 9
Posts: 144

Rep: Reputation: 15
grep question


hi friends,

I seem to be having a little problem as to when to use this options of grep command

grep -A NUM
grep -B NUM

Kindly give me varoius life examples of how and when to use this options.

Thanks
 
Old 01-05-2008, 03:28 AM   #2
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Just to clarify what they do
Code:
$ cat file
first line
second line
third line
Code:
$ grep -A 1 second file
second line
third line
Code:
$ grep -B 1 second file
first line
second line
Here's a small example (not the best of examples but it works)
Code:
$ cat /var/lib/pacman/local/zip-2.32-1/desc
%NAME%
zip

%VERSION%
2.32-1
Code:
$ grep -A 1 %NAME% /var/lib/pacman/local/zip-2.32-1/desc|tail -1
zip
Of course awk does this better, but I can't think of any examples right now
 
Old 01-05-2008, 06:09 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
There are 3 options to print the context of a match: -A (after), -B (before), and -C(before and after)

In addition to the examples above, "grep -C5 <searchterm> <filename>" gives you 5 lines before and after the line with the match.
 
Old 01-05-2008, 07:07 AM   #4
yawe_frek
Member
 
Registered: Sep 2005
Distribution: feather 0.72-usb, DSL,CentOS,Ubuntu, Redhat 9
Posts: 144

Original Poster
Rep: Reputation: 15
Thanks so much for the quick reply, i really appreciate it. lets use the output of this command for both examples. Thanks

netstat -r|grep -A num

Last edited by yawe_frek; 01-05-2008 at 07:13 AM.
 
  


Reply



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
question about grep new_2_unix Linux - Newbie 5 12-07-2007 04:45 PM
grep question tokernizer Programming 2 12-20-2005 08:02 AM
Question about Grep irfanhab Linux - Newbie 6 08-23-2004 02:57 PM
grep question djgerbavore Linux - Newbie 7 07-07-2004 07:11 AM
grep question Hockeyfan Linux - General 1 05-15-2004 02:09 AM

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

All times are GMT -5. The time now is 04:17 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