5

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

10
  • 6
    please can you show us an example of what you mean?
    – Zanna
    Commented Sep 8, 2016 at 13:20
  • So you want to add line numbers to a plain text file?
    – Byte Commander
    Commented Sep 8, 2016 at 13:20
  • 1
    And would you like unnumbered lines to be indented so that they start in the same column as numbered lines?
    – Byte Commander
    Commented Sep 8, 2016 at 13:35
  • 2
    ... 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) Commented Sep 8, 2016 at 13:36
  • 4
    Your classmate has already asked this. It just doesn't make any sense. cat doesn't work with paragraphs. (But maybe the prof meant “line” rather than “paragraph”.) Commented Sep 8, 2016 at 16:17

5 Answers 5

13

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

5
  • 1
    Ooh you're speedy! Does smell like homework though!
    – Arronical
    Commented Sep 8, 2016 at 13:45
  • 1
    @Arronical shrug sure, but I'm not a detective, I just answer the question ;)
    – Zanna
    Commented Sep 8, 2016 at 13:48
  • There's also -n flag i think. Check man page, there's gotta be some difference Commented Sep 8, 2016 at 19:32
  • 1
    -n is all lines whether empty or not @Serg
    – Zanna
    Commented Sep 8, 2016 at 20:09
  • @Zanna Ah , so there is a difference ! Commented Sep 8, 2016 at 21:57
7

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

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
5
  • This does not indent unnumbered lines and it removes blank lines from the output.
    – Byte Commander
    Commented Sep 8, 2016 at 13:36
  • It'd be useful to show the OP the redirection to send this output to another file too.
    – Arronical
    Commented Sep 8, 2016 at 13:36
  • 1
    @ByteCommander: The OP doesn't mention anything about indenting unnumbered lines
    – user000001
    Commented Sep 8, 2016 at 13:40
  • Yes, 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...
    – Byte Commander
    Commented Sep 8, 2016 at 13:48
  • 1
    +1 as the OP wasn't totally clear about definitely needing cat in the orignal version of the question.
    – Arronical
    Commented Sep 8, 2016 at 14:06
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
0

I know of no "paragraph" command. cat -b is what you want to use.

Assuming this is for a specific controlled assessment task this year ;) The confusion between lines and paragraph numbering probably stems from the fact that if you make the text file using pico/nano you have made it look like a paragraph by pressing enter while writing the contents, therefore each of your "paragraphs" is only one line.

Try making the file in a test editor in the desktop environment that has word wrap. You will see the cat -b output looks as you expected. A paragraph in a command line editor is just a very long single line of text that does not word wrap.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .