空行の後ろの改行を削除

空行の後ろの改行を削除

データ

4. Alendronic acid
A. Antiosteoporotic agent. 
B. Inhibit osteoclast formation and function by inhibiting FPPS enzyme, so increase bone mass. 
C. Osteoporosis in combination with vitamin D. 

5. Aminophylline
A. Methylxanthine. Less potent and shorter-acting bronchodilator than Theophylline. 
B. Phosphodiesterase (PDE) inhibitor, so increase cAMP so affecting calcium so relaxes respiratory SM and dilates bronchi/bronchioles. 
C. Last option of asthma attack, COPD, Reversible airways obstruction. 

私が望むもの(後述の疑似コードに示されているように、後で空白行はありません)

4. Alendronic acid
A. Antiosteoporotic agent. B. Inhibit osteoclast formation and function by inhibiting FPPS enzyme, so increase bone mass. C. Osteoporosis in combination with vitamin D. 

5. Aminophylline
A. Methylxanthine. Less potent and shorter-acting bronchodilator than Theophylline. B. Phosphodiesterase (PDE) inhibitor, so increase cAMP so affecting calcium so relaxes respiratory SM and dilates bronchi/bronchioles. C. Last option of asthma attack, COPD, Reversible airways obstruction. 

私の試みはもともと空白行をすべて削除するというアイデアに基づいていましたが、gsed -n "s/^$//;t;p;"今は不可能です。

擬似コード

  • (空白行ではなく)すべての改行を削除します。tr '\n' ' '(これはすべてライナーですが、問題は空白の行も必要だからです!)
  • すべてを変えるㅏ。渡す\nA。渡すsed 's#A.#\nA.#'
  • 空白行をすべて削除します。gsed -n "s/^$//;t;p;"

擬似コードのまとめ

cat                                 \
     10.6.2015.tex                  \
                                    \
| tr '\n' ' '                       \
                                    \
| sed 's#A.#\nA.#'                  \
                                    \
| gsed -n "s/^$//;t;p;"             \
                                    \
> 10.6.2015_quizlet.tex

しかし、これは最初の行の論理エラーのために間違っています。

Perl/Sed/trで空白行の後の改行を削除する方法は?

答え1

Perl または awk を使用してデータを一度に 1 つずつ読み込み、最初の改行文字を除くすべての項目を削除します。

perl -00 -pe '$\="\n\n"; s/\n/\0/; s/\n//g; s/\0/\n/' file

コメントしました。

perl -00 -pe '   # each record is separated by blank lines (-00)
                 # read the file a record at a time and auto-print (-p)
    $\="\n\n";   # auto-append 2 newlines to each record
    s/\n/\0/;    # turn the first newline into a null byte
    s/\n//g;     # remove all other newlines
    s/\0/\n/     # restore the first newline
' file

同様に

awk -v RS= -F'\n' '{print $1; for (i=2; i<=NF; i++) printf "%s", $i; print ""; print ""}' file

答え2

あなたはそれを使用することができます:

sed '/[0-9]\./{n;:l;N;/\n$/!s/\n/ /;t l}' file

すると、以下が出力されます。

4. Alendronic acid
A. Antiosteoporotic agent.  B. Inhibit osteoclast formation and function by inhibiting FPPS enzyme, so increase bone mass.  C. Osteoporosis in combination with vitamin D. 

5. Aminophylline
A. Methylxanthine. Less potent and shorter-acting bronchodilator than Theophylline.  B. Phosphodiesterase (PDE) inhibitor, so increase cAMP so affecting calcium so relaxes respiratory SM and dilates bronchi/bronchioles.  C. Last option of asthma attack, COPD, Reversible airways obstruction. 

説明する

数字とピリオドのある行を一致させます/[0-9]\./。次に、次の行に移動するコードブロックを入力しますn。ループを開始し、:l次の行をに追加しNて改行文字をに置き換えますs/\n/ /。条件によって選択された空行に達すると、ループは終了します/\n$/!

答え3

以下は、awk入力と出力のフィールドとレコードの区切り文字を適切に定義して問題を解決するソリューションです。したがって、有効なコマンド($1=$1 FS)は非常に簡単です。

awk '
  BEGIN { RS="" ; FS="\n" ; OFS="" ; ORS="\n\n" }
  $1=$1 FS
'

説明する:

RS=""- 空行で区切られたデータブロックを1つのレコードとして扱います。

FS="\n"- ブロックの各行を独自のアドレス指定可能フィールドとして定義する

OFS=""- スペースで終わるデータのため、フィールド区切り文字を出力する必要はありません。

ORS="\n\n"- 新しいブロック(入力データ)を空白行に分割します。

$1=$1 FS- 最初のフィールド(つまり、最初の行)は改行文字でブロックの残りの行と区別されます。なぜなら、割り当てはawk変更されたレコード(ブロック)の真の条件であるために印刷されます。

答え4

sed -n '/^[0-9]/!H;//x;$x;s/\n\([^A]\)/ \1/gp' <infile >outfile

問題を解決しているようです。

  1. /^[0-9]/!H
    • 行が数字で始まらない場合は、新しい行の後の古いスペース!に追加されます。H\n
  2. //x;$x
    • 数字で始まる場合、またはこれが$最後の行である場合は、xパターンとh前のスペースを変更してください。
  3. s/\n\([^A]\)/ \1/gp
    • 1つ以上のewlineシーケンス\nの後に非文字が続く場合文字はパターン空間内で検索され、これらのシーケンスの目盛りでグローバルにg置き換えるs///ことができます。\n<スペース>そしてp結果を印刷してみてください。
    • \newlineが見つかる唯一の時間は、eが変更された直後ですx。したがって、数字で始まる行または最後の行でのみ見つけることができます。
    • \n区切り線が次のようになるため、先行数字は先行するエッジを保持します。最後eがx変更されると、パターン空間の文字が後に\([^A]\)続くため、s///スペースに置き換えられません。

それはすべてです。

出力:

4. Alendronic acid
A. Antiosteoporotic agent.  B. Inhibit osteoclast formation and function by inhibiting FPPS enzyme, so increase bone mass.  C. Osteoporosis in combination with vitamin D. 

5. Aminophylline
A. Methylxanthine. Less potent and shorter-acting bronchodilator than Theophylline.  B. Phosphodiesterase (PDE) inhibitor, so increase cAMP so affecting calcium so relaxes respiratory SM and dilates bronchi/bronchioles.  C. Last option of asthma attack, COPD, Reversible airways obstruction. 

関連情報