Wrap and indent text using coreutils
With gnu awk you can do something simple like this:
awk -F '---' '
{ gsub(/.{50,60} /,"&\n ",$2)
printf "%-10s %s\n", $1, $2 }'
For a more accurate long-winded version handling long words:
awk -F '---' '
{ printf "%-10s ", $1
n = split($2,x," ")
len = 11
for(i=1;i<=n;i++){
if(len+length(x[i])>=80){printf "\n "; len = 11}
printf "%s ",x[i]
len += 1+length(x[i])
}
printf "\n"
}'
After the fold command pipe the output to sed and replace the start of line with a tab. And you can control the indent with with the 'tabs ' command prior:
tabs 5
echo "A very long line that I want to fold on the word boundary and indent as well" | fold -s -w 20 | sed -e "s|^|\t|g"
A very long line that I want to fold on the word boundary and indent as well
Here's a shorter answer that uses fold then shifts its output by 11 spaces.
To see what it is doing add a -v
or -x
to the final bash.
| sed 's:\(.*\)---\(.*\):printf "%-10s " "\1";fold -w '$(($COLUMNS - 11))' -s <<\\!|sed "1!s/^/ /"\n\2\n!\n:' | bash