Numerando parágrafos com o comando 'cat'

5

Atualmente, estou tentando encontrar uma maneira de usar o comando cat para mostrar um arquivo de texto como parágrafos numerados automaticamente para um projeto que estou fazendo, mas não consegui encontrar um único comando.

Exemplo:

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.

Depois que o comando for inserido:

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.

Isso é algo que honestamente achei que encontraria facilmente, mas não consegui encontrar um único site com uma resposta sobre como fazê-lo. (Lembre-se que tem que ser uma variação do comando cat .)

    
por Campers 08.09.2016 / 15:19

5 respostas

13

Se o parágrafo é realmente uma linha como o seu exemplo, e você tem que usar apenas cat , então certamente você quer -b (número de linhas não vazias)?

cat -b file

parece:

     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.

Para salvar isso em um arquivo em vez de imprimir no terminal:

cat -b file > file2

Caso você realmente precise deles, adicione os pontos após os números, mas não, afaik, sem recorrer a outro comando para ajudar cat , como sed , que substitui o espaço em branco e os números em linhas que começam com eles (desde cat -b indent) com o mesmo padrão mais um . para fazer 1. 2. etc (isso foi sugerido por @terdon tão incrivelmente rápido que eu não tive tempo para fazê-lo eu mesmo e recebo o crédito)

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

    
por Zanna 08.09.2016 / 15:43
7

No seu exemplo, cada parágrafo é, na verdade, apenas uma linha única. A única maneira que seria formada em um parágrafo seria por quebra de texto em qualquer aplicativo que estivesse sendo usado para exibi-lo.

Você pode numerar todas as linhas não vazias em um arquivo, usando cat com:

cat -b file

Se você quiser enviar isso para outro arquivo, use o redirecionamento:

cat -b file > newfile

O comando man é realmente útil para aprender sobre os usos de outros comandos, por exemplo 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.
    
por Arronical 08.09.2016 / 15:44
6

Se por "parágrafos" você quer dizer blocos de linhas separadas por linhas vazias, você poderia adicionar a numeração com o comando awk simples:

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

Para preservar as linhas em branco na saída, você pode definir a variável ORS como \n\n da seguinte forma:

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

Se você quiser salvar a saída em um novo arquivo, poderá usar um redirecionamento como este:

awk -v RS= '{print ++i, $0}' file > newfile
    
por user000001 08.09.2016 / 15:32
4

Os "parágrafos" não são parágrafos ainda, apenas linhas longas (como outros já notaram)

Precisamos numerar as linhas e transformá-las em parágrafos Você pode usar o fold para isso.

cat -b file | fold -sw 80

Isto numera linhas não vazias, canaliza-as para a dobra, o que mantém a largura em 80 caracteres (ou colunas) e quebra a linha nos espaços.

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

dobra

   -s, --spaces
          break at spaces

   -w, --width=WIDTH
          use WIDTH columns instead of 80
    
por Miati 08.09.2016 / 23:08
0

Não conheço nenhum comando "paragraph". cat -b é o que você deseja usar.

Supondo que isso seja para uma tarefa de avaliação controlada específica este ano;) A confusão entre linhas e numeração de parágrafos provavelmente deriva do fato de que se você fizer o arquivo de texto usando pico / nano você terá parecido com um parágrafo pressionando enter enquanto escreve o conteúdo, portanto, cada um dos seus "parágrafos" é apenas uma linha.

Tente criar o arquivo em um editor de teste no ambiente de área de trabalho que tenha quebra de linha. Você verá a saída cat -b da forma esperada. Um parágrafo em um editor de linha de comando é apenas uma linha muito longa de texto que não envolve quebra de linha.

    
por DoctorOozy 13.01.2017 / 15:56