读取输入并打印到具有多行换行功能的表格

时间:2014-11-30 21:18:08

标签: bash

我的stdin看起来像这样:

1. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
2. This is a test, this is only a test.  Do not adjust your television.  Blah, blah, blah; yak, yak yak; foo, bar, baz, qux.
3. Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.

我希望能够在命令行的表格中对此进行整理 - 使用描述包装并继续到下一行的相应列:

 1. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo...
 2. This is a test, this is only a test.  Do not adjust your television.  Blah,
    blah, blah; yak, yak yak; foo, bar, baz, qux.
 3. Four score and seven years ago our fathers brought forth on this continent,
    a new nation, conceived in Liberty, and dedicated to the proposition that
    all men are created equal.

硬包装或软包装都可以。

任何人都知道如何做到这一点。

1 个答案:

答案 0 :(得分:1)

不是最漂亮的,但这会奏效。

$ cat input
1. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
2. This is a test, this is only a test.  Do not adjust your television.  Blah, blah, blah; yak, yak yak; foo, bar, baz, qux.
3. Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.
$ cat input | fold -s -w 78| awk '/^[[:digit:]]+\./{a=length($1); print; next} {sub(/^ +/, ""); printf "%*s%s\n", a+1, " ", $0}'
1. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
   tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
   quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
   consequat.
2. This is a test, this is only a test.  Do not adjust your television.
   Blah, blah, blah; yak, yak yak; foo, bar, baz, qux.
3. Four score and seven years ago our fathers brought forth on this
   continent, a new nation, conceived in Liberty, and dedicated to the
   proposition that all men are created equal.

我给fold一个略小于实际窗口的大小,以便添加的填充不会从窗口的末端推出一行(并再次换行)。