Saturday, February 24, 2024
13
rated 0 times [  13] [ 0]  / answers: 1 / hits: 5522  / 3 Years ago, wed, august 4, 2021, 12:41:46

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.)


More From » command-line

 Answers
3

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



[#13815] Wednesday, August 4, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
oleard

Total Points: 344
Total Questions: 105
Total Answers: 113

Location: Bonaire
Member since Tue, Sep 20, 2022
1 Year ago
;