空行の後ろの改行を削除

空行の後ろの改行を削除

データ

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

おすすめ記事