1

我正在尝试找到一个解决方案(Korn Shell 脚本)来解决我将一长行文本拆分为多行段落的问题。该脚本将在 AIX 5.3 上运行

文本最长为 255 个字符,从 VARCHAR2 类型的 Oracle 表列字段中读取。

我想将它分成 10 行,每行最少 20 个字符,最多 30 个字符,同时确保单词不会在 2 行之间拆分。

我已经尝试过,到目前为止,我已经实现了通过使用多个 SUBSTR 调用在 SQL 查询本身内拆分的能力,但这并不能解决我没有将同一个单词拆分成两行的问题,因此希望看看这是否可以在 Shell 脚本中解决。

例如,如果输入变量是

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.

输出应该是

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.

感谢有人可以指导我。这可以使用 sed 或 awk 来实现吗?或者是其他东西。

4

3 回答 3

4

这个怎么样?

    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

结果:

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
于 2013-06-20T23:36:37.343 回答
1

一种使用方式awk

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

测试:

$ 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. 
于 2013-06-20T23:53:05.890 回答
0

你们不知道“人”吗?

man fmt

给出一个页面,顶部有

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

因此:

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.
*
*
*
*
*
*******************************************************************************
于 2013-06-21T12:27:15.643 回答