1

what command would I use to see all lines from a file starting with a line that I find with grep

so I imagine something like a tail, but beginning from a line that I identify with a grep.

For example I am looking at a log and I can identify a point that has a time in it. So I want to see all log lines that come after the log line that contains a particular date time.

3

this should do it:

sed -n -e "/pattern/,\$p" filename
1

The -A option for grep can do this. Normal usage is for context after matches, and -B for context before matches. The -m option limits the number of matches.

grep -A1000000 -m1 <search> <file>

will output up to 1000000 trailing lines of context (and to make this work just ensure the number is greater than the number of lines in the file -- and if your file is more than 1000000 lines long, grep is probably not the right tool). In a similar fashion,

grep -B1000000 -m1 <search> <file>

will print out all lines up to and including the first match. (This is not, to my knowledge, the intended use of these options, but is a natural side-effect of how they work: the alternative is to use a program other than grep.)

  • No need for one billion lines of context and yes, you can do it with gnu grep regardless of the no. of lines in your input: { grep -m1 pattern; cat; } <infile – don_crissti Feb 19 '16 at 17:23
  • @don_crissti Do I get this properly: grep will stop reading the file after the first match and the rest will be displayed because cat takes over the rest of the input since the whole file is passed to the group? – Fiximan Feb 19 '16 at 18:50
  • 1
    @Fiximan - that is correct buddy, see mikeserv's explanation here (this question is almost a duplicate of that one) – don_crissti Feb 19 '16 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.