#! /usr/bin/gawk -f # Last edited on 2025-06-25 23:05:51 by stolfi # Reads from {stdin} table of star properties,. Writes the same to {stdout}, # with the stars renumbered per page # On input, each parag must have the format "{PAGE} | S{SNUM} | ..." where # {PAGE} is a page's f-number (like "f103r" or "f111v") and {SNUM} is a # non-negative star number within the page, from top to bottom, or "??". # # The output will have the same lines, except that the {SNUM} field # will be replaced by new consecutive integers starting with 01 # at each page. # The input file may contain blank lines and '#'-comments. These are all # reproduced in the output file without change. BEGIN { abort = -1; nlins = 0; # Number of data lines read. opage = ""; nsinp = 0; # Number of stars in current page. } (abort >= 0) { exit(abort); } # Remove trailing blanks: // { gsub(/[ \011]+$/, "", $0); } # Pass blank lines: /^$/ { print ""; next; } # Pass comment lines: /^[#]/ { print; next; } # Pass table headers: /[!].*[!]/ { print; next; } # Pass table rules: /[-][+][-]/ { print; next; } /^ *f[0-9]+[rv] *[|] *S([?]+|[0-9]+) *[|]/ { nlins++; if (NF < 5) { prog_error(("unexpected {NF}")); } page = $1; snum_in = $3; rest = $0; gsub(/^[^|]*[|][^|]*[|]/, "", rest); if (! match(page, /^f[1-9][0-9]*[rv][0-9]*$/)) { data_error(("bad page number \"" page "\"")); } if (! match(snum_in, /S^([?]+|[0-9]+)$/)) { data_error(("bad star ID\"" unit "\"")); } snum_in = substr(snum_in, 2); if (page != opage) { new_page(); } nsinp++; snum_ot = nsinp; if ((snum_in != "??") && (snum_in+0 != snum_ot+0)) { printf "page %sstar number changed %02d -> %02d\n", page, snum_in, snum_ot > "/dev/stderr"; } printf " %-6s | %02d |%s\n", page, snum_ot, rest; next; } // { data_error(("bad line format")); } END { if (abort >= 0) { exit(abort); } new_page(); } function new_page() { if (opage != "") { printf "page %s - %d stars\n", opage, nsinp > "/dev/stderr"; } opage = page; nsinp = 0; }