web-dev-qa-db-ja.com

「cat」コマンドを使用して段落に番号を付ける

現在、catコマンドを使用して、実行中のプロジェクトのテキストファイルを自動的に番号付けされた段落として表示する方法を探していますが、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. 

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.

これは私が簡単に見つけられると正直に思ったものですが、それを行う方法についての答えがある単一のWebサイトを見つけることができませんでした。 (catコマンドのバリエーションでなければならないことに注意してください。)

5
Campers

段落が実際の例のような行であり、catのみを使用する必要がある場合、-b(空でない行の番号)が必要ですか?

cat -b file

次のようになります。

     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 file > file2

本当に必要な場合は、数字の後にドットを追加できますが、catを助けるために別のコマンドを使用することなく、afaikではなく、sedのようになります。それらで始まる(cat -bインデント)、同じパターンに加えて.を追加して1.2.などを作成します(これは@terdonによって非常に速く提案されたので、自分で作ってクレジットを取る時間がある

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

あなたの例では、各段落は実際にはただの1行です。それが段落に形成される唯一の方法は、それを表示するために使用されているアプリケーションでテキストを折り返すことです。

catを使用して、ファイル内のすべての空でない行に番号を付けることができます。

cat -b file

これを別のファイルに送信する場合は、リダイレクトを使用します。

cat -b file > newfile

manコマンドは、他のコマンドの使用法を学ぶのに非常に役立ちます。たとえば、man catは次のようになります。

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

「段落」で空行で区切られた行のブロックを意味する場合、単純なawkコマンドで番号を追加できます。

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

出力の空白行を保持するには、次のようにORS変数を\n\nに設定できます。

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

出力を新しいファイルに保存する場合は、次のようなリダイレクトを使用できます。

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

「段落」はまだ段落ではなく、単なる長い行です(他の人が述べているように)

行に番号を付けてから段落にする必要があります。これにはfoldを使用できます。

cat -b file | fold -sw 80

これは空でない行に番号を付け、それを80文字(または列)で幅を保持する折り畳みにパイプし、スペースで行を分割します。

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.

ネコ

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

折る

   -s, --spaces
          break at spaces

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

「段落」コマンドはありません。 cat -bは使用したいものです。

これが今年の特定の管理された評価タスクであると仮定します;)行と段落番号間の混乱はおそらく、pico/nanoを使用してテキストファイルを作成すると、書き込み中にEnterキーを押して段落のようにしたという事実に起因しますしたがって、各「段落」は1行のみです。

Wordラップがあるデスクトップ環境のテストエディターでファイルを作成してみてください。 cat -b出力が期待どおりに表示されます。コマンドラインエディターの段落は、Wordで折り返されない非常に長い1行のテキストです。

0
DoctorOozy