[GRASS-SVN] r45886 - in grass/branches/develbranch_6/scripts: v.in.garmin v.in.mapgen

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Apr 9 21:35:57 EDT 2011


Author: hamish
Date: 2011-04-09 18:35:56 -0700 (Sat, 09 Apr 2011)
New Revision: 45886

Modified:
   grass/branches/develbranch_6/scripts/v.in.garmin/v.in.garmin
   grass/branches/develbranch_6/scripts/v.in.mapgen/v.in.mapgen
Log:
apply awk work-around for lack-of-tac on Mac OSX (#181)

Modified: grass/branches/develbranch_6/scripts/v.in.garmin/v.in.garmin
===================================================================
--- grass/branches/develbranch_6/scripts/v.in.garmin/v.in.garmin	2011-04-09 19:41:51 UTC (rev 45885)
+++ grass/branches/develbranch_6/scripts/v.in.garmin/v.in.garmin	2011-04-10 01:35:56 UTC (rev 45886)
@@ -140,7 +140,20 @@
     exit 1
 fi
 
+#### check if we have tac
+# perhaps try `type -t` instead of `which`, as it is more POSIXy
+if [ -x "`which tac`" ] ; then
+   TAC=tac
+else
+   TAC=awk_tac
+fi
 
+awk_tac()
+{
+   awk '1 { last = NR; line[last] = $0; }
+          END { for (i = last; i > 0; i--) { print line[i]; } }'
+}
+
 #### setup temporary file
 TEMPFILE="`g.tempfile pid=$$`"
 if [ $? -ne 0 ] || [ -z "$TEMPFILE" ] ; then
@@ -524,9 +537,9 @@
 	#### prepare line components
 	if [ $RTE -eq 1 ] ; then
 	    # add vertex counts
-	    cat "${TEMPFILE}.gpst" | sed -e '1d' | tac | awk 'BEGIN { FS="\t" ; R=0 } \
+	    cat "${TEMPFILE}.gpst" | sed -e '1d' | "$TAC" | awk 'BEGIN { FS="\t" ; R=0 } \
 		$1=="W" { printf(" %.7f %.7f\n", $6, $5) ; ++R } ; \
-		$1=="R" { printf("L %d 1\n", R) ; R=0 } END {;}' | tac > "${TEMPFILE}.base"
+		$1=="R" { printf("L %d 1\n", R) ; R=0 } END {;}' | "$TAC" > "${TEMPFILE}.base"
 
 	    # create attr table: cat(int), id number(int 0-19), name varchar(16+), starting_wpt(varchar 10)
 	    cat "${TEMPFILE}.gpst" | grep '^R' | cut -f2,3 | grep -n '^' | \
@@ -538,9 +551,9 @@
 
 	elif [ $TRK -eq 1 ] ; then
 	    # add vertex counts
-	    cat "${TEMPFILE}.gpst" | sed -e '1d' | tac | awk 'BEGIN { FS="\t" ; R=0 } \
+	    cat "${TEMPFILE}.gpst" | sed -e '1d' | "$TAC" | awk 'BEGIN { FS="\t" ; R=0 } \
 		$1=="T" { printf(" %.7f %.7f\n", $4, $3) ; ++R } ; \
-		$1=="" { printf("L %d 1\n", R) ; R=0 } END {;}' | tac > "${TEMPFILE}.base"
+		$1=="" { printf("L %d 1\n", R) ; R=0 } END {;}' | "$TAC" > "${TEMPFILE}.base"
 
 	    # create attr table: cat(int), start_time varchar(40), start_lat(double precision), start_lon(double precision), \
 	    #			    end_time varchar(40), end_lat(double precision), end_lon(double precision)
@@ -557,9 +570,9 @@
 	# gardump
 	if [ $RTE -eq 1 ] ; then
 	    # add vertex counts
-	    grep -v '^\[\|^#' "${TEMPFILE}.gard" | tac | awk 'BEGIN { R=0 } \
+	    grep -v '^\[\|^#' "${TEMPFILE}.gard" | "$TAC" | awk 'BEGIN { R=0 } \
 		/^[-0-9]/ { printf(" %.7f %.7f\n", $2, $1) ; ++R } ; \
-		/^\*\*/ { printf("L %d 1\n", R) ; R=0 } END {;}' | tac > "${TEMPFILE}.base"
+		/^\*\*/ { printf("L %d 1\n", R) ; R=0 } END {;}' | "$TAC" > "${TEMPFILE}.base"
 
 	    # create attr table: cat(int), id number(int 0-19), name varchar(40), starting_wpt(varchar 15)
 	    # grep -n is to insert cat number, starting from 1. (route numbers start from 0)
@@ -577,7 +590,7 @@
 	elif [ $TRK -eq 1 ] ; then
 	    # add vertex counts
 	    # some tracks don't have time/date, so we have to decide on the fly
-	    grep -v '^\[\|^#\|^Track:' "${TEMPFILE}.gard" | tac | awk 'BEGIN { R=0 } \
+	    grep -v '^\[\|^#\|^Track:' "${TEMPFILE}.gard" | "$TAC" | awk 'BEGIN { R=0 } \
 	    ! /start/ {
 	      if($0 ~ /..:..:../ ) { printf(" %.7f %.7f\n", $4, $3) }
 	      else { printf(" %.7f %.7f\n", $2, $1) }
@@ -589,7 +602,7 @@
 	      else { printf(" %.7f %.7f\nL %d 1\n", $2, $1, R+1) }
 	      R=0
 	    }
-	    END {;}' | tac > "${TEMPFILE}.base"
+	    END {;}' | "$TAC" > "${TEMPFILE}.base"
 
 	    # create attr table: cat(int), \
 	    #  start_time varchar(40), start_lat(double precision), start_lon(double precision), \
@@ -709,13 +722,13 @@
 
     if [ "$HAVE_ALT" = "TRUE" ] ; then
 	# cut out altitude data and add blank lines between track lines
-	grep -v '^\[\|^#\|^Track:' "${TEMPFILE}.gard" | tac | \
+	grep -v '^\[\|^#\|^Track:' "${TEMPFILE}.gard" | "$TAC" | \
 	  awk '{ if($0 ~ /..:..:../ ) {
 		   print $5 " " $6
 		 } else {
 		   print $3 " " $4
 		 }
-	       }' | sed -e 's/ start/\n/' -e 's/ $//' | tac > "${TEMPFILE}.alt"
+	       }' | sed -e 's/ start/\n/' -e 's/ $//' | "$TAC" > "${TEMPFILE}.alt"
 
 	paste -d' ' "${TEMPFILE}.P_base" "${TEMPFILE}.alt" > "${TEMPFILE}.P_baseZ"
     else

