sedまたはawkを使用して段落内の改行を削除する方法

sedまたはawkを使用して段落内の改行を削除する方法

段落から改行を削除する方法を知りたいです。この本そしてKindleで利用可能なその他のコンテンツ。望ましい効果は、空白行で区切られた各ブロックを連続したテキスト行に置き換えることです。私は一連の複雑なvim代替コマンドを使ってこの本の作業を完了しましたが、将来的には作業を完了するより良い方法を見つけようとしています。

私はこの目的に使用できるvim、perl、sed、またはawkスクリプトを取得しますが、あなたのアイデアで開いています。

解決策が見つかりましたが、将来のGoogleスタッフのためのサンプル入出力は次のとおりです。

改行文字を入力してください:

Letter 1

_To Mrs. Saville, England._


St. Petersburgh, Dec. 11th, 17—.


You will rejoice to hear that no disaster has accompanied the
commencement of an enterprise which you have regarded with such evil
forebodings. I arrived here yesterday, and my first task is to assure
my dear sister of my welfare and increasing confidence in the success
of my undertaking.

I am already far north of London, and as I walk in the streets of
Petersburgh, I feel a cold northern breeze play upon my cheeks, which
braces my nerves and fills me with delight. Do you understand this
feeling? This breeze, which has travelled from the regions towards
which I am advancing, gives me a foretaste of those icy climes.
Inspirited by this wind of promise, my daydreams become more fervent
and vivid. I try in vain to be persuaded that the pole is the seat of
frost and desolation; it ever presents itself to my imagination as the
region of beauty and delight. There, Margaret, the sun is for ever
visible, its broad disk just skirting the horizon and diffusing a...

段落から改行なしで出力:

_To Mrs. Saville, England._


St. Petersburgh, Dec. 11th, 17--.


You will rejoice to hear that no disaster has accompanied the commencement of an enterprise which you have regarded with such evil forebodings. I arrived here yesterday; and my first task is to assure my dear sister of my welfare, and increasing confidence in the success of my undertaking.

I am already far north of London; and as I walk in the streets of Petersburgh, I feel a cold northern breeze play upon my cheeks, which braces my nerves, and fills me with delight. Do you understand this feeling? This breeze, which has travelled from the regions towards which I am advancing, gives me a foretaste of those icy climes. Inspirited by this wind of promise, my day dreams become more fervent and vivid. I try in vain to be persuaded that the pole is the seat of frost and desolation; it ever presents itself to my imagination as the region of beauty and delight. There, Margaret, the sun is for ever visible; its broad disk just skirting the horizon, and diffusing a... 

これで元々好奇心として使用したvimコマンドは次のようになります。

ggVG:norm A<space>   -- adds a space to the end of each line
:%s/\v^\s*$/<++>     -- swaps all blank lines with a unique temporary string
ggVGgJ               -- joins all lines without adding a space
:%s/<++>/\r\r/g      -- replaces all occurrences of my unique string with two newline characters 

ベストアンサー1

段落がすでに2つ以上の改行で区切られていて、各段落内の改行のみを削除したい場合(またはより良い方法は改行を空白に置き換えること)、次の手順を実行します。

perl -00 -lpe 's/\n/ /g' pg42324.txt > pg42324-new.txt
  • -00Perlに一度に1つの段落を入力して読み取って処理するように指示します(段落の境界は2つ以上の改行です)。

  • -lPerlの行末の自動処理(またはこの場合は段落の終わり)をオンにします。

  • -pPerlを次のように実行しますsed。つまり、スクリプトを変更して入力を読み、印刷します。

  • -ePerlに、次の引数が実行するスクリプトであることを伝えます。

これらのオプションの詳細を確認してくださいman perlrun

または内部編集の場合(元の.bak拡張子でバックアップされています):

perl -i.bak -00 -lpe 's/\n/ /g' pg42324.txt 

段落内の行に先行または末尾のスペースがある場合は、複数のスペースを単一のスペースに置き換える必要があります。; s/ +/ /gPerlスクリプトに追加します。

perl -00 -lpe 's/\n/ /g; s/  +/ /g' 

しかし、私の考えでは、ファイル全体をマークダウンとして処理し(太字、イタリック体、章のタイトルなどにマークダウン形式を追加することさえ可能です)、次のものを使用する方が良いでしょう。読書または、マークダウンからepubに変換してください。結局、Markdownはオプションの書式文字を含むプレーンテキストです。例えば

pandoc pg42324.txt -o pg42324.epub

最小限の編集は、ファイル(または何でも)を開き、vim各段落の間に空白行があることを確認することです。

さて、pandocを使って電子ブックを作成するテキストやMarkdownファイルから.epubブックを作成する方法についての短いが便利な一般的な紹介です。


または、テキストのみのバージョンではなく、.epubまたは.mobiバージョンの書籍をダウンロードすることをお勧めします。 Project Gutenbergは様々な書籍を提供しています。

Mary ShelleyのFrankensteinをさまざまな形式でダウンロードするためのリンクがあります。

https://www.gutenberg.org/ebooks/42324

おすすめ記事