Skip to content

Instantly share code, notes, and snippets.

@DavidPesticcio
Created June 29, 2020 17:37
Show Gist options
  • Save DavidPesticcio/ebf511d3d52eedd1fdea01aad205c065 to your computer and use it in GitHub Desktop.
Save DavidPesticcio/ebf511d3d52eedd1fdea01aad205c065 to your computer and use it in GitHub Desktop.
AWK: Splitting a string into sub-strings of length X
Using AWK to split a line of text into sub-strings:
To split a line of text at column X, followed by a new line (or whatever) - even if the input it less than a multiple of X.
If something other than a NEWLINE is desired substitute it, use "& " or "& value=" etc...
You may want to play around with BEGIN{} & END{} blocks if you plan on getting fancy.
Handy for reformatting a mangled SSH or GPG key!
DATA="$(dd if=/dev/urandom bs=512 count=1 | base64 -w0)"
by pipe
echo $DATA | awk '{gsub(/.{64}/,"&\n")}1'
by file
awk '{gsub(/.{2048}/,"&\n")}1' /path/to/file
by process substitution
awk '{gsub(/.{2048}/,"&\n")}1' <(echo $DATA )
use tr to remove the input TAB or NEWLINE, or whatever else
tr '\t|\n' ' '
eg. Use V for VISUAL LINE highlight of lines to see...
vi <(man bash | tr '\t|\n' ' ' | awk '{gsub(/.{64}/,"&\n")}1')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment