srtファイルから改行と段落区切り文字を削除する

srtファイルから改行と段落区切り文字を削除する

私はこのスクリプトを使用して字幕からタイムスタンプを削除します。

awk '/-->/{for(i=1;i<d;i++){print a[i]};delete a;d=0;next}{a[++d]=$0}
    END{for(i in a)print a[i]}' xxxxx.srt > xxx.txt

その後、結果を改行と段落が削除されたWebページに貼り付けました。段落は1つだけで、区切り文字の代わりにスペースがあります。行ったところ: https://www.textfixer.com/tools/remove-line-breaks.php

これらすべてのタスクを1つのコマンドにまとめるソリューションを探していましたが、これを行う方法が見つかりませんでした。私はawkの代替案があることを知っています。 Mac端末でこれを簡単に行うことができるすべてが私に適しています!

助けてください?

以下は、フォーマットしたいが機能しない字幕の例です。一部の字幕が動作するのを見たことがありますが…おかしいですね。

字幕ファイル

予想出力:

Welcome to our program! This month’s theme is “Are You Paying Attention?” Strained relationships, illnesses, careers, entertainment —we’ll learn how to stay focused on Jehovah despite these potential distractions. We’ll see how our ministry is more effective when we focus on reaching the hearts of people. And our new song was written especially for you young adults to help you keep your eyes on the prize of life.

しかし、これは私があなたのスクリプトから得たものです。

    Welcome to our program!
 
 2
 00:00:06,089 --> 00:00:08,624
 This month’s theme is
 
 3
 00:00:08,625 --> 00:00:11,126
 “Are You Paying Attention?”
 
 4
 00:00:11,127 --> 00:00:13,595
 Strained relationships,
 
 5
 00:00:13,596 --> 00:00:16,131
 illnesses,
 

ベストアンサー1

awk「ショートモード」で使用:

awk -v RS= '{
    for (i=5;i<=NF;i++){
      printf "%s%s", (sep ? " " : ""), $i
      sep=1
    }
  }
  END{ print "" }
' file.srt > file.txt

これはレコード区切り文字を空の文字列に設定し、レコードは空行で区切られます。各レコードの最初の4つのフィールドはスキップされます(フィールド1は行番号、フィールド2〜4は表示時間)、最初のフィールドを除くすべてのフィールドはプレフィックススペースで印刷されます。

最後に改行文字が印刷されます。

入力ファイル:

1
00:00:06,453 --> 00:00:10,579
When one chooses to walk
the Way of the Mandalore,

2
00:00:10,581 --> 00:00:14,095
you are both hunter and prey.

3
00:00:17,935 --> 00:00:20,076
There is one job.

4
00:00:20,078 --> 00:00:21,945
Underworld?

5
00:00:21,947 --> 00:00:26,118
How uncharacteristic of
one of your reputation.

出力:

When one chooses to walk the Way of the Mandalore, you are both hunter and prey. There is one job. Underworld? How uncharacteristic of one of your reputation.

おすすめ記事