#! /usr/bin/gawk -f # Last edited on 2011-06-01 17:12:44 by stolfilocal BEGIN{ USAGE = ( "append-new-data.gawk [-v mock=1] SOURCE.txt .." ); abort = -1; FS = "|"; OFS = "|"; source = "????"; if (mock == "") { mock = 0; } } (abort >= 0) { exit abort; } /^[ ]*([\#]|$)/ { next; } /^[ ]*source[ ]*[=]/ { source = $0; gsub(/^[ ]*source[ ]*[=][ ]*/, "", source); gsub(/[ ]/, "", source); next; } /2011-[0-9][0-9]-99/ { next; } /^(cams|pres|heat|flow|wlev)-un[123][.]txt[ ]+[|]/ { # Get fields: if (NF < 3) { data_error("too few fields"); } gsub(/[ ]/, "", $0); fn = $1; dt = $2; hr = $3; # Add source suffix to file name: gsub(/[.]txt$/,"-t.txt",fn); # Where shall we write it? ofile = (mock ? "/dev/stderr" : fn); # Get and fix date, time; compute elapsed hours. if (hr ~ /^[0-9]+[:][0-9]+$/) { hr = (hr ":00"); } ht = ehours(dt, hr); # Write line to proper file: if (mock) { printf "%s <-- ", fn >> ofile; } printf "%s | %s | %8.3f |", dt, hr, ht >> ofile; for (i=4; i < NF; i++) { printf " %5d |", $(i) >> ofile; } printf " %s |\n", source >> ofile; if (! mock) { close(ofile); } next; } // { data_error("bad format"); } function ehours(dt,hr, dtx,yr,dy,n,hf,ht) { dtx=(dt " 00 00 00"); gsub(/[\/-]/, " ", dtx); yr=substr(dt,1,4); dy=(yr-2011)*365 + strftime("%j", mktime(dtx)); n=split(hr,hf,":"); if (hf[1] == "99") { ht = 9999.999; } else { ht = 24*dy - 1680 + hf[1]+(1.0/60.0)*hf[2]; if (n == 3) { ht = ht + (1.0/3600.0)*hf[3]; } } return ht; } function data_error(msg) { printf "%d: **%s\n", FNR, msg > "/dev/stderr"; abort = 1; exit abort; } function arg_error(msg) { printf "**%s\n", msg > "/dev/stderr"; abort = 1; exit abort; }