I got an interesting project to do! I'm thinking about converting an srt file into a csv/xls file.
a srt file would look like this:
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!
while I want to output it into a csv file like this:
"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!"
So as you can see, each subtitle takes up two rows. My thinking would be using grep to put the srt data into the xls, and then use awk to format the xls file.
What do you guys think? How am I suppose to write it? I tried
$grep filename.srt > filename.xls
It seems that all the data including the time codes and the subtitle words ended up all in column A of the xls file...but I want the words to be in column B...How would awk be able to help with the formatting?
Thank you in advance! :)
My other answer was half awk and half Perl, but, given that
awk
can't write Excel spreadsheets whereasPerl
can, it seems daft to require you to master bothawk
andPerl
whenPerl
is perfectly capable of doing it all on its own... so here goes in Perl:Save the above on a file called
srt2xls
and then make it executable with the command:Then you can run it with
and it will give you this spreadsheet called
result.xlsx
Since you want to convert the srt into csv. below is awk command
detail veiw of awk
take the output.csv on windows platform and then open with microsoft excel and save it as .xls extension.
I think something like this should do it quite nicely:
The
-v RS=
sets the Record Separator to blank lines. The-F'\n'
sets the Field Separator to new lines.The
sub()
replaces the "-->" with a pipe symbol (|
).The first three fields are then printed separated by pipes, and then there is a little loop to print the remaining lines of description, inset by three pipe symbols to make them line up.
Output
As I am feeling like having some more fun with Perl and Excel, I took the above output and parsed it in Perl and wrote a real Excel XLSX file. Of course, there is no real need to use
awk
andPerl
so ideally one would re-cast theawk
and integrate it into thePerl
since the latter can write Excel files while the former cannot. Anyway here is the Perl.Output