Numbering paragraphs with the 'cat' command
Solution 1
If the paragraph is actually a line like your example, and you have to use only cat
, then surely you want -b
(number non-empty lines)?
cat -b file
looks like:
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.
To save this in a file instead of printing in the terminal:
cat -b file > file2
In case you really need them, you could add the dots after your numbers, though not, afaik, without resorting to using another command to help cat
, like sed
, which here replaces whitespace and numbers in lines that start with them (since cat -b
indents) with the same pattern plus a .
to make 1.
2.
etc (this was suggested by @terdon so blazingly fast I didn't have time to make it myself & take the credit)
cat -b file | sed -r 's/^\s+[0-9]+/&./' > file2
Solution 2
In your example, each paragraph is actually just a single line. The only way it would be formed into a paragraph would be by text wrapping in whichever application was being used to display it.
You can number all non-empty lines in a file, using cat
with:
cat -b file
If you want to send this to another file, use redirection:
cat -b file > newfile
The man
command is really useful for learning about the uses for other commands, for instance man cat
gives:
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.
Solution 3
If by "paragraphs" you mean blocks of lines separated by empty lines, you could add the numbering with simple awk command:
awk -v RS= '{print ++i, $0}' file
To preserve the blank lines in the output, you can set the ORS variable to \n\n
like this:
awk -v RS= -vORS='\n\n' '{print ++i, $0}' file
If you want to save the output to a new file you can use a redirect like this:
awk -v RS= '{print ++i, $0}' file > newfile
Solution 4
The "paragraphs" are not paragraphs yet, just long lines (as others have noted)
We need to number the lines and then make them into paragraphs You can use fold for this.
cat -b file | fold -sw 80
This numbers non-empty lines, pipes it into fold which keeps the width at 80 characters (or columns) and breaks the line on spaces.
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
-b, --number-nonblank
number nonempty output lines, overrides -n
fold
-s, --spaces
break at spaces
-w, --width=WIDTH
use WIDTH columns instead of 80
Related videos on Youtube
Campers
Updated on September 18, 2022Comments
-
Campers over 1 year
I'm currently trying to find a way to use the cat command to show a text file as automatically numbered paragraphs for a project I'm doing, but I haven't been able to find a single command.
Example:
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.
Then once the command has been input:
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.
This is something that I honestly thought I'd find easily, but I have not been able to find a single website with an answer on how to do it. (Keep in mind it has to be a variation of the
cat
command.)-
Zanna over 7 yearsplease can you show us an example of what you mean?
-
Byte Commander over 7 yearsSo you want to add line numbers to a plain text file?
-
Campers over 7 yearsIt would be something similar to that, but I wouldn't want to number EVERY SINGLE line, it'd just be the paragraph, and yes, it is a plain text file
-
Byte Commander over 7 yearsAs Zanna said, please give us a short example of maybe two little paragraphs and what output you would like for that.
-
Byte Commander over 7 yearsAnd would you like unnumbered lines to be indented so that they start in the same column as numbered lines?
-
steeldriver over 7 years... in particular, what feature of the text delineates paragraphs (we can guess it might be one or more blank lines, but you need to be specific about that)
-
Arronical over 7 years@ByteCommander are you sure you mean
-A
, seems to just show line-end, tabs and non-printing characters? -
Byte Commander over 7 years@Arronical Oh, no. Good catch, I meant of course
-n
. -
Byte Commander over 7 yearsIn your example, each "paragraph" is just a single line though. And if you only may use the cat command (smells like homework, btw), you must use
cat -n filename
. Orcat -b filename
as suggested by Zanna and Arronical in their answers.
-
-
Byte Commander over 7 yearsThis does not indent unnumbered lines and it removes blank lines from the output.
-
Arronical over 7 yearsIt'd be useful to show the OP the redirection to send this output to another file too.
-
user000001 over 7 years@ByteCommander: The OP doesn't mention anything about indenting unnumbered lines
-
Arronical over 7 yearsOoh you're speedy! Does smell like homework though!
-
Zanna over 7 years@Arronical shrug sure, but I'm not a detective, I just answer the question ;)
-
Byte Commander over 7 yearsYes, he doesn't. And looking at the example he just provided, probably he simply wants
cat -n
, as all his "paragraphs" are just long lines... -
Arronical over 7 years+1 as the OP wasn't totally clear about definitely needing cat in the orignal version of the question.
-
Sergiy Kolodyazhnyy over 7 yearsThere's also -n flag i think. Check man page, there's gotta be some difference
-
Zanna over 7 years-n is all lines whether empty or not @Serg
-
Sergiy Kolodyazhnyy over 7 years@Zanna Ah , so there is a difference !