sedに一致する前の行と一致するいくつかの行を追加するには?

sedに一致する前の行と一致するいくつかの行を追加するには?

たとえば、転送は次のようになります。

00:00:10.730
this presentation is delivered by the

00:00:13.230
Stanford center for professional

00:00:14.610
development okay so let's get started

00:00:25.500
with today's material so um welcome back

00:00:32.399
to the second lecture what I want to do

到着

00:00:10.730 --> 00:00:13.230
this presentation is delivered by the

00:00:13.230 --> 00:00:14.610
Stanford center for professional

00:00:14.610 --> 00:00:25.500
development okay so let's get started

00:00:25.500 --> 00:00:32.399
with today's material so um welcome back

00:00:32.399
to the second lecture what I want to do

ベストアンサー1

コードを明確にするには、次のようにしますGNU sed

sed -nE '

   /^([0-9][0-9]:){2}[0-9]+[.][0-9]+/!{p;d;}

   h;:a
      $bb;n;H
   /^([0-9][0-9]:){2}[0-9]+[.][0-9]+/!ba

   :b
   x
   y/\n_/_\n/
   s/^([^_]*)_(.*)_([^_]*)$/\1 ---> \3_\2/
   y/\n_/_\n/

   p;g;$!s/^/\n/;D

' yourfile

結果

00:00:10.730 ---> 00:00:13.230
this presentation is delivered by the

00:00:13.230 ---> 00:00:14.610
Stanford center for professional

00:00:14.610 ---> 00:00:25.500
development okay so let's get started

00:00:25.500 ---> 00:00:32.399
with today's material so um welcome back

00:00:32.399
to the second lecture what I want to do

説明する

  • 数字から次の数字まで行の範囲を維持します。
  • その後、範囲の終わりから最後の部分が前方に押され、範囲が印刷され、パターン空間がクリアされ、範囲の終わりで埋められ、そのパターン空間の値を使用して制御が最上位sedに渡される。現在の範囲の終わりから再開するコードの次の数字まで、またはeofに達するまで繰り返します。

おすすめ記事