AWK Stuff

Just some random experimental AWK stuff.

Some random AWK commands

Print the reverse the order of certificates in a bundled x509 file

cat $PEM_FILE | awk 'BEGIN {n=0} split_after == 1 {n++;current_line=0;split_after=0} /-----END CERTIFICATE-----/ {split_after=1} NF {a[n][current_line++]=$0} END {for(m=n;m>=0;m--) for(line in a[m]) print a[m][line];}'

Print out the CN (common name) of Nth certificate from x509 file

# n==0 is the first one
cat $PEM_FILE | awk 'split_after == 1 {n++;split_after=0} /-----END CERTIFICATE-----/ {split_after=1} n==0 && NF {print}' | openssl x509 -noout -subject | awk -F/CN= '{print $NF}'