Skip to content

Instantly share code, notes, and snippets.

@gullinbursti
Last active April 10, 2022 01:41
Show Gist options
  • Save gullinbursti/40d4d6ce9574f05c785f1a2aacafb682 to your computer and use it in GitHub Desktop.
Save gullinbursti/40d4d6ce9574f05c785f1a2aacafb682 to your computer and use it in GitHub Desktop.
gawk piper to (r-)pad a list of strs
#!/bin/bash
#-- space sep list
pkgs="bc exfat-utils gawk git hfsutils jq pv source-highlight sysbench tmux wget"
#-- --=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=-- --#
# \\ [ bash 1-liner ]\_
# \\~~*~~*~~*~~*~~*~~*~~*~~*~~*~~*~~*~~*~~*~~*~~\_
#-- start by displaying list
#-- have awk use ' ' as the row sep to print strlen & str
#-- pipe new list to gawk, set max len & tot counter to 0
#-- BEGIN : iterate thru lines, keeping track of max str len (also making tot++)
#-- END : loop thru 'em again, this time w/ max val amount of '.' rt padded
#-- piped to replace the '.' w/ ' ' via tr
#-- piped to sed, replace line prefixed & suffixed w/ a ' ' like " $l "
#-- finally, echo nothing for a new line (prolly coulda included w/ the sed)
echo $pkgs | awk 'BEGIN {RS=" "} ; {print length($0),$0}' | gawk 'BEGIN {max=0 ; tot=0} ; {if($1 > max) max=$1 ; arr[tot]=$2 ; tot=tot+1} ; END {for(i=0; i<tot-1; i++) printf "[%*s]\n",(max),arr[i]}' | tr ' ' '.' | sed -E 's/([a-z\-]+)/ \1 /g' ; echo
# //~~*~~*~~*~~*~~*~~*~~*~~*~~*~~*~~*~~*~~*~~*~~*~~/'
#-- terminate w/o error
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment