LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-09-2008, 10:34 PM   #1
ryanlum
Member
 
Registered: Jul 2007
Posts: 47

Rep: Reputation: 15
Linux Scripting


Hey guyz,

lets say i have this template.

xxxx AAA xxxx

Total number of bytes =xx
total uplink rate = xx

Lets say i grep for xxxx AAA xxxx
from there i want to get the whole line for 3 lines down
how do i do that?

means final out put i want the xx from total uplink rate
 
Old 06-09-2008, 10:44 PM   #2
eggixyz
Member
 
Registered: Apr 2008
Posts: 310

Rep: Reputation: 30
Hey There,

Gnu's version of grep has a -A argument

grep -A 3 yourString

-B works the same way, except the argument is the number of lines Before your match

to get your line and the 3 lines following.

You can also do it with sed.

Hope that helps

, Mike
 
Old 06-09-2008, 11:20 PM   #3
ryanlum
Member
 
Registered: Jul 2007
Posts: 47

Original Poster
Rep: Reputation: 15
hey.. its not working..

cat test.sh | grep -A 2 for
grep: illegal option -- A
Usage: grep -hblcnsviw pattern file . . .
^[cat test.sh | grep -B
grep: illegal option -- B
Usage: grep -hblcnsviw pattern file . . .
 
Old 06-09-2008, 11:46 PM   #4
ryanlum
Member
 
Registered: Jul 2007
Posts: 47

Original Poster
Rep: Reputation: 15
hey it works
i was using it on a SUN platform earlier thats why
 
Old 06-10-2008, 12:14 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
On the sun, you could try using awk:

awk '/xxxx AAA xxxx/{ end=NR+3 }
NR <= end { print $0 }
NR > end { end = 0 }' file

If you don't want the xxxx AAA xxxx line printed:
awk '/xxxx AAA xxxx/{ end=NR+3; start=NR+1 }
NR <= end && NR >= start { print $0 }
NR > end { end = 0 }' simple

Code:
~> cat file
xxxx AAA xxxx

Total number of bytes = 56
total uplink rate = 27

abc
def

xxxx AAA xxxx

Total number of bytes =xx
total uplink rate = 30

agh
~> awk '/xxxx AAA xxxx/{ end=NR+3; start=NR+1 }
NR <= end && NR >= start { print $0 }
NR > end { end = 0 }' file

Total number of bytes = 56
total uplink rate = 27

Total number of bytes =xx
total uplink rate = 30

Last edited by jschiwal; 06-10-2008 at 12:19 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
Linux scripting 151803 Linux - Newbie 11 03-23-2007 10:21 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
linux scripting redhatbeatswin Linux - Newbie 2 11-15-2004 01:02 PM
Linux scripting Cybercool Linux - Newbie 5 04-16-2004 01:57 AM
Linux scripting... WeNdeL Linux - Newbie 7 10-20-2002 12:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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