User:Gah4/awk

Awk programs for table formatting IAEA table B1 # read a file with table columns separated by TABLE, and table rows by double TABLE. # write a wikitable # refor

Awk programs for table formatting

IAEA table B1

# read a file with table columns separated by TABLE, and table rows by double TABLE.
# write a wikitable
# reformat the first column from the atno-symbol-atwt form to <sup> ... form.
# count <table> and </table> tags to find the appropriate part of table

BEGIN {
   table=1;
   OFS1="|-\n"
   OFS2="";
   OFS3="|";
   OFS3h="!";
   OFS4="";
   OFS5="||"
   OFS5h="!!"
   OFS6=""
   OFS7="|"
   COLS=6;
   nextrow=1;
   skip=1;
# which columns are allowed to rowspan:
# 0 means don't span, non-zero span like that column
   rowspan[1]=rowspan[2]=rowspan[5]=rowspan[6]=1;
# print wikitable head
   print "{| class=\"wikitable\"";   # }
   }

/^#/ { next }

/^<table/ {
   table--;
   if(table==0) skip=0;
   }

(skip) { next; }


/^<\/table>/  {
   skip=1;
   }

/^ *<tr> *$/ {
   printf "$ %d \n", NR > "/dev/stderr"
   row=nextrow;
   nextrow++; 
   col=0;
   next;
   }

/^ *<th / {
   col++;
   head[row]=1
   }

/^ *<td / {
   col++;
   }

{  
# leave <sup> and </sup> but remove all other tags.
   gsub("<sup>","!sup!");
   gsub("</sup>","!/sup!");
   gsub("<[^>]*>","");
   gsub(" ","");
   gsub("&nbsp","");
   gsub("!sup!","<sup>");
   gsub("!/sup!","</sup>");
   if($0 ~ "^[ \t]*$") next;
#   print > "/dev/stderr"
   sub("^[ \t]*","");
   sub("[ \t]*$","");
#   gsub(" x 10","e");
   for(k=row; (k,col) in tab; ) k++
   printf "! %d (%d,%d) %s\n" ,NR, k, col, $0 > "/dev/stderr"
   tab[k,col]=$0
   if(k>=nextrow) nextrow=k+1;
   }

END {
   for(i=1;i<=row;i++) {
      printf "%s", OFS1;
      j=1;
      if((i,j) in tab) {
#         printf "! (%d,%d)\n", i, j;
         k=i+1;
         if(rowspan[j]) for(k=i+1;k<=row && !((k,rowspan[j]) in tab); ) k++;
         split(tab[i,1],x,"-")
         if(head[i]) printf "%s", OFS3h
         else printf "%s", OFS3
         if(head[i]) printf "%s%s", tab[i,j], OFS4;
         else if(k==i+1) printf "{{nuclide2|%s|%s}}%s", x[2], x[3], OFS4;
         else printf "rowspan=\"%d\"%s{{nuclide2|%s|%s}}%s", k-i, OFS7, x[2], x[3], OFS4;
         }
      for(j=2; j<=COLS; j++) {
         if((i,j) in tab) {
            k=i+1;
            if(rowspan[j]) for(;k<=row && !((k,rowspan[j]) in tab); ) k++;
         if(head[i]) printf "%s", OFS5h
         else printf "%s", OFS5
            if(k==i+1) printf "%s%s", tab[i,j], OFS6;
            else printf "rowspan=\"%d\"%s%s", k-i, OFS7, tab[i,j], OFS6;
            }
         }
      for(j=2;(i,j) in tab; j++) sum[j] += tab[i,j]
      print OFS2;
      }
# print wikitable tail
   print "|}";
#   for(i=2; i in sum; i++) printf "Sum[%d]=%f\n", i, sum[i] >> "/dev/stderr"
   }
 

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.