我目前正在尝试找到一种方法来使用 cat 命令将文本文件显示为我正在做的项目的自动编号段落,但我还找不到单个命令。
例子:
Frederick II (German: Friedrich; 24 January 1712 – 17 August 1786) was King of Prussia from 1740 until 1786.[1] His most significant accomplishments during his reign included his military victories, his reorganization of Prussian armies, his patronage of the Arts and the Enlightenment in Prussia, and his final success against great odds in the Seven Years' War.
Frederick was the last titled King in Prussia and declared himself King of Prussia after achieving full sovereignty for all historical Prussian lands. Prussia had greatly increased its territories and became a leading military power in Europe under his rule. He became known as Frederick the Great (Friedrich der Große) and was affectionately nicknamed Der Alte Fritz ("Old Fritz") by the Prussian people.
然后一旦输入命令:
1. Frederick II (German: Friedrich; 24 January 1712 – 17 August 1786) was King of Prussia from 1740 until 1786.[1] His most significant accomplishments during his reign included his military victories, his reorganization of Prussian armies, his patronage of the Arts and the Enlightenment in Prussia, and his final success against great odds in the Seven Years' War.
2.Frederick was the last titled King in Prussia and declared himself King of Prussia after achieving full sovereignty for all historical Prussian lands. Prussia had greatly increased its territories and became a leading military power in Europe under his rule. He became known as Frederick the Great (Friedrich der Große) and was affectionately nicknamed Der Alte Fritz ("Old Fritz") by the Prussian people.
我真的以为我会很容易找到这个,但我找不到一个网站可以回答如何做到这一点。(请记住,它必须是命令的变体cat
。)
答案1
如果段落实际上是像您的示例一样的一行,并且您只能使用cat
,那么您肯定想要-b
(非空行数)?
cat -b file
好像:
1 Frederick II (German: Friedrich; 24 January 1712 – 17 August 1786) was King of Prussia from 1740 until 1786.[1] His most significant accomplishments during his reign included his military victories, his reorganization of Prussian armies, his patronage of the Arts and the Enlightenment in Prussia, and his final success against great odds in the Seven Years' War.
2 Frederick was the last titled King in Prussia and declared himself King of Prussia after achieving full sovereignty for all historical Prussian lands. Prussia had greatly increased its territories and became a leading military power in Europe under his rule. He became known as Frederick the Great (Friedrich der Große) and was affectionately nicknamed Der Alte Fritz ("Old Fritz") by the Prussian people.
要将其保存在文件中而不是在终端中打印:
cat -b file > file2
如果你真的需要它们,你可以在数字后添加点,但据我所知,不需要使用另一个命令来帮助cat
,比如sed
,这里用相同的模式加上一个来替换以它们开头的行中的空格和数字(因为缩进cat -b
)等等(这是@terdon 建议的,速度非常快,我没有时间自己做并承担责任).
1.
2.
cat -b file | sed -r 's/^\s+[0-9]+/&./' > file2
答案2
在您的示例中,每个段落实际上只是一行。将其形成段落的唯一方法是通过文本换行(无论使用哪个应用程序来显示它)。
您可以使用以下方式对文件中的所有非空行进行编号cat
:
cat -b file
如果您想将其发送到另一个文件,请使用重定向:
cat -b file > newfile
该man
命令对于了解其他命令的用途非常有用,例如man cat
:
NAME
cat - concatenate files and print on the standard output
SYNOPSIS
cat [OPTION]... [FILE]...
DESCRIPTION
Concatenate FILE(s), or standard input, to standard output.
-A, --show-all
equivalent to -vET
-b, --number-nonblank
number nonempty output lines, overrides -n
-e equivalent to -vE
-E, --show-ends
display $ at end of each line
-n, --number
number all output lines
-s, --squeeze-blank
suppress repeated empty output lines
-t equivalent to -vT
-T, --show-tabs
display TAB characters as ^I
-u (ignored)
-v, --show-nonprinting
use ^ and M- notation, except for LFD and TAB
--help display this help and exit
--version
output version information and exit
With no FILE, or when FILE is -, read standard input.
EXAMPLES
cat f - g
Output f's contents, then standard input, then g's contents.
cat Copy standard input to standard output.
AUTHOR
Written by Torbjorn Granlund and Richard M. Stallman.
REPORTING BUGS
Report cat bugs to [email protected]
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report cat translation bugs to <http://translationproject.org/team/>
COPYRIGHT
Copyright © 2013 Free Software Foundation, Inc. License GPLv3+: GNU
GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
SEE ALSO
tac(1)
The full documentation for cat is maintained as a Texinfo manual. If
the info and cat programs are properly installed at your site, the
command
info coreutils 'cat invocation'
should give you access to the complete manual.
答案3
如果“段落”指的是由空行分隔的行块,则可以使用简单的 awk 命令添加编号:
awk -v RS= '{print ++i, $0}' file
为了保留输出中的空白行,您可以将 ORS 变量设置为\n\n
如下形式:
awk -v RS= -vORS='\n\n' '{print ++i, $0}' file
如果要将输出保存到新文件,可以使用如下重定向:
awk -v RS= '{print ++i, $0}' file > newfile
答案4
我不知道有“段落”命令。cat -b
这就是您想要使用的。
假设这是今年的一项特定的受控评估任务;)行号和段落编号之间的混淆可能源于这样一个事实:如果您使用 pico/nano 制作文本文件,您在编写内容时按回车键使其看起来像一个段落,因此您的每个“段落”只有一行。
尝试在具有自动换行功能的桌面环境中的测试编辑器中创建该文件。您将看到输出cat -b
符合预期。命令行编辑器中的段落只是一行很长的文本,不自动换行。