<p>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. </p>

<p>A sample complete sentence looks like:</p>

<pre><code>$GPRMC,005727.000,A,3751.9418,S,14502.2569,E,0.00,339.17,210808,,,A*76
</code></pre>

<p>The line should start with a <code>$</code> sign, and end with an <code>*</code> 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.</p>

<p>The following Python code might work:   </p>

<pre><code>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)
</code></pre>

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

<p><strong>Update</strong>: 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 <code>$</code> before it would work:</p>

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

<p>Two further questions arise, how can I make it ignore blank lines?  And can I combine the last two <code>grep</code>s?</p>

[{'answer_id': 36779, 'author': 'Motti', 'author_id': 3848, 'author_profile': 'https://Stackoverflow.com/users/3848', 'pm_score': 2, 'selected': False, 'text': '<p>The minimum of testing shows that this should do it:</p>\n\n<pre><code>grep -Ev "^\\$.*\\*[0-9A-Fa-f]{2}$" a.txt | grep -v ADVER\n</code></pre>\n\n<ul>\n<li>-E use extended regexp</li>\n<li>-v Show lines that do <strong>not</strong> match</li>\n<li>^ starts with</li>\n<li>.* anything</li>\n<li>\\* an asterisk </li>\n<li>[0-9A-Fa-f] hexadecimal digit</li>\n<li>{2} exactly two of the previous</li>\n<li>$ end of line</li>\n<li>| <code>grep -v ADVER</code> weed out the ADVER lines </li>\n</ul>\n\n<p>HTH, Motti.</p>\n'}, {'answer_id': 36842, 'author': 'Paul Tomblin', 'author_id': 3333, 'author_profile': 'https://Stackoverflow.com/users/3333', 'pm_score': 1, 'selected': False, 'text': '<p>@Motti\'s answer doesn\'t ignore ADVER lines, but you easily pipe the results of that grep to another:</p>\n\n<pre><code>grep -Ev "^\\$.*\\*[0-9A-Fa-f]{2}$" a.txt |grep -v ADVER\n</code></pre>\n'}, {'answer_id': 39021, 'author': 'Motti', 'author_id': 3848, 'author_profile': 'https://Stackoverflow.com/users/3848', 'pm_score': 1, 'selected': False, 'text': '<blockquote>\n  <p>@Tom (rephrased) I had to remove the trailing $ for it to work</p>\n</blockquote>\n\n<p>Removing the $ means that the line may end with something else (e.g. the following will be accepted)</p>\n\n<pre><code>$GPRMC,005727.000,A,3751.9418,S,14502.2569,E,0.00,339.17,210808,,,A*76xxx\n</code></pre>\n\n<blockquote>\n  <p>@Tom And can I combine the last two greps?</p>\n</blockquote>\n\n<pre><code>grep -Ev "ADVER|ADPMB"\n</code></pre>\n'}, {'answer_id': 39516, 'author': 'Tom', 'author_id': 3715, 'author_profile': 'https://Stackoverflow.com/users/3715', 'pm_score': 0, 'selected': False, 'text': "<p>@Motti: Combining the <code>grep</code>s isn't working, it's having no effect.</p>\n\n<p>I understand that without the trailing <code>$</code> something else may folow the checksum &amp; still match, but it didn't work at all with it so I had no choice...</p>\n\n<p>GNU grep 2.5.3 and GNU bash 3.2.39(1) if that makes any difference.</p>\n\n<p>And it looks like the log files are using DOS line-breaks (CR+LF).  Does <code>grep</code> need a switch to handle that properly?</p>\n"}, {'answer_id': 43336, 'author': 'Motti', 'author_id': 3848, 'author_profile': 'https://Stackoverflow.com/users/3848', 'pm_score': 0, 'selected': False, 'text': "<p>@Tom</p>\n\n<blockquote>\n  <p>GNU grep 2.5.3 and GNU bash 3.2.39(1) if that makes any difference.\n  And it looks like the log files are using DOS line-breaks (CR+LF). Does grep need a switch to handle that properly?</p>\n</blockquote>\n\n<p>I'm using <code>grep (GNU grep) 2.4.2</code> on Windows (for shame!) and it works for me (and DOS line-breaks are naturally accepted) , I don't really have access to other OSs at the moment so I'm sorry but I won't be able to help you any further :o(</p>\n"}]
['https://Stackoverflow.com/questions/36742', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3715/']