0

I'm looking to number my paragraphs within a text file, I'm pretty sure I have to use the command cat -n or cat -b but I'm not sure how to use it, could someone expand upon this please.

closed as unclear what you're asking by Anthon, roaima, X Tian, chaos, G-Man Oct 9 '15 at 16:47

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • man cat should help – ryekayo Oct 9 '15 at 14:49
  • -b and -n work on lines, you cannot use cat to number paragraphs unless the consist off one line (in that case use cat -b). Do you have example input? – Anthon Oct 9 '15 at 14:59
  • What is your question?  Are you trying to learn how to use the options of cat, or are you trying to solve a text-processing question and guessing that cat will be able to do it?  See What is the XY problem? and Pounding a Nail: Old Shoe or Glass Bottle?  … (Cont’d) – G-Man Oct 9 '15 at 16:45
  • (Cont’d) …  You say that you want to number paragraphs, but you don’t define “paragraph”.  Computer parsing of free-form English text is a very hard problem — see How to put sentences on separate lines in Linux — and you haven’t even specified that you’re dealing with English text.  (I don’t know that the problem would be much different in, say, French or German, but Arabic or Chinese would be different kettles of fish.) – G-Man Oct 9 '15 at 16:46
1

cat -n file.txtwill display the content of the file.txt with line numbers.
If you want to have the line numbers in the file, then you can use an I/O redirect like
cat -n file.txt > file1.txt.
But i don't think there is an option to number paragraphs.

0

Grep can tell you how many empty lines there are in the file. That will usually be 1 less than the number of paragraphs. Unless there are extra blank lines in the text.

grep -c '^$' file.txt | wc -l

It can also tell you how many non blank lines there are. I would imagine if your paragraphs are all written continuously without new lines, this would work.

grep -cv '^$' a | wc -l
0

As the other answers indicate, cat isn't a very appropriate tool for this.

As I said in my comment, your question is ill-defined because you don’t specify how the command is supposed to recognize paragraphs. One way to do that is by the first line being indented. nl -bp"^ " is a command that’s fairly well suited to handling that form of input:

$ cat text1
Some Verse
 The quick brown fox
jumps over the lazy dog.
 The Owl and the Pussy-cat went to sea
In a beautiful pea green boat,

$ nl -bp"^ " text1
       Some Verse
     1   The quick brown fox
       jumps over the lazy dog.
     2   The Owl and the Pussy-cat went to sea
       In a beautiful pea green boat,

Another way is to use blank lines as separators. awk is pretty good at handling that sort of thing.

$ cat num_pp
#!/bin/sh
awk 'BEGIN    {start=1}
     /^$/     {start=1}
    {
        if ($0 != ""  &&  start) {
                print ++ppnum, $0
                start=0
        } else print
    }' "$@"

$ cat text2
Some Verse
 The quick brown fox
jumps over the lazy dog.

The Owl and the Pussy-cat went to sea
 In a beautiful pea green boat,

$ ./num_pp text2
1 Some Verse
 The quick brown fox
jumps over the lazy dog.

2 The Owl and the Pussy-cat went to sea
 In a beautiful pea green boat,

Not the answer you're looking for? Browse other questions tagged or ask your own question.