Cam*_*ers 5 command-line bash text-processing cat
我目前正在尝试找到一种方法来使用 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.
Run Code Online (Sandbox Code Playgroud)
然后一旦输入命令:
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.
Run Code Online (Sandbox Code Playgroud)
老实说,这是我认为很容易找到的东西,但我一直无法找到一个网站来回答如何做到这一点。(请记住,它必须是cat命令的变体。)
Zan*_*nna 13
如果段落实际上是像您的示例一样的一行,并且您必须只使用cat,那么您肯定想要-b(编号非空行)?
cat -b file
Run Code Online (Sandbox Code Playgroud)
好像:
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.
Run Code Online (Sandbox Code Playgroud)
要将其保存在文件中而不是在终端中打印:
cat -b file > file2
Run Code Online (Sandbox Code Playgroud)
如果你真的需要它们,你可以在你的数字之后添加点,虽然不是,afaik,而不用使用另一个命令来帮助cat,比如sed,这里用它们替换以它们开头的行中的空格和数字(因为cat -b缩进)相同的模式加上一个.to make1. 2.等(这是@terdon 建议的如此之快,我没有时间自己制作并获得荣誉)
cat -b file | sed -r 's/^\s+[0-9]+/&./' > file2
Run Code Online (Sandbox Code Playgroud)
在您的示例中,每个段落实际上只是一行。将它形成一个段落的唯一方法是在用于显示它的任何应用程序中进行文本换行。
您可以对文件中的所有非空行进行编号,使用catwith:
cat -b file
Run Code Online (Sandbox Code Playgroud)
如果要将其发送到另一个文件,请使用重定向:
cat -b file > newfile
Run Code Online (Sandbox Code Playgroud)
该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 bug-coreutils@gnu.org
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.
Run Code Online (Sandbox Code Playgroud)
如果“段落”是指由空行分隔的行块,则可以使用简单的 awk 命令添加编号:
awk -v RS= '{print ++i, $0}' file
Run Code Online (Sandbox Code Playgroud)
要保留输出中的空行,您可以将 ORS 变量设置为\n\n:
awk -v RS= -vORS='\n\n' '{print ++i, $0}' file
Run Code Online (Sandbox Code Playgroud)
如果要将输出保存到新文件,可以使用如下重定向:
awk -v RS= '{print ++i, $0}' file > newfile
Run Code Online (Sandbox Code Playgroud)