哲思官方群认证群组  - 讨论区

标题:[zeuux-universe] 如何调整纯文本表格宽度

2011年05月26日 星期四 09:28

高增琦 pgf00a在gmail.com
星期四 五月 26 09:28:37 CST 2011

给力...
我第一时间想到的实现就是用c一个字符一个字符的读取,然后强制写到屏幕的指定位置...

2011/5/26 Zhang Weiwu <almustafa在realss.com>

>
> On Wed, 25 May 2011, 高增琦 wrote:
>
>  抱歉,说的不太清楚,其实是这么个意思:
>>
>
>
> 我原以为你说的算法实现起来比说清楚容易,如今发现实践起来也花了我一些时间,
> 肯定多于文字说明的时间了。
>
> 实践效果基本令人满意!但仅测试了一个文本. 注意为了测试这个文本,你需要能一字不差拷贝粘贴的软件,有些邮件软件会把下
> 面文本中的tab换为空格。
>
> filename is /tmp/input.txt:
>
> The sun went down with practiced bravado. Twilight crawled across the sky,
> laden with foreboding.       After Y2K, the end of the world had become a
> cliché. But who was I to talk, a brooding underdog avenger alone against an
> empire of evil out to right a grave injustice. Everything was subjective.
> There were only personal apocalypses. Nothing is a cliché when it is
> happening to you.
> It was a lucky break. The goons inside were spooked, but luck always came
> with a price tag.     It was not about how good you were. It was chaos and
> luck and anyone who thought differently was a fool.
> Snow fell like ash from post-apocalyptic skies. Who was I kidding, the best
> I was, was superman on kryptonite.
>
>
> $ foldcol.awk width=72 pass=1 /tmp/input.txt pass=2 /tmp/input.txt
> [The sun went down with  ][After Y2K, the end of the world had become a   ]
> [practiced bravado.      ][cliché. But who was I to talk, a brooding      ]
> [Twilight crawled across ][underdog avenger alone against an empire of    ]
> [the sky, laden with     ][evil out to right a grave injustice.           ]
> [foreboding.             ][Everything was subjective. There were only     ]
> [                        ][personal apocalypses. Nothing is a cliché      ]
> [                        ][when it is happening to you.                   ]
> [                        ][                                               ]
> [It was a lucky break.   ][It was not about how good you were. It was     ]
> [The goons inside were   ][chaos and luck and anyone who thought          ]
> [spooked, but luck       ][differently was a fool.                        ]
> [always came with a      ][                                               ]
> [price tag.              ][                                               ]
> [                        ][                                               ]
> [Snow fell like ash from ][Who was I kidding, the best I was, was         ]
> [post-apocalyptic skies. ][superman on kryptonite.                        ]
> [                        ][                                               ]
>
>
> The source code:
>
> #!/usr/bin/awk -f
> # foldcol.awk: perform like fold(1) plus column(1)
> # example: foldcol.awk width=72 pass=1 /tmp/input.txt pass=2 /tmp/input.txt
> BEGIN { FS="\t"; width=80; } # table default to 80 columns wide
>
> pass==1 {
>        for (i=1; i<=NF; i++) {
>                c[i] += length($i); # count of number of chars in each
> column
>                t += length($i);    # total chars in all columns
>        }
> }
>
> pass==2 {
>        for (i=1; i<=NF; i++) {
>                cmd[i] = "echo '" $i "' | " "fold -sw" int(c[i]/t*width)
>        }
>        tr = 1  # number of columns that has output of fold(1)
>        while (tr > 0) {     # go on until no column has output from fold(1)
>                tr = 0
>                for (i=1; i<=NF; i++) {
>                        l = "";
>                        tr += (cmd[i] | getline l)
>                        printf "[%-" int(c[i]/t*width) "s]", l
>                }
>                print "";
>        }
>        for (i=1; i<=NF; i++) {
>                close(cmd[i])
>        }
> }




-- 
GaoZengqi
pgf00a在gmail.com
zengqigao在gmail.com
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: <http://www.zeuux.org/pipermail/zeuux-universe/attachments/20110526/7d4626f0/attachment-0001.html>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-universe]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2024

    京ICP备05028076号