The par
text formatter (and GNU sed
) (see end for non-par
solution):
$ tr -s ' ' <file.in | awk '{ print $0, "\n" }' | par 50p8h | sed -r -e '/^$/d' -e 's/^ {8}/\t/'
11430.00 SH: gry to dk gry, firm to mod hd, plty,
flk, ea to gt, abd LCM; SLTST: gry, sft to
firm, amor to blky, slty to ea
11460.00 SH: gry to dk gry, firm to mod hd, plty,
flk, ea to gt, abd LCM; SLTST: gry, sft to
firm, amor to blky, slty to ea
11490.00 MRL: lt gry, mod hd, blky, occ flk, wxy;
SH: gry to dk gry, firm to mod hd, plty,
occ blky, ea to gt; SLTST: gry to dk gry,
mod firm to firm, amor, blky, slty
11520.00 SH: gry to dk gry, firm to mod hd, plty,
blky, ea to gt, tr MRL, occ LCM; SLTST:
gry, occ brnsh rd, firm, amor to blky, ea
to g
tr -s ' '
compresses multiple successive spaces into one.
- The
awk
code just adds an extra newline to each line of input.
par 50p8h
formats the resulting text to a width of 50 characters with 8 characters of hanging indent.
- The
sed
expressions deletes empty lines and replaces runs of 8 spaces at the start of a line with a single tab character.
For a solution that doesn't use GNU sed
, you must insert a literal tab character in place of \t
in the last sed
expression.
For a solution that uses spaces for the indent and which aligns the left hanging indent exactly as in your screenshot (9 spaces in):
$ tr -s ' ' <file | awk '{ print $0, "\n" }' | par 50p9h | sed -e '/^$/d'
11430.00 SH: gry to dk gry, firm to mod hd, plty,
flk, ea to gt, abd LCM; SLTST: gry, sft
to firm, amor to blky, slty to ea
11460.00 SH: gry to dk gry, firm to mod hd, plty,
flk, ea to gt, abd LCM; SLTST: gry, sft
to firm, amor to blky, slty to ea
11490.00 MRL: lt gry, mod hd, blky, occ flk, wxy;
SH: gry to dk gry, firm to mod hd, plty,
occ blky, ea to gt; SLTST: gry to dk gry,
mod firm to firm, amor, blky, slty
11520.00 SH: gry to dk gry, firm to mod hd, plty,
blky, ea to gt, tr MRL, occ LCM; SLTST:
gry, occ brnsh rd, firm, amor to blky, ea
to g
Adding j
to 50p9h
will justify the paragraphs nicely:
11430.00 SH: gry to dk gry, firm to mod hd, plty,
flk, ea to gt, abd LCM; SLTST: gry, sft
to firm, amor to blky, slty to ea
11460.00 SH: gry to dk gry, firm to mod hd, plty,
flk, ea to gt, abd LCM; SLTST: gry, sft
to firm, amor to blky, slty to ea
11490.00 MRL: lt gry, mod hd, blky, occ flk, wxy;
SH: gry to dk gry, firm to mod hd, plty,
occ blky, ea to gt; SLTST: gry to dk gry,
mod firm to firm, amor, blky, slty
11520.00 SH: gry to dk gry, firm to mod hd, plty,
blky, ea to gt, tr MRL, occ LCM; SLTST:
gry, occ brnsh rd, firm, amor to blky, ea
to g
... and adding l
to that will force-justify the last line of each paragraph too (not as nice):
11430.00 SH: gry to dk gry, firm to mod hd, plty,
flk, ea to gt, abd LCM; SLTST: gry,
sft to firm, amor to blky, slty to ea
11460.00 SH: gry to dk gry, firm to mod hd, plty,
flk, ea to gt, abd LCM; SLTST: gry,
sft to firm, amor to blky, slty to ea
11490.00 MRL: lt gry, mod hd, blky, occ flk, wxy;
SH: gry to dk gry, firm to mod hd, plty,
occ blky, ea to gt; SLTST: gry to dk
gry, mod firm to firm, amor, blky, slty
11520.00 SH: gry to dk gry, firm to mod
hd, plty, blky, ea to gt, tr MRL,
occ LCM; SLTST: gry, occ brnsh
rd, firm, amor to blky, ea to g
par
is available from most package managers on most Unices, but you may also find its sources (to compile yourself) at http://www.nicemice.net/par/
Solution using fmt
instead of par
$ tr -s ' ' <file.in | awk '{ print $0, "\n" }' | fmt -w 50 |
awk '/^[^0-9]/ { $0 = " " $0 }
{ print }' | fmt -w 50 | sed '/^$/d'
11430.00 SH: gry to dk gry, firm to mod hd, plty,
flk, ea to gt, abd LCM; SLTST: gry, sft
to firm, amor to blky, slty to ea
11460.00 SH: gry to dk gry, firm to mod hd, plty,
flk, ea to gt, abd LCM; SLTST: gry, sft
to firm, amor to blky, slty to ea
11490.00 MRL: lt gry, mod hd, blky, occ flk, wxy;
SH: gry to dk gry, firm to mod hd, plty,
occ blky, ea to gt; SLTST: gry to dk gry,
mod firm to firm, amor, blky, slty
11520.00 SH: gry to dk gry, firm to mod hd, plty,
blky, ea to gt, tr MRL, occ LCM; SLTST:
gry, occ brnsh rd, firm, amor to blky, ea
to g
fmt
is not as flexible in its formatting and here we need to use it twice to get the intended result. We also use the fact that each original line starts with a digit.
tr -s ' '
, as before.
awk '{ print $0, "\n" }'
, as before.
- The first
fmt
call (fmt -w 50
) is there to get the first line of each paragraph into the right width (50 characters).
- The
awk
script will indent each line that does not start with a digit by 9 spaces.
- The second
fmt
call will format the whole text to 50 characters, but now the indented lines will stay indented.
- The
sed
expression deletes empty lines.