awk and NMEA strings

Hi all:

I have a file with NMEA sentences of the type:

$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:

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:

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

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

... && !/no depth info/

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

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

 $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

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

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:

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

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

r.-

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
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

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

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

#! /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

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