0

我有一个目录(机密),其中包含一堆文本文件。

机密的

  :- Secret-file1.txt
  :- Secret-file2.txt
  :- Secret-file3.txt

我想生成另一个文本文件(Summary.txt),文本宽度为 80,格式如下

Secret-file1         - This file describes various secret activities of
                       organization Secret-Organization-1
Secret-file2         - This file describes various secret activities of
                       organization Secret-Organization-2. This summarizes
                       their activities from year 2001.
Secret-file3         - This file describes various secret activities of
                       organization Secret-Organization-3. This summarizes
                       their activities from year 2024.

其中第二列右对齐并从相应文本文件的第一行复制。例如,“Secret-file1.txt”看起来像这样

This file describes various secret activities of organization Secret-Organization-1.
XXXXXXXXXXXXXXXXX BUNCH of TEXT TILL EOF XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

我怎样才能做到这一点?我正在查看 bash 的各种选项(例如,sed、awk、grep、your-prefered-bash-built-in)。

谢谢

一个

4

3 回答 3

1

这是我想到的最简单的事情,因为您没有写出您尝试过的内容,所以我将可能的调整留给您,但我相信这是一个好的开始;)

for file in "*"; do echo "$file\t\t$(head -1 "$file")"; done
于 2013-07-04T17:10:50.427 回答
1

You can do this cleanly with a few lines of Python:

#!/usr/bin/env python3.3

import glob
import textwrap
from os.path import basename

INDENT=' ' * 22

for filename in glob.glob("Confidential/*.txt"):
    with open(filename, 'r') as secret:
        print("{:20s}- {}\n".format(
            basename(filename),
            '\n'.join(textwrap.wrap(secret.readline(),
                                    width=74,
                                    initial_indent=INDENT,
                                    subsequent_indent=INDENT)).strip()),
            end="")

prints

Secret-file1.txt    - This file describes various secret activities of
                      organization Secret-Organization-1
Secret-file2.txt    - This file describes various secret activities of
                      organization Secret-Organization-2. This summarizes
                      their activities from year 2001.
Secret-file3.txt    - This file describes various secret activities of
                      organization Secret-Organization-3. This summarizes
                      their activities from year 2024.

It’s not shell, but it’s going to be faster because you’re not forking a bunch of processes, and you’re not going to spend a ton of time with string-formatting and writing loops to indent the text when the textwrap module can do it for you.

于 2013-07-04T17:34:07.197 回答
0

看看fmtUnix 中的命令。它可以以特定宽度重新格式化您的文档,甚至可以控制缩进。

我已经很久没有使用它了。但是,它可以跟随缩进,设置宽度等。我感觉它可以做你想做的事。

另一个要查看的命令是pr. pr,默认情况下将文本分成页面,并添加页码,但您可以关闭所有这些。这是另一个可以按照您想要的方式修改文本的命令。

于 2013-07-05T00:13:53.940 回答