How to format text file as it can be seen in man pages (justifying text, nothing more) using bash

StackOverflow https://stackoverflow.com/questions/22581267

  •  19-06-2023
  •  | 
  •  

سؤال

What I would like to do is the following.

Text file content :

This is a simple text file
containing lines of text
with different width
but I would like to justify
them. Any idea ?

Expected result :

This is a simple text file containing
lines of  text with  different  width
but I  would  like  to  justify them.
Any Idea ?

I already can split my files at the required width using :

cat textfile|fmt -s -w 37

But in that case, there is no justification...

EDIT : Using par as suggested, I found a problem with accented chars. This is what gives par 37j1 for me :

This   is   à  simplé   text   file
containing   lines   of  tèxt   with
different wïdth but I woùld like to
justîfy them. Any idéà ?

Not justified anymore... But spaces are altered anyway...

Thanks for your help,

Slander

هل كانت مفيدة؟

المحلول

You can employ nroff as using it man.

(echo '.ll 37'
 echo '.pl 0'
 cat orig.txt) | nroff

from your input produces:

This is a simple text file containing
lines of text  with  different  width
but I would like to justify them. Any
idea ?

The above WORKS ONLY WITH ASCII.

EDIT

If you want handle utf8 text with a nroff, you can try the next:

cat orig.txt | (        #yes, i know - UUOC
    echo '.ll 37'     #line length
    echo '.pl 0'      #page length (0-disables empty lines)
    echo '.nh'        #no hypenation
    preconv -e utf8 -
) | groff -Tutf8

From this utf8 encoded input:

Voix ambiguë d'un cœur qui au zéphyr préfère les jattes de kiwi.
Voyez le brick géant que j'examine près du wharf.
Monsieur Jack, vous dactylographiez bien mieux que votre ami Wolf.
Eble ĉiu kvazaŭ-deca fuŝĥoraĵo ĝojigos homtipon..
Laŭ Ludoviko Zamenhof bongustas freŝa ĉeĥa manĝaĵo kun spicoj.
Nechť již hříšné saxofony ďáblů rozezvučí síň úděsnými tóny waltzu, tanga a
quickstepu.

produces:

Voix  ambiguë d’un cœur qui au zéphyr
préfère les jattes de kiwi.  Voyez le
brick  géant  que  j’examine  près du
wharf.     Monsieur    Jack,     vous
dactylographiez  bien mieux que votre
ami  Wolf.   Eble   ĉiu   kvazaŭ‐deca
fuŝĥoraĵo   ĝojigos  homtipon..   Laŭ
Ludoviko  Zamenhof  bongustas   freŝa
ĉeĥa  manĝaĵo  kun spicoj.  Nechť již
hříšné saxofony ďáblů  rozezvučí  síň
úděsnými   tóny   waltzu,   tanga   a
quickstepu.

If you delete the line

echo '.nh'   #no hypenation

you will get hypenated text

Voix  ambiguë d’un cœur qui au zéphyr
préfère les jattes de kiwi.  Voyez le
brick  géant  que  j’examine  près du
wharf.  Monsieur Jack, vous  dactylo‐
graphiez  bien  mieux  que  votre ami
Wolf.  Eble ĉiu kvazaŭ‐deca fuŝĥoraĵo
ĝojigos  homtipon..  Laŭ Ludoviko Za‐
menhof bongustas freŝa  ĉeĥa  manĝaĵo
kun  spicoj.   Nechť již hříšné saxo‐
fony  ďáblů  rozezvučí  síň  úděsnými
tóny waltzu, tanga a quickstepu.

نصائح أخرى

You could use par:

par -j -w37 < inputfile
  • The -j option would justify paragraphs.
  • -w denotes max output line length.

For your input, it'd produce:

This is a simple text file containing
lines  of text  with different  width
but I would like to justify them. Any
idea ?

An alternative would be to use emacs:

emacs -batch inputfile --eval '(set-fill-column 37)' --eval '(fill-region (point-min) (point-max))' -f save-buffer

This would also produce:

This is a simple text file containing
lines of text with different width
but I would like to justify them. Any
idea ?
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top