e42.uk Circle Device

 

Quick Reference

awk

AWK to Extract Section of a File

Extract a section of a file based on a start and end pattern, and do a little arithmetic:

BEGIN{FS=",";OFS=",";found=0;reading_no=0}
/2019-01-11 10:02:2/{found=1;depth=50;wt=5}
/2019-01-11 10:04:0/{found=0}
/2019-01-11 10:04:1/{found=1;depth=100}
/2019-01-11 10:06:0/{found=0}
{if(found){d_diff_a=depth-$9;d_diff_b=depth-(($10+400)/4);print $0,depth,d_diff_a,d_diff_b,reading_no,wt;reading_no++}}

FS is input field separator OFS is output field separator, by default these fields are both the space character (0x20). The output field separator is used when the fields are output using print:

echo 'ben;green;eu' | awk -e 'BEGIN{FS=";";OFS=":"}{print $1,$2,$3}'
ben:green:eu

$10 refers to the 10th field in this file (in this example like the 10th column in a CSV).

Using AWK to Update DNS Records

Automating the creation of SSL certificates with Let's Encrypt for wildcard domain names. Using nsupdate would be nice but it would seem NSD does not support that and so I have a script that uses AWK to do some stuff...

Cleanup of a TSV File

Input file should be in UTF-8 format... probably without a BOM.

awk 'BEGIN {OFS="\t"; FS="\t"}; {match($2, / (.*)/, RESA); match($9, / (.*)/, RESB); print RESA[1]","RESB[1];}' atmospheric.csv > atmospheric_pressure.csv

Quick Links: Techie Stuff | General | Personal | Quick Reference