regex - with - what does nmea data look like




How do I find broken NMEA log sentences with grep? (2)

My GPS logger occassionally leaves "unfinished" lines at the end of the log files. I think they're only at the end, but I want to check all lines just in case.

A sample complete sentence looks like:

$GPRMC,005727.000,A,3751.9418,S,14502.2569,E,0.00,339.17,210808,,,A*76

The line should start with a $ sign, and end with an * and a two character hex checksum. I don't care if the checksum is correct, just that it's present. It also needs to ignore "ADVER" sentences which don't have the checksum and are at the start of every file.

The following Python code might work:

import re
from path import path
nmea = re.compile("^\$.+\*[0-9A-F]{2}$")
for log in path("gpslogs").files("*.log"):
   for line in log.lines():
      if not nmea.match(line) and not "ADVER" in line:
         print "%s\n\t%s\n" % (log, line)

Is there a way to do that with grep or awk or something simple? I haven't really figured out how to get grep to do what I want.

Update: Thanks @Motti and @Paul, I was able to get the following to do almost what I wanted, but had to use single quotes and remove the trailing $ before it would work:

grep -nvE '^\$.*\*[0-9A-F]{2}' *.log | grep -v ADVER | grep -v ADPMB

Two further questions arise, how can I make it ignore blank lines? And can I combine the last two greps?


@Tom (rephrased) I had to remove the trailing $ for it to work

Removing the $ means that the line may end with something else (e.g. the following will be accepted)

$GPRMC,005727.000,A,3751.9418,S,14502.2569,E,0.00,339.17,210808,,,A*76xxx

@Tom And can I combine the last two greps?

grep -Ev "ADVER|ADPMB"

@Motti's answer doesn't ignore ADVER lines, but you easily pipe the results of that grep to another:

grep -Ev "^\$.*\*[0-9A-Fa-f]{2}$" a.txt |grep -v ADVER




nmea