Created
March 19, 2021 10:31
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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