首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AIX Shell脚本-将255个字符的长行格式化为10行段落

AIX Shell脚本-将255个字符的长行格式化为10行段落
EN

Stack Overflow用户
提问于 2013-06-21 06:59:21
回答 3查看 883关注 0票数 1

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

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

我想把它分成10行,每行最少20个字符,最多30个字符,同时确保单词不会被分成2行。

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

例如,如果输入变量是

代码语言:javascript
复制
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.

输出应为

代码语言:javascript
复制
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来实现吗?或者别的什么。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-06-21 07:36:37

这个怎么样?

代码语言:javascript
复制
    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

结果:

代码语言:javascript
复制
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
票数 4
EN

Stack Overflow用户

发布于 2013-06-21 07:53:06

使用awk的一种方式

代码语言:javascript
复制
awk '{for(i=1;i<=NF;i++){printf("%s%s",$i,i%6?" ":"\n")}}'

测试:

代码语言:javascript
复制
$ 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. 
票数 1
EN

Stack Overflow用户

发布于 2013-06-21 20:27:16

你们不知道“男人”这个词吗?

代码语言:javascript
复制
man fmt

给出一个页面,顶部有

代码语言:javascript
复制
/usr/bin/fmt [ -Width ] [ File ... ]

因此:

代码语言:javascript
复制
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.
*
*
*
*
*
*******************************************************************************
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17225218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档