Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make grep stop after first NON-matching line

Tags:

grep

bash

I'm trying to use grep to go through some logs and only select the most recent entries. The logs have years of heavy traffic on them so it's silly to do

    tac error.log | grep 2012
    tac error.log | grep "Jan.2012" 

etc.

and wait for 10 minutes while it goes through several million lines which I already know are not going to match. I know there is the -m option to stop at the first match but I don't know of a way to make it stop at first non-match. I could do something like grep -B MAX_INT -m 1 2011 but that's hardly an optimal solution.

Can grep handle this or would awk make more sense?

like image 799
mmdanziger Avatar asked Jan 20 '12 11:01

mmdanziger


People also ask

How do I stop grep At first match?

Limiting Output with grep -m This parameter will make grep stop matching after finding N matching lines, which works great as it will limit the output to one line, always containing the first match.

How do you grep for lines that don't match?

To display only the lines that do not match a search pattern, use the -v ( or --invert-match ) option. The -w option tells grep to return only those lines where the specified string is a whole word (enclosed by non-word characters). By default, grep is case-sensitive.

How do I get my first grep match?

-m 1 means return the first match in any given file. But it will still continue to search in other files. Also, if there are two or more matched in the same line, all of them will be displayed.


2 Answers

How about using awk like this:

tac error.log | awk '{if(/2012/)print;else exit}'

This should exit as soon as a line not matching 2012 is found.

like image 94
dogbane Avatar answered Sep 30 '22 21:09

dogbane


Here is a solution in python:

# foo.py
import sys, re
for line in sys.stdin:
    if re.match(r'2012', line):
        print line,
        continue
    break

you@host> tac foo.txt | python foo.py

like image 36
guettli Avatar answered Sep 30 '22 20:09

guettli