3

ドキュメントの新しいバージョンのそれぞれに 1 行に 1 つの文が含まれるように、一般的なプレーン テキストからドキュメントの新しいバージョンを作成したいと考えています。つまり、テキストの各行には、 で終わる一連の文字列が含まれてい.ます。そのためのサンプル スクリプトをいくつか教えてください。

 In the beginning God created the heavens and the earth.
 Now the earth was formless and empty.  Darkness was on the surface
 of the deep.  God's Spirit was hovering over the surface
 of the waters.

の中へ

 In the beginning God created the heavens and the earth.
 Now the earth was formless and empty.
 Darkness was on the surface of the deep.
 God's Spirit was hovering over the surface of the waters.
4

3 に答える 3

3

を使用した片道perl

perl -pe 's/\n\Z/ /; s/(\.)\s*/$1\n/g' infile

出力:

In the beginning God created the heavens and the earth.
Now the earth was formless and empty.
Darkness was on the surface of the deep.
God's Spirit was hovering over the surface of the waters.
于 2012-04-30T09:04:21.293 に答える
3
awk 'BEGIN {RS = "[.] *"; ORS = ".\n"} {gsub(" *\n *", " "); if ($0 !~ /^ +$/) print}'

各ピリオドでテキストを区切り、その後にスペースがある場合はスペース ( RS) を付けます。

各行の出力の後にピリオドと改行 ( ORS) が続きます。

各改行と周囲のスペースをスペースに置き換えます ( gsub())。

行がスペースだけで構成されていない場合は、印刷します。

[[:blank:]]スペースだけでなくタブも使用する場合は、スペースの後にアスタリスクまたはプラス記号が続く (アスタリスクまたはプラス記号が続く) 場所を変更できます。

于 2012-04-30T01:52:27.843 に答える
2

まず、との組み合わせを試してtrくださいsed

$ cat input
They're selling postcards of the hanging. They're painting the passports brown. The beauty parlor is filled with sailors. The circus is in town.


$ cat input | tr '.' '\n' | sed 's/$/\./;s/[    ]*//'
They're selling postcards of the hanging.
They're painting the passports brown.
The beauty parlor is filled with sailors.
The circus is in town.
于 2012-04-29T21:02:47.400 に答える