3

I am currently using a G-STAR IV globalsat GPS which gives me the following output (which is coming from the GPS every 1 second) and I save this output by redirecting to a file name "gpsoutput.tr". This file gets constantly updated each time it receives a GPS signal. The content of this file is as follows:

$GPGSV,3,2,10,14,43,184,,27,33,314,,29,21,152,,16,17,263,*7D
$GPGSV,3,3,10,24,10,092,,19,06,322,*79
$GPRMC,095827.000,A,2335.2440,N,05809.8432,E,0.55,103.38,091114,,,A*66
$GPGGA,095828.000,2335.2443,N,05809.8433,E,1,04,5.2,89.5,M,-34.7,M,,0000*46
$GPGSA,A,3,21,22,18,15,,,,,,,,,6.4,5.2,3.6*3C
$GPRMC,095828.000,A,2335.2443,N,05809.8433,E,1.21,102.44,091114,,,A*63
$GPGGA,095829.000,2335.2446,N,05809.8434,E,1,04,5.2,89.5,M,-34.7,M,,0000*45
$GPGSA,A,3,21,22,18,15,,,,,,,,,6.4,5.2,3.6*3C
$GPRMC,095829.000,A,2335.2446,N,05809.8434,E,0.92,102.44,091114,,,A*69
$GPGGA,095830.000,2335.2448,N,05809.8437,E,1,04,5.2,89.5,M,-34.7,M,,0000*40
$GPGSA,A,3,21,22,18,15,,,,,,,,,6.4,5.2,3.6*3C
$GPRMC,095830.000,A,2335.2448,N,05809.8437,E,1.25,102.12,091114,,,A*62
$GPGGA,095831.000,2335.2450,N,05809.8441,E,1,04,5.2,89.5,M,-34.7,M,,0000*49
$GPGSA,A,3,21,22,18,15,,,,,,,,,6.4,5.2,3.6*3C
$GPRMC,095831.000,A,2335.2450,N,05809.8441,E,1.47,101.80,091114,,,A*67
$GPGGA,095832.000,2335.2455,N,05809.8446,E,1,04,5.2,89.5,M,-34.7,M,,0000*48
$GPGSA,A,3,21,22,18,15,,,,,,,,,6.4,5.2,3.6*3C
$GPGSV,3,1,10,21,55,052,18,22,54,297,14,18,53,011,22,15,18,042,18*72
$GPGSV,3,2,10,14,43,184,,27,33,314,,29,21,152,,16,17,263,*7D
$GPGSV,3,3,10,24,10,092,,19,06,322,*79
$GPRMC,095832.000,A,2335.2455,N,05809.8446,E,2.36,100.07,091114,,,A*6D

this file ("gpsoutput.tr") keeps getting added with new lines each time it receives a GPS signal.

Now, I have a bash file which runs an awk program continuously to extracts the (speed), the (latitude, longitude) and the (UTC timestamp) in three different files. This is the bash script:

#!/bin/sh
echo "Starting GPS info gathering"
while true; do
    awk -F, -f gpsgetinfo.awk gpsoutput.tr

 done

Finally, my awk program is very simple which I want it to constantly read the latest record from the gpsoutput.tr file and extract the above mentioned infos, in the three separate files. the awk program (gpsgetinfo.awk) is as follows:

BEGIN {
    FS = ","; 
    OFS = " ";

    while ((getline < "gpsoutput.tr") > 0) {


        if($1=="$GPRMC") {
                converted = $8*1.852; 
                printf $4 " " $6 "\n" >> "data1.txt";
                printf "%.3f \n", converted >> "speed.txt";
                printf $2 "\n" >> "gpstime.txt";
        }


    }
}


END {
    fflush();
}

The problem is, that in my output files (data1.txt, speed.txt and gpstime.txt) from the awk program, the same output is repeating several times and within a few seconds, the file size is reaching to the size of Mega bytes. To make it clear. This is how my utc time output file looks:

095546.000
095547.000
095546.000
095547.000
095546.000
095547.000
095546.000
095547.000
095546.000
095547.000
095546.000
095547.000
095546.000

and it repeats for several 100 times in the file.

While the expected output in gpstime.tr should just be :

095546.000
095547.000
095548.000
095549.000
095550.000
095551.000

and so on.. since the gps signal is received every 1 second.

The gpsoutput.tr file where the nmea data is being saved, is just a few kbs, and without any recurrences in data.

Can anyone tell me . why the output files are having so many repeated values? I have also tried using printf $4 " " $6 "\n" > "data1.txt"; (single > instead of >>) but didn't work for me.

  • How did you get the original, raw GPS data, output to your terminal (or gpsoutput.tr), in the first place? Where does it come from? Some additional utility/software? – voices Mar 14 '17 at 10:21
  • Or did you just cat /dev/input/gps1 > gpsoutput.tr, or something like that? If not, maybe that information would help lead you to solve your problem somehow. – voices Mar 18 '17 at 13:51
2

The problem is indeed what @Lekensteyn explained. Since your file is constantly growing, your awk will always repeat the lines it has already processed. Unfortunately, using tail -f is not a solution since that will go on for ever and awk will not print anything.

Instead, use the same script as suggested by @Lekensteyn but with > instead of >>:

#!/usr/bin/awk -f
BEGIN {
    FS = ",";
}
$1 == "$GPRMC" {
    converted = $8*1.852; 
    print $4, $6 > "data1.txt";
    printf "%.3f \n", converted > "speed.txt";
    print $2 > "gpstime.txt";
}

You then run that script every second to re-create your output files:

#!/bin/sh
echo "Starting GPS info gathering"
while true; do
    ./gpsgetinfo.awk gpsoutput.tr    
 done
  • thanks,, this works good..But , the file gets filled up and suddenly all the data gets blank for a couple of seconds, and again the file gets data into it. Is there a way to avoid the file from getting blank during running the shell script? Since I am using another C program which is constantly taking data as input from these output files generated through the awk script..And if the file is empty,, my c program would return an error and terminate.. what can be done? – Shezwan91 Nov 10 '14 at 5:31
  • @Shezwan91 OK, what we have here is an XY problem. Yes, this approach will recreate the file every time the script is run and that can break downstream functions. This is why you need to explain what your final aim is when asking instead of giving us only the intermediate steps. Anyway, I suggest you post a new question explaining everything you need to happen. For example, you might be able to run your C program only when the file changes by using inotify or bash_script.sh && c_program. – terdon Nov 10 '14 at 14:22
0

If the GPS data file only grows instead of overwritten, then you will write the same data over and over again, that is why you get duplicates.

You must somehow keep feeding new lines to awk. This can be done using tail --follow:

tail -f gpsoutput.tr | ./gpsoutput.awk

Your gpsoutput.awk should then be this executable script:

#!/usr/bin/awk -f
BEGIN {
    FS = ",";
}
$1 == "$GPRMC" {
    converted = $8*1.852; 
    print $4, $6 >> "data1.txt";
    printf "%.3f \n", converted >> "speed.txt";
    print $2 >> "gpstime.txt";
    # Optional: add fflush() here.
}
  • I have tried running the above.. But the output files(gpstime.txt, data1.txt etc) are empty. no data is getting saved in them at all. – Shezwan91 Nov 9 '14 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.