我正在尝试找到一个解决方案(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来实现吗?或者别的什么。
发布于 2013-06-21 07:36:37
这个怎么样?
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-21 07:53:06
使用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-21 20:27:16
你们不知道“男人”这个词吗?
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.
*
*
*
*
*
*******************************************************************************https://stackoverflow.com/questions/17225218
复制相似问题