awk ward gps monitoring

55 views Asked by At

I have a little Raspberry Pi monitoring things like temperature, wind and pressure. Now our dear russians have started to tamper with GPS in my neighbourhood and I saw that our authorities where rather slow to react. I have an old GPS pl2303 GPS dongle which I think could be good to keep a timely check on local GPS behaviour. My problem is the following:

Using the NMEA-stream from gpsd and gpspipe I try to extract the coordinates with awk:

gpspipe -r |grep GPGGA

gives me

...... $GPGGA,225842.00,5919.2601,N,01758.1669,E,1,06,2.00,19.55,M,24.163,M,,*5 .....

but

gpspipe -r |grep GPGGA|awk -F, '{ print $3 } { print $5 }'

gives me nothing. If I, however, save the output from gpspipe -r to a file g

cat g|grep GPGGA|awk -F, '{ print $3 } { print $5 }'

gives me ... 5919.2689 01758.1838 ... which is what I want. Why doesn't awk produce the desired output when directly fed with the NMEA stream? What have I missed? H

2

There are 2 answers

2
jhnc On

Probably you are not waiting long enough.

If your grep has a --line-buffered option that should help:

gpspipe -r |
grep --line-buffered GPGGA |
awk '{ print $3; print $5 }'

Or if your system has stdbuf you can try:

gpspipe -r |
stdbuf -oL -- grep GPGGA |
awk '{ print $3; print $5 }'

(or ... stdbuf -o0 ...)

When using awk, grep is not required:

gpspipe -r |
awk '/GPGGA/ { print $3; print $5; fflush() }'

explanation

I am guessing that gpspipe -r produces output continuously until stopped. If that is the case, your problem is probably that when grep output is fed into a pipe, it is buffered. Until the buffer is full, awk does not receive any input.

You can get an idea with:

# prints immediately
seq 100000 |
grep . |
awk '(NR%10000)==0{printf"."}END{print""}'
# prints after a delay
(
    # not enough to fill buffer
    seq 10
    # wait
    sleep 5
    # enough to fill buffer
    seq 10000
) |
grep . |
awk '(NR%10000)==0{printf"."}END{print""}'
0
user37342 On

In our bash world streams without newlines aren't uncommon. Examples are ls and cat that may send you long lineless streams. Sometimes the use of named pipes is a remedy. This particular problem can be handled with:

~$ mkfifo gpsfifo; gpspipe -r -o gpsfifo & cat gpsfifo|awk -F, '/GPGGA/ { print $3 " " $5; }'

[1] 57231 5919.2625 01758.1808 5919.2625 01758.1808 5919.2625 01758.1808 5919.2625 01758.1808 5919.2625 01758.1808 5919.2621 01758.1765 .... ....