# awk and NMEA strings

**URL:** <https://community.unix.com/t/awk-and-nmea-strings/203861>\
**Category:** Shell Programming and Scripting\
**Created:** [July 7, 2008, 5:24am UTC](https://community.unix.com/t/awk-and-nmea-strings/203861 "2008-07-07T05:24:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rleal](https://community.unix.com/letter_avatar/rleal/32/6_449c533f586939b6e5f246632a46421f.png) [@rleal](https://community.unix.com/u/rleal)\
**Post date:** [July 7, 2008, 5:24am UTC](https://community.unix.com/t/awk-and-nmea-strings/203861/1 "2008-07-07T05:24:39Z")

</div>

Hi all:

I have a file with NMEA sentences of the type:

```nohighlight
$GPVTG,012.0,T,,M,00.0,N,,K*7D
$GPRMC,180541,A,3631.874,N,00617.423,W,00.0,004.0,210608,,*36
$SDDBT,,f,,M,,F*28
$GPGLL,3631.874,N,00617.423,W,180542,A*3B
$GPVTG,009.0,T,,M,00.0,N,,K*77
$GPVTG,019.0,T,,M,00.0,N,,K*76
$GPRMC,180546,A,3631.874,N,00617.423,W,00.0,019.0,210608,,*3D
$SDDBT,,f,,M,,F*28
$GPGLL,3631.874,N,00617.423,W,180548,A*31
$GPVTG,012.0,T,,M,00.0,N,,K*7D
$GPRMC,180551,A,3631.874,N,00617.423,W,00.0,004.0,210608,,*37
$GPVTG,004.0,T,,M,00.0,N,,K*7A
$SDDBT,,f,,M,,F*28
$GPGLL,3631.874,N,00617.423,W,180554,A*3C
$GPVTG,009.0,T,,M,00.0,N,,K*77
$GPRMC,180556,A,3631.874,N,00617.423,W,00.0,017.0,210608,,*32
$SDDBT,000016,f,00005,M,0002.7,F*01
$GPVTG,017.0,T,,M,00.0,N,,K*78
$GPGLL,3631.874,N,00617.423,W,180600,A*3E
$GPVTG,038.0,T,,M,00.0,N,,K*75
$GPRMC,180601,A,3631.874,N,00617.423,W,00.0,038.0,210608,,*3E
$SDDBT,000016,f,00005,M,0002.7,F*01

```

$GPRMC for position and time, $SDDBT for depth. All sentences are sequential in time.

I can extract position and time with awk, no problem:

```nohighlight
gawk -F "," '$1=="$GPRMC" && $3 != "V" { print 20substr($10,5,2),substr($10,3,2),substr($10,1,2),substr($2,1,2),substr($2,3,2),substr($2,5,2),substr($6,1,3),substr($6,4),substr($4,1,2),substr($4,3)}' NMEAfile >output

```

and depth:

```nohighlight
gawk -F "," '$1=="$SDDBT" && $4 !="" { print $4}' NMEAfile >output

```

I would like to combine position, time and depth into one single file, only if depth is true (note that sometimes $SDDBT,,f,,M,,F\*28 -no depth info-). That is, if depth is true then use the position from the previous occurrence of $GPRMC and combine them into a single line. Sure that's possible with awk/gawk, but don't know how to do it.

Thanks in advance,

r.-

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [July 7, 2008, 6:29am UTC](https://community.unix.com/t/awk-and-nmea-strings/203861/2 "2008-07-07T06:29:27Z")

</div>

I am not sure if I got it right and what your wanted output should like, but to be sure you don't get the lines with "no depth info", why not just append that expression the the awk you already have like

```nohighlight
... && !/no depth info/

```

You used such stuff in your awk line already so.. maybe I just did not understand 🙂  
An output example might make it much easier to understand, ty.

---

<div class="post-metadata">

**Author:** ![rleal](https://community.unix.com/letter_avatar/rleal/32/6_449c533f586939b6e5f246632a46421f.png) [@rleal](https://community.unix.com/u/rleal)\
**Post date:** [July 7, 2008, 7:20am UTC](https://community.unix.com/t/awk-and-nmea-strings/203861/3 "2008-07-07T07:20:18Z")

</div>

Sorry for the misunderstanding. I cannot add your suggestion because each sentence is written in a different line. The desired output from

```nohighlight
 $GPRMC,180541,A,3631.874,N,00617.423,W,00.0,004.0,210608,,*36
$SDDBT,,f,,M,,F*28
$GPGLL,3631.874,N,00617.423,W,180542,A*3B
$GPVTG,009.0,T,,M,00.0,N,,K*77
$GPVTG,019.0,T,,M,00.0,N,,K*76
$GPRMC,180546,A,3631.874,N,00617.423,W,00.0,019.0,210608,,*3D
$SDDBT,,f,,M,,F*28
$GPGLL,3631.874,N,00617.423,W,180548,A*31
$GPVTG,012.0,T,,M,00.0,N,,K*7D
$GPRMC,180551,A,3631.874,N,00617.423,W,00.0,004.0,210608,,*37
$GPVTG,004.0,T,,M,00.0,N,,K*7A
$SDDBT,,f,,M,,F*28
$GPGLL,3631.874,N,00617.423,W,180554,A*3C
$GPVTG,009.0,T,,M,00.0,N,,K*77
$GPRMC,180556,A,3631.874,N,00617.423,W,00.0,017.0,210608,,*32
$SDDBT,000016,f,00005,M,0002.7,F*01
$GPVTG,017.0,T,,M,00.0,N,,K*78

```

would be

```nohighlight
 
210608 180556 36 31.874 00617.423 00005

```

What it should do is get the depth (if not empty) and grab the corresponding lat,lon,time from the immediately previous $GPRMC line. In this example the first 3 $SDDBT sentences have no depth info (with comma as separators ,, means empty value).

best regs,

r.-

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [July 7, 2008, 7:39am UTC](https://community.unix.com/t/awk-and-nmea-strings/203861/4 "2008-07-07T07:39:06Z")

</div>

```nohighlight
awk -F,>ouput '/GPRMC/ {
  _ = $(NF-2) s $2 s substr($4, 1, 2) s substr($4, 3) s $6 
}
/SDDBT/ && $2 { print _, $4 }
' s=" " NMEAfile

```

And if SDDBT always follows immediately GPRMC,  
this should be sufficient:

```nohighlight
awk -F,>ouput '/SDDBT/ && $2 { print _, $4 }
{ _ = $(NF-2) s $2 s substr($4, 1, 2) s substr($4, 3) s $6 }
' s=" " NMEAfile 

```

---

<div class="post-metadata">

**Author:** ![rleal](https://community.unix.com/letter_avatar/rleal/32/6_449c533f586939b6e5f246632a46421f.png) [@rleal](https://community.unix.com/u/rleal)\
**Post date:** [July 7, 2008, 8:36am UTC](https://community.unix.com/t/awk-and-nmea-strings/203861/5 "2008-07-07T08:36:50Z")

</div>

Yes, that one did it!! Thanks a lot,

r.-

---

<div class="post-metadata">

**Author:** ![darius2](https://community.unix.com/letter_avatar/darius2/32/6_449c533f586939b6e5f246632a46421f.png) [@darius2](https://community.unix.com/u/darius2)\
**Post date:** [January 1, 2009, 1:03pm UTC](https://community.unix.com/t/awk-and-nmea-strings/203861/6 "2009-01-01T13:03:25Z")

</div>

> [@radoulov](#):
>
> ```plaintext
> awk -F,>ouput '/GPRMC/ {
> _ = $(NF-2) s $2 s substr($4, 1, 2) s substr($4, 3) s $6 
> }
> /SDDBT/ && $2 { print _, $4 }
> ' s=" " NMEAfile
> 
> ```
> 
> And if SDDBT always follows immediately GPRMC,  
> this should be sufficient:
> 
> ```plaintext
> awk -F,>ouput '/SDDBT/ && $2 { print _, $4 }
> { _ = $(NF-2) s $2 s substr($4, 1, 2) s substr($4, 3) s $6 }
> ' s=" " NMEAfile 
> 
> ```

Hi,

I have the same problem with NMEA sentences, streamed on-line to stadout, in terminal session, running gpsd or gpspipe.  
I would like to learn how to pipe NMEA into awk script code  
to get selected gps data like long, lat, speed, time  
from selected NMEA sentences, for use in another pipelined application.

First I need to create NMEA \> AWK \> output pipe

Got a nice example from  
[Info: (gawk.info) Getline/Pipe](http://osr600doc.sco.com/cgi-bin/info2html?(gawk.info)Getline%2FPipe&lang=en)  
awk '{  
if ($1 == "@execute") {  
tmp = substr($0, 10)  
while ((tmp | getline) \> 0)  
print  
close(tmp)  
} else  
print  
}'  
and another awk manual with examples  
[The GAWK Manual - Reading Input Files](http://www.cs.utah.edu/dept/old/texinfo/gawk/gawk_5.html)

Got another example

I am trying to create a pipeline which prints every word on a new line, then [sorts](javascript:void(0)) them and applies uniq -c to it!  
{c=[split](javascript:void(0))($0, s); for(n=1; n\<=c; ++n) print s[n] | "[sort](javascript:void(0))" | "uinq -c"}

answer

fmt -1 filename|sed 's/[,.":;!]//g'|sort -f|uniq -ci

What is a way to create a pipeline for a data stream generated by gpsd, gpspipe ?

gpsd \> filename |sed ..  
gpsd | getline .....

Or just please refer me to a nice place.

Thanks.  
Happy New Year

Darius

---

<div class="post-metadata">

**Author:** ![summer\_cherry](https://community.unix.com/letter_avatar/summer_cherry/32/6_449c533f586939b6e5f246632a46421f.png) [@summer\_cherry](https://community.unix.com/u/summer_cherry)\
**Post date:** [January 2, 2009, 12:01am UTC](https://community.unix.com/t/awk-and-nmea-strings/203861/7 "2009-01-02T00:01:05Z")

</div>

Hi not very sure about your detail logic, but below perl script may help you some

```nohighlight
#! /usr/bin/perl
open FH,"<a.txt";
while(<FH>){
	my @tmp=split(",",$_);
	if($tmp[0] eq "\$GPRMC"){
		$tmp=$tmp[9]."-".$tmp[1];
	}
	if($tmp[0] eq "\$SDDBT"){
		my @arr=split(",",$_);
		print $tmp.":".$arr[3]."\n" if($arr[3] ne "");
	}
}
close FH;

```

---

<div class="post-metadata">

**Author:** ![darius2](https://community.unix.com/letter_avatar/darius2/32/6_449c533f586939b6e5f246632a46421f.png) [@darius2](https://community.unix.com/u/darius2)\
**Post date:** [January 2, 2009, 10:53am UTC](https://community.unix.com/t/awk-and-nmea-strings/203861/8 "2009-01-02T10:53:42Z")

</div>

> [@summer\_cherry](#):
>
> Hi not very sure about your detail logic, but below perl script may help you some
> 
> ```plaintext
> #! /usr/bin/perl
> open FH,"<a.txt";
> while(<FH>){
> my @tmp=split(",",$_);
> if($tmp[0] eq "\$GPRMC"){
> $tmp=$tmp[9]."-".$tmp[1];
> }
> if($tmp[0] eq "\$SDDBT"){
> my @arr=split(",",$_);
> print $tmp.":".$arr[3]."\n" if($arr[3] ne "");
> }
> }
> close FH;
> 
> ```

Thanks .  
I tested basic awk scripts in the night like

awk '$1 ~ /foo/ { print $0 }'

awk '$1 ~ /GPGGA/) { print $0 }'  
awk 'BEGIN { RS = "/" } ; { if ($1 ~ /$GPGGA/) print $1 }'

awk '$1 ~ /$GPGGA/ { print $0 }'  
awk 'BEGIN { RS = "/" } ;{ $1 ~ /GPGGA/ { print $0 }}'

gawk -F "," '$1=="$SDDBT" && $4 !="" { print $4}' NMEAfile \>output

awk '-F,\>ouput '/GPGGA/ {  
\_ = $(NF-2) s $2 s substr($4, 1, 2) s substr($4, 3) s $6  
}  
/SDDBT/ && $2 { print \_, $4 }  
' s=" "'

awk -F,\>ouput '/GPGGA/ {  
\_ = $(NF-2) s $2 s substr($4, 1, 2) s substr($4, 3) s $6  
}  
/GPGGA/ && $2 { print \_, $4 }

The issue is how to store data fields from a number of processed NMEA  
records for postprocessing by another pipelined application.

One solution is getline var to read and process 2 records of  
fields.

Another is getline var \< file  
awk '{  
if (NF == 2 && $1 == "@include") {  
while ((getline line \< $2) \> 0)  
print line  
close($2)  
} else  
print

command | getline

example  
awk '{  
if ($1 == "@execute") {  
tmp = substr($0, 10)  
while ((tmp | getline) \> 0)  
print  
close(tmp)  
} else  
print  
}'

}'

' s=" "  
---------

command | getline var===  
the examples come from excellent awk on-line manual.  
----cs.utah.edu/dept/old/texinfo/gawk/gawk\_22.html------

awk 'BEGIN {  
"date" | getline current\_time  
close("date")  
print "Report printed on " current\_time  
}'  
Working with two dimensional array I can save a series of NMEA records for  
postprocessing.  
The issue is I have to process live data stream on-the-fly.

Is awk fit for a live data stream processing .  
Gps tick is 1 sec.

Darius  
Darius

---

<div class="post-metadata">

**Author:** ![reborg](https://community.unix.com/user_avatar/community.unix.com/reborg/32/167_2.png) [@reborg](https://community.unix.com/u/reborg)\
**Post date:** [January 2, 2009, 12:47pm UTC](https://community.unix.com/t/awk-and-nmea-strings/203861/9 "2009-01-02T12:47:49Z")

</div>

Darius,

Which NMEA keywords are you interested in, what data do you want to extract and what format.

If you give the sentence keywords, what you want the output to look like it would be pretty trivial to get you started along the right path.

---

<div class="post-metadata">

**Author:** ![darius2](https://community.unix.com/letter_avatar/darius2/32/6_449c533f586939b6e5f246632a46421f.png) [@darius2](https://community.unix.com/u/darius2)\
**Post date:** [January 2, 2009, 1:32pm UTC](https://community.unix.com/t/awk-and-nmea-strings/203861/10 "2009-01-02T13:32:01Z")

</div>

> [@reborg](#):
>
> Darius,
> 
> Which NMEA keywords are you interested in, what data do you want to extract and what format.
> 
> If you give the sentence keywords, what you want the output to look like it would be pretty trivial to get you started along the right path.

Thanks.  
I need any data I can build routes, tracks, POI, waypoints, lead and so on.  
So long, lat, time, speed, lead ..  
Moe ideas to come soon.

Just reading NMEA manual.  
Ok.  
Today I can try to read senteces, filter them and filter fields in records.  
To build tracks, routes I can print data to output or send to a file \>

Replacing one data stream by another data stream based on filtering rules.

Frankly speaking I am looking for basic gps application to read such filtered data stream on-the-fly updating (refresh) a map...

First step is to write dialog script as a viewer of gps data.  
Don't know if I can pipe awk | dialog script data refresh

Darius
