Skip to content

Instantly share code, notes, and snippets.

@Micrified
Created April 14, 2023 21:26
Show Gist options
  • Save Micrified/e4905322c17baf7d873eced321d59b99 to your computer and use it in GitHub Desktop.
Save Micrified/e4905322c17baf7d873eced321d59b99 to your computer and use it in GitHub Desktop.
Aligns lines based on input character (for use with vim)
#!/bin/sh
# align: align a series of lines based on a character (default '=')
# Lines with no occurrences are passed through
# Lines with multiple occurrences are aligned at first
# occurrence
CHAR='='
# argument processing
while getopts "c:" arg; do
case $arg in
c) CHAR=`printf '%c' $OPTARG` ;;
*) echo "unrecognized argument: " $arg 1>&2; exit 1 ;;
esac
done
# stdin processing
awk -v sym=$CHAR '
BEGIN { i = 1; k = 0 }
{
split($0,parts,sym)
if (2 <= length(parts)) {
sub(/[ ]*$/, "", parts[1])
sub(/^[ ]*/, "", parts[2])
for (j=3; j <= length(parts); j++) {
parts[2] = parts[2] parts[j]
}
voor[i]=parts[1]
acht[i]=parts[2]
if (length(voor[i]) > k) {
k=length(voor[i])
}
} else {
voor[i]=$0
}
i=i+1
}
END {
for (j=1;j<=i;j++) {
if (j in acht) {
printf "%-" k "s " sym " %s\n", voor[j], acht[j]
} else {
print voor[j]
}
}
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment