使用 grep 和 awk 将数据从 .srt 传输到 .csv/xls

Pen*_*nny 2 csv shell awk grep srt

我有一个有趣的项目要做!我正在考虑将 srt 文件转换为 csv/xls 文件。

srt 文件如下所示:

1
00:00:00,104 --> 00:00:02,669
Hi, I'm shell-scripting.

2
00:00:02,982 --> 00:00:04,965
I'm not sure if it would work,
but I'll try it!

3
00:00:05,085 --> 00:00:07,321
There must be a way to do it!
Run Code Online (Sandbox Code Playgroud)

虽然我想将其输出到 csv 文件中,如下所示:

"1","00:00:00,104","00:00:02,669","Hi, I'm shell-scripting."   
"2","00:00:02,982","00:00:04,965","I'm not sure if it would work"
,,,"but I'll try it!"
"3","00:00:05,085","00:00:07,321","There must be a way to do it!"
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,每个字幕占据两行。我的想法是使用grep将srt数据放入xls中,然后使用awk格式化xls文件。

你们有什么感想?我该怎么写呢?我试过

$grep filename.srt > filename.xls
Run Code Online (Sandbox Code Playgroud)

似乎所有数据,包括时间代码和字幕单词最终都在 xls 文件的 A 列中...但我希望这些单词位于 B 列中...awk 如何帮助格式化?

先感谢您!:)

Ed *_*ton 5

$ cat tst.awk
BEGIN { RS=""; FS="\n"; OFS=","; q="\""; s=q OFS q }
{
    split($2,a,/ .* /)
    print q $1 s a[1] s a[2] s $3 q
    for (i=4;i<=NF;i++) {
        print "", "", "", q $i q
    }
}

$ awk -f tst.awk file
"1","00:00:00,104","00:00:02,669","Hi, I'm shell-scripting."
"2","00:00:02,982","00:00:04,965","I'm not sure if it would work,"
,,,"but I'll try it!"
"3","00:00:05,085","00:00:07,321","There must be a way to do it!"
Run Code Online (Sandbox Code Playgroud)