Modified: grass/branches/develbranch_6/scripts/v.in.mapgen/v.in.mapgen
===================================================================
--- grass/branches/develbranch_6/scripts/v.in.mapgen/v.in.mapgen	2011-04-09 19:41:51 UTC (rev 45885)
+++ grass/branches/develbranch_6/scripts/v.in.mapgen/v.in.mapgen	2011-04-10 01:35:56 UTC (rev 45886)
@@ -72,6 +72,20 @@
 LC_NUMERIC=C
 export LC_NUMERIC
 
+#### check if we have tac
+# perhaps try `type -t` instead of `which`, as it is more POSIXy
+if [ -x "`which tac`" ] ; then
+   TAC=tac
+else
+   TAC=awk_tac
+fi
+
+awk_tac()
+{
+   awk '1 { last = NR; line[last] = $0; }
+          END { for (i = last; i > 0; i--) { print line[i]; } }'
+}
+
 eval `g.gisenv`
 : ${GISBASE?} ${GISDBASE?} ${LOCATION_NAME?} ${MAPSET?}
 LOCATION="$GISDBASE/$LOCATION_NAME/$MAPSET"
@@ -126,7 +140,7 @@
 
   ## matlab format. Cleanse with NaN->nan and spaces to tabs,
   ##   and skip blank and comment lines
-  grep -v '^#\|^$' "$FILE" | tac | awk '
+  grep -v '^#\|^$' "$FILE" | "$TAC" | awk '
     {print $1 "\t" $2 "\t" $3}
     END {if ($3~/[0-9Nna]/) print "NaN\tNaN\tNaN"
          else print "NaN\tNaN"}' | \
@@ -135,15 +149,15 @@
     $1~/nan.*/ { printf("L %d\n", R) ; R=0 ; next }
     $1~/\d*\d*/ { printf(" %.15g %.15g %.15g\n", $1, $2, $3) ; ++R }
     END {;}' | \
-   grep -v "^L 0$" | tac > "$TMPFILE"
+   grep -v "^L 0$" | "$TAC" > "$TMPFILE"
 
 else
   ## mapgen format.
-  tac "$FILE" | awk '
+  "$TAC" "$FILE" | awk '
     BEGIN { FS="\t" ; R=0 }
     $1~/#.*/ { printf("L %d\n", R) ; R=0 }
     $1~/\d*\.\d*/ { printf(" %.8f %.8f\n", $1, $2) ; ++R }
-    END {;}' | tac > "$TMPFILE"
+    END {;}' | "$TAC" > "$TMPFILE"
 fi
 
 #### create digit header



More information about the grass-commit mailing list