web-dev-qa-db-fra.com

Numérotation des paragraphes avec la commande 'cat'

J'essaie actuellement de trouver un moyen d'utiliser la commande cat pour afficher un fichier texte sous forme de paragraphes numérotés automatiquement pour un projet en cours, mais je n'ai pas réussi à trouver une seule commande.

Exemple:

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.

Puis, une fois la commande entrée:

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.

Honnêtement, je pensais que c’était quelque chose que je trouverais facilement, mais je n’ai pas été capable de trouver un seul site Web avec une réponse sur la façon de le faire. (N'oubliez pas qu'il doit s'agir d'une variante de la commande cat.)

5
Campers

Si le paragraphe est en réalité une ligne comme celle de votre exemple et que vous ne devez utiliser que catname__, alors vous voulez sûrement -b (nombre de lignes non vides)?

cat -b file

ressemble à:

     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.

Pour enregistrer ceci dans un fichier au lieu d’imprimer dans le terminal:

cat -b file > file2

Au cas où vous en auriez vraiment besoin, vous pourriez ajouter les points après vos numéros, mais pas tant que vous le voudriez, sans recourir à une autre commande pour aider catname__, comme sedname__, qui remplace ici les espaces et les nombres commençant par ceux-ci (depuis cat -b ) avec le même motif plus un . pour faire 1.2. etc etc (cela a été suggéré par @terdon tellement rapidement que je n’ai pas eu le temps de le faire moi-même et de prendre le crédit)

cat -b file | sed -r 's/^\s+[0-9]+/&./' > file2
13
Zanna

Dans votre exemple, chaque paragraphe ne contient en réalité qu'une seule ligne. Le seul moyen de le transformer en paragraphe consiste à insérer du texte dans n'importe quelle application utilisée pour l'afficher.

Vous pouvez numéroter toutes les lignes non vides d'un fichier, en utilisant cat avec:

cat -b file

Si vous voulez envoyer ceci dans un autre fichier, utilisez la redirection:

cat -b file > newfile

La commande man est très utile pour en savoir plus sur les utilisations d’autres commandes. Par exemple, man cat donne:

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

Si par "paragraphes" vous voulez dire des blocs de lignes séparés par des lignes vides, vous pouvez ajouter la numérotation avec la commande awk simple:

awk -v RS= '{print ++i, $0}' file

Pour conserver les lignes vides dans la sortie, vous pouvez définir la variable ORS sur \n\n comme ceci:

awk -v RS= -vORS='\n\n' '{print ++i, $0}' file

Si vous souhaitez enregistrer la sortie dans un nouveau fichier, vous pouvez utiliser une redirection comme ceci:

awk -v RS= '{print ++i, $0}' file > newfile
6
user000001

Les "paragraphes" ne sont pas encore des paragraphes, mais de longues lignes (comme d'autres l'ont noté)

Nous devons numéroter les lignes, puis les transformer en paragraphes. Vous pouvez utiliser fold pour cela.

cat -b file | fold -sw 80

Cela numérote les lignes non vides, les plie en deux, ce qui maintient la largeur à 80 caractères (ou colonnes) et coupe les lignes sur les espaces.

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.

chat

   -b, --number-nonblank
          number nonempty output lines, overrides -n

plier

   -s, --spaces
          break at spaces

   -w, --width=WIDTH
          use WIDTH columns instead of 80
4
Miati

Je ne connais aucune commande de "paragraphe". cat -b est ce que vous voulez utiliser.

En supposant qu'il s'agisse d'une tâche d'évaluation contrôlée spécifique cette année;) La confusion entre les lignes et la numérotation des paragraphes provient probablement du fait que si vous créez le fichier texte à l'aide de pico/nano, vous le faites ressembler à un paragraphe en appuyant sur Entrée tout en écrivant le contenu, donc chacun de vos "paragraphes" est une seule ligne.

Essayez de créer le fichier dans un éditeur de test de l’environnement de bureau doté d’un retour à la ligne. Vous verrez que la sortie cat -b se présente comme prévu. Un paragraphe dans un éditeur de ligne de commande est juste une très longue ligne de texte qui ne recouvre pas Word.

0
DoctorOozy