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.
> 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.What do you mean by "grep out" ? Do you want to display those lines, or skip those lines? Do you want to see the "bar" line? Is that included in the 5 lines? Anyway, you probably want to use "sed" here, rather than "grep". -- rgds Stephen
On 8/28/07, Scott McClanahan <scott.mcclanahan at trnswrks.com> wrote:> 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.'grep -A 5 bar foo' should do the trick. From the grep manpage -> -A NUM, --after-context=NUM Print NUM lines of trailing context after matching lines. Places a line containing -- between contiguous groups of matches. -- During times of universal deceit, telling the truth becomes a revolutionary act. George Orwell
On Aug 28, 2007, at 10:05 AM, Scott McClanahan wrote:> 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.for the line matching "bar" and the next five lines after it: $ grep -A 5 bar foo if you don't want the line matching "bar": $ grep -A 5 bar foo | tail -5 -steve -- If this were played upon a stage now, I could condemn it as an improbable fiction. - Fabian, Twelfth Night, III,v
> > 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.
Scott McClanahan wrote:> grep out the next 5 lines after the first and only instanceThe scope of grep's view of the world is a single line. At any one time, it knows nothing more. If you need to deal with multiple lines, I suggest perl. Untested code: #!/usr/bin/perl while (<>) { if (m/bar/) { for ($i = 0; $i < 5 and defined($_); ++$i) { print; $_ = <>; } last; } }
Rodrigo Barbosa wrote:> > Ick. I hate perl. > > If I find something I can't do in bash/sed/awk, I just code it in C :)Ick. I hate C. ;) If I can't use grep, I'll use an short inline Perl script. Or, in extreme cases, I'll write a small utility script. Or, more commonly, I'll write a huge inline Perl monstrosity that really should have been done as a utility script. (It's really hard to sort out nested loops and conditionals without any formatting...) -- Bowie
On Tue, 2007-08-28 at 10:05 -0400, Scott McClanahan wrote:> 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.I know I'm late on this and there have been many fine replies. I prefer csplit for tasks like this. As always with *IX, many ways to skin the cat.> <snip>-- Bill