#! /usr/bin/gawk -f # Last edited on 2017-05-04 23:44:52 by stolfilocal BEGIN { PROG_NAME = ARGV[0]; gsub(/^.*[\/]/, "", PROG_NAME); PROG_DESC = "normalizes the nanosecond information in date-times of an sdf file"; PROG_HELP = ( \ "${PROG_NAME} < {INFILE}.sdf > {OUTFILE}.sdf" \ ); PROG_INFO = ( \ "\nNAME" \ "\n ${PROG_NAME} - ${PROG_DESC}." \ "\n" \ "\nSYNOPSIS" \ "\n ${PROG_HELP[@]}" \ "\n" \ "\nDESCRIPTION" \ "\n Reads from standard input a raw file in the 'SDF'" \ "\nformat, as produced by find-all-files-size-date(1)." \ "\nAdds, pads, or truncates the fraction-of-second part" \ "\nof the date-timefield, leaving exactly 9 decimal" \ "\ndigits after the period. Does not do any rounding." \ "\n" \ "\nOPTIONS" \ "\n None yet." \ "\n" \ "\nSEE ALSO" \ "\n find-all-files-size-date(1), sdf-file-remove-musec" \ "\nAUTHOR" \ "\n Created 2009-04-07 by Jorge Stolfi, Unicamp" \ ) abort = -1; } (abort >= 0) { exit abort; } /^[ ]*[0-9]+[ ]+(19|20)[0-9][0-9]-[01][0-9]-[0-3][0-9]-[0-2][0-9][0-6][0-9][0-6][0-9]([.][0-9]+|)[ ]/ { # Get hold of the three fields of the SDF file. # Beware that the file name may contain embedded blanks. sz = $1 # Size in bytes. dt = $2 # Date and time. fn = $0; gsub(/^[ ]*[0-9]+[ ]+[-.0-9]+[ ]/, "", fn); if (match(dt, /[.]/)) { # Get the fraction of second in the time field: ns = dt; gsub(/^[-0-9]*[.]/, ".", ns); # Pad or truncate to '.' and 9 fraction digits: ns = substr(( ns "000000000"),1,10) # Strip the fraction of second from the time field: gsub(/[.][0-9]*/, "", dt); } else { # Seconds were whole, provide a null fraction: ns = ".000000000"; } # Paranoia: if (dt !~ /^[12][09][0-9][0-9]-([0][1-9]|[1][0-2])-[0-3][0-9]-[0-2][0-9][0-6][0-9][0-6][0-9]$/) { data_error(("bad date-time format «" dt "»")); } if (ns !~ /^[.][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/) { data_error(("bad fraction-of-second format «" ns "»")); } # Print it: printf "%14s %s%s %s\n", sz, dt, ns, fn; next; } //{ data_error("bad format"); } function data_error(msg) { printf "%s:%s: ** %s\n", FILENAME, FNR, msg > "/dev/stderr" abort = 1 exit abort }