Guddu
Guddu

Reputation: 1588

AIX Shell Script - Formatting a long line of 255 Characters into a 10 line paragraph

I am trying to find a solution (Korn Shell Script) to my problem of splitting a long line of text into a multi-line paragraph. The script will run on AIX 5.3

The text will be a maximum of 255 Characters long and is read from a Oracle table column field of VARCHAR2 Type.

I would like to split it into 10 lines of minimum 20 and maximum 30 Characters per line and at the same time ensuring that the words don't get split between 2 lines.

I have tried and so far, I have achieved the ability to split within the SQL Query itself by using multiple SUBSTR calls but that does not solve my problem of not having the same word split across two lines and hence hoping to see if this can be solved within the Shell Script.

So for example, if the input variable is

Life is not about searching for the things that could be found. It's about letting the unexpected happen. And finding things you never searched for. Sometimes, the best way to move ahead in life is to admit that you've enough.

Output should be

Life is not about searching for 
the things that could be found. 
It's about letting the unexpected
happen. And finding things you 
never searched for. Sometimes, the 
best way to move ahead in life is 
to admit that you've enough.

Appreciate if someone could guide me. Can this be achieved using sed or awk? Or something else.

Upvotes: 1

Views: 950

Answers (3)

pedz
pedz

Reputation: 2349

Don't you guys know about "man" ?

man fmt

gives a page, the top has

/usr/bin/fmt [ -Width ] [ File ... ]

thus:

fmt -20 < /etc/motd
*******************************************************************************
*
*
*
* *  Welcome to AIX
Version
6.1!
*
*
*
*
* *  Please see the
README file in
/usr/lpp/bos for
information
pertinent to    *
*  this release of
the AIX Operating
System.
*
*
*
*
*
*******************************************************************************

Upvotes: 0

KeepCalmAndCarryOn
KeepCalmAndCarryOn

Reputation: 9075

How about this?

    echo "Life is not about searching for the things that could be \
found. It's about letting the unexpected happen. And finding things \
you never searched for. Sometimes, the best way to move ahead in life \
is to admit that you've enough" |
        fmt -w 30

Result:

Life is not about searching
for the things that could be
found.  It's about letting
the unexpected happen.
And finding things you never
searched for.  Sometimes,
the best way to move ahead
in life  is to admit that
you've enough

Upvotes: 4

jaypal singh
jaypal singh

Reputation: 77135

One way using awk:

awk '{for(i=1;i<=NF;i++){printf("%s%s",$i,i%6?" ":"\n")}}'

Test:

$ echo "$line" | awk '{for(i=1;i<=NF;i++){printf("%s%s",$i,i%6?" ":"\n")}}'
Life is not about searching for
the things that could be found.
It's about letting the unexpected happen.
And finding things you never searched
for. Sometimes, the best way to
move ahead in life is to
admit that you've enough. 

Upvotes: 1

Related Questions