AWK

Show ASCII table

#! /usr/bin/awk -f 
BEGIN { 
    for (i=0; i<16; i++) { 
        for (j=32+i; j<128; j+=16) { 
            if (j == 32) { x = "SPC" } 
            else if (j == 127) { x = "DEL" } 
            else { x = sprintf("%c", j) } 
                    printf("%3d/0x%02X: %-5s", j, j, x) 
        } 
print "" 
     } 
}

Stars and Cartesian coordinates printed in nice columns

awk -F, '{printf "%-25s %8.3f %8.3f %8.3f\n", $1, $2, $3, $4}' hipparcos.csv



Combinations of r elements chosen from a pool of n elements
BEGIN {
for (i=1; i <= r; i++) {
A[i] = i
if (i < r ) printf i OFS
else print i}
while (A[1] = 1; i–) {
if (A[i] < n – r + i) {
A[i]++
p = i
break}}
for (i = p + 1; i <= r; i++) A[i] = A[i – 1] + 1
for (i=1; i <= r; i++) {
if (i < r) printf A[i] OFS
else print A[i]}}
exit}

 



Take the mean of two values in scientific notation

#!/bin/sh
awk -v a="$1" -v b="$2" 'BEGIN {
    printf "%.3E\n", (a + b) / 2
}' | sed -E 's/-0?/-/g'

 


Use awk to take a list of data with repetitions and build a new list of unique entries, with a second column displaying the number of instances of the original item in that first list

awk '{count[$1]++} END {for (x in count) print x "," count[x]}' list