我有一个目录(机密),其中包含一堆文本文件。
保密
:- 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)。
由于
A
答案 0 :(得分:1)
这是我想到的最简单的事情,因为你没有写下你试过的东西我会给你留下可能的调整,但我相信这是一个好的开始;)
for file in "*"; do echo "$file\t\t$(head -1 "$file")"; done
答案 1 :(得分:1)
你可以用几行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="")
打印
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.
它不是shell,但它会更快,因为你不会花费大量的进程,而且你不会花费大量时间进行字符串格式化和编写循环来缩进文本{{ 3}}可以为你做。
答案 2 :(得分:0)
查看Unix中的fmt
命令。它可以以特定宽度重新格式化文档,甚至可以控制缩进。
我使用它已经有很长一段时间了。但是,它可以跟随缩进,设置宽度等。我感觉它可以做你想要的。
要查看的另一个命令是pr
。默认情况下,pr
会将文字分成页面,并添加页码,但您可以将所有内容都转换为页面。这是另一个可能能够按照您想要的方式处理文本的命令。