How to count the number of lines in a file after a grep match

csvfilesgrep

I am trying to count the number of lines after a problematic row in a csv file. I am aware I can use the grep -a # syntax to output # number of lines after a match has been found. I'm only interested in the actual number of lines. I realize I could set the number to MAX_INT, pipe it into a file and do some more processing.

I'm looking for a succinct one-liner to just tell me the count.

Any suggestions?

Best Answer

{ grep -m1 match; grep -c ''; } <file

That will work w/ GNU grep and an lseek()able infile. The first grep will stop at 1 -match, and the second will -count every line remaining in input.

Without GNU grep:

{ sed '/match/q'; grep -c ''; } <file

Of course, w/ grep you can use any/all of its other options besides, and stopping at one match is not at all necessary.

Related Question