Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2026-04-04 08:08:15

0001 #!/bin/bash
0002 
0003 cat > fill.awk <<'AWK'
0004 # Read extra.dat first: store columns 2 and 3 sequentially
0005 FNR==NR { a[++m] = $2; b[m] = $3; next }
0006 
0007 # Return position of the nth occurrence of character c in string s (1-based), or 0 if not found
0008 function nthpos(s, c, n,   i, p) {
0009   p = 0
0010   for (i = 1; i <= n; i++) {
0011     p = index(substr(s, p + 1), c)
0012     if (p == 0) return 0
0013     p += (length(substr(s, 1, p + (p>0?0:0))) - length(substr(s, 1, p)))  # no-op; kept simple below
0014   }
0015   return 0
0016 }
0017 
0018 # Simpler nthpos implementation (no tricks)
0019 function nthamp(s, n,   i, p, q) {
0020   q = 0
0021   for (i = 1; i <= n; i++) {
0022     p = index(substr(s, q + 1), "&")
0023     if (p == 0) return 0
0024     q += p
0025   }
0026   return q
0027 }
0028 
0029 {
0030   line = $0
0031 
0032   # Only touch lines that look like table rows: contain & and end with \\
0033   if (index(line, "&") && line ~ /\\\\[[:space:]]*$/) {
0034     row++
0035 
0036     if (row <= m) {
0037       # Find the 4th '&' (after col1..col4). This anchors "column 5 starts after here".
0038       p4 = nthamp(line, 4)
0039       if (p4 > 0) {
0040         prefix = substr(line, 1, p4)        # includes the 4th '&'
0041         tail   = substr(line, p4 + 1)       # starts at col5 content
0042 
0043         # Replace the first two empty cells in tail: " <spaces>&<spaces>&"
0044         # (these are the separators after empty col5 and empty col6)
0045         sub(/^[[:space:]]*&[[:space:]]*&/, " " a[row] " & " b[row] " ", tail)
0046 
0047         line = prefix tail
0048       }
0049     }
0050   }
0051 
0052   print line
0053 }
0054 AWK
0055 
0056 awk -f fill.awk extra.dat table.tex > table_filled.tex