我正在尝试找到一个解决方案(Korn Shell Script)来解决将长行文本拆分成多行段落的问题。该脚本将在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实现吗?或者别的什么。
答案 0 :(得分: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
答案 1 :(得分: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.
答案 2 :(得分: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.
*
*
*
*
*
*******************************************************************************