RE: grep

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



> 
> Not a CentOS specific question, although I am running grep on 
> CentOS 4.3
> but how would you grep out a series of lines in a file starting at a
> specific point.  For instance, if I have a file named foo and 
> I want to
> grep out the next 5 lines after the first and only instance of the
> string "bar" how could I pull that off?  Thanks so much.
> 

"man grep" is your friend here

Quoting (cause they explain it better than I):
OPTIONS
       -A NUM, --after-context=NUM
              Print NUM  lines  of  trailing  context  after  matching
lines.
              Places  a  line  containing  --  between  contiguous
groups  of
              matches.

.
.
.
.
       -m NUM, --max-count=NUM
              Stop reading a file after NUM matching lines.  If the
input  is
              standard  input  from a regular file, and NUM matching
lines are
              output, grep ensures that the standard input  is
positioned  to
              just  after the last matching line before exiting,
regardless of
              the presence of trailing context lines.  This enables a
calling
              process  to resume a search.  When grep stops after NUM
matching
              lines, it outputs any trailing context lines.  When  the
-c  or
              --count  option  is  also  used,  grep  does  not output a
count
              greater than NUM.  When the -v or --invert-match option is
also
              used, grep stops after outputting NUM non-matching lines.



So your command would look something like this:

grep -m1 -A5 bar foo


This does not handle your case of "first and only" instance however. It
will stop after the first instance of the word bar.
_______________________________________________
CentOS mailing list
CentOS@xxxxxxxxxx
http://lists.centos.org/mailman/listinfo/centos


[Index of Archives]     [CentOS]     [CentOS Announce]     [CentOS Development]     [CentOS ARM Devel]     [CentOS Docs]     [CentOS Virtualization]     [Carrier Grade Linux]     [Linux Media]     [Asterisk]     [DCCP]     [Netdev]     [Xorg]     [Linux USB]
  Powered by Linux