LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Linux Scripting (https://www.linuxquestions.org/questions/linux-newbie-8/linux-scripting-648184/)

ryanlum 06-09-2008 10:34 PM

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

eggixyz 06-09-2008 10:44 PM

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

ryanlum 06-09-2008 11:20 PM

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 . . .

ryanlum 06-09-2008 11:46 PM

hey it works
i was using it on a SUN platform earlier thats why

jschiwal 06-10-2008 12:14 AM

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



All times are GMT -5. The time now is 10:28 AM.