Skip to content

Instantly share code, notes, and snippets.

@b-layer
Created March 19, 2021 10:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b-layer/7490fac70dce19355d80056922945d3d to your computer and use it in GitHub Desktop.
Save b-layer/7490fac70dce19355d80056922945d3d to your computer and use it in GitHub Desktop.
An awk script I threw together to break down strings for explanation in Stack Exchange answers.
# 1. Take any string such as this regex \v<[1-9]\d{,2}\ze(\d{3})*>
# 2. Indicate separations with a delimiter char like underscore
# 3. Run awk like so:
#
# awk -v RS='_' -f {thisfile} <<< '\v_<_[1-9]_\d_{,2}_\ze_(_\d_{3}_)_*_>'
# Result:
# \v<[1-9]\d{,2}\ze(\d{3})*>
# \v #
# < #
# [1-9] #
# \d #
# {,2} #
# \ze #
# ( #
# \d #
# {3} #
# ) #
# * #
# > #
# Change the 'RS' value to whatever delimiter char you prefer.
{
recs[NR] = $1
full = full $1
}
END {
printf " %s\n", full
len = length(full); pad = 1
for (rec in recs) {
printf "%*s%-*s# \n", pad, " ", len-pad+2, recs[rec]
pad += length(recs[rec])
}
}
# You want it all crushed on a single command-line?
#
# awk -v RS='_' '{ recs[NR] = $1; full = full $1; }; END { printf " %s\n", full; len = length(full); pad = 1; for (rec in recs) { printf "%*s%-*s# \n", pad, " ", len-pad+2, recs[rec]; pad += length(recs[rec]); } }' <<< '\v_<_[1-9]_\d_{,2}_\ze_(_\d_{3}_)_*_>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment