mag37 / AWK_tricks.md

Last active 1 week ago

Like 0
AWK_tricks.md Raw

Print Power_On_Hours from smartctl

for d in sd{a..e}; do smartctl -a /dev/$d | \
  awk -v d="$d" '/Power_On/ {print "PowerOnHours "d, int($10), "("int($10 / 24)" days)"}' ; done

Example output:

PowerOnHours sda 23050 (960 days)
PowerOnHours sdb 47183 (1965 days)
PowerOnHours sdc 32260 (1344 days)
PowerOnHours sdd 20713 (863 days)
PowerOnHours sde 46021 (1917 days)

Sum containers total RAM usage

Podman:

podman stats --no-stream --format 'table {{ .Name }} {{ .AVGCPU }} {{ .MemUsage }}' | \
  awk 'NR>1 { gsub("MB",""); sum+=$3;} END{print sum,"MB";}'

Docker:

docker stats --no-stream --format 'table {{ .Name }} {{ .CPUPerc }} {{ .MemUsage }}' | \
  awk 'NR>1 { gsub("MB",""); sum+=$3;} END{print sum,"MB";}'

Sum media durations (eg audiobooks)

find . -iname "*.m4b" -exec sh -c 'mediainfo --Inform="General;%Duration%" "{}"' \; | awk '{totalMS+=$0;} END {Hours=(totalMS/3600000); Minutes=((totalMS%3600000)/6000); Seconds=((totalMS%60000)/1000); Milliseconds=(totalMS % 1000); printf "%02dh %02dm %02ds\n", Hours, Minutes, Seconds}'

#prints:
145h 68m 51s

## Make it a bash function:

function dursum() {
  if [[ ! -n "$1" ]]; then
    echo "no file extension provided"
  else
    find . -iname "*.${1#.}" -exec sh -c 'mediainfo --Inform="General;%Duration%" "{}"' \; \
    | awk '{totalMS+=$0;} END {Hours=(totalMS/3600000); Minutes=((totalMS%3600000)/6000); Seconds=((totalMS%60000)/1000); Milliseconds=(totalMS % 1000); printf "%02dh %02dm %02ds\n", Hours, Minutes, Seconds}'
  fi
}