unix - Simple bash/script formatting tool, for paragraph text inside the script? -
context: know how type sentences in word processor, , don't have special formatting if insert new sentence in paragraph, or make existing sentence longer or shorter?
i'm writing paragraphs document given script does, inside it. want able output when user invokes script "-help", etc option, not have adjust echo or printf statements everytime add sentence, or make 1 longer or shorter. how can that?
i'm willing make output 80 columns or fixed width.
edit reader: answers informative found.
you use widely-available fmt
utility.
if have gnu coreutils (which if using linux), can use the gnu version. there very similar utility available on many bsd systems, including mac os x.
here's simple example, should work either of implementations. note second paragraph indented 2 spaces, , indentation preserved:
description="\ lorem ipsum dolor sit amet, consectetur adipiscing elit. aenean ornare leo non dolor porttitor euismod. cras commodo, nisi vel gravida volutpat, enim turpis tempor eros, ut venenatis elit leo ut nunc. nulla fermentum ligula id tincidunt porttitor. morbi ut massa vitae tortor rutrum gravida ut id nunc. integer imperdiet pharetra augue, quis finibus justo luctus id. phasellus diam ac risus consequat pharetra. cras lacinia neque sed ipsum euismod, non commodo felis facilisis. suspendisse luctus purus justo, sed iaculis lectus consequat nec. etiam pretium ultricies ligula, pretium sapien facilisis eu. nulla rhoncus viverra turpis rutrum. cras eu porttitor urna. duis nec metus vel nisi accumsan scelerisque. cras lectus erat, mattis non mauris in, consectetur vulputate ipsum. " fmt -w 60 <<<"$description"
output:
lorem ipsum dolor sit amet, consectetur adipiscing elit. aenean ornare leo non dolor porttitor euismod. cras commodo, nisi vel gravida volutpat, enim turpis tempor eros, ut venenatis elit leo ut nunc. nulla fermentum ligula id tincidunt porttitor. morbi ut massa vitae tortor rutrum gravida ut id nunc. integer imperdiet pharetra augue, quis finibus justo luctus id. phasellus diam ac risus consequat pharetra. cras lacinia neque sed ipsum euismod, non commodo felis facilisis. suspendisse luctus purus justo, sed iaculis lectus consequat nec. etiam pretium ultricies ligula, pretium sapien facilisis eu. nulla rhoncus viverra turpis rutrum. cras eu porttitor urna. duis nec metus vel nisi accumsan scelerisque. cras lectus erat, mattis non mauris in, consectetur vulputate ipsum.
incorporating above in script.
you can trivially add script (thanks @ninjaj suggestion using tput desired column width.)
#!/bin/bash description=... # above, not repeated space # check see if first argument cry if [[ $1 == -help ]]; fmt -w $(tput cols) <<< "$description" exit 0 fi
Comments
Post a Comment