Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6038053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:09:46+00:00 2026-05-23T06:09:46+00:00

I’m trying to fix a subtitles (.srt) text file that has some incorrect data

  • 0

I’m trying to fix a subtitles (.srt) text file that has some incorrect data with a one-line ruby script. The file looks like this:

53
00:03:52,835 --> 00:03:54,835
Boss?... BOSS?!

54
00:03:54,845 --> 00:03:56,990


55
00:0 --> 00:03:58,490
Go!

I want the 55 stanza to look like this:

55
00:03:56,490 --> 00:03:58,490
Go!

Where the first time stamp is taken from the second but with 2 seconds subtracted.

Here is my attempt, which is not working:

ruby -pi.bak -e 'gsub(/(\d{2}):(\d) --> (\d{2}):(\d{2}):(\d{2}),(\d{3})/, "#{$3}:#{$4}:#{$5},#{$6} --> #{$3}:#{$4}:#{$5.to_i - 2},#{$6}")' *.srt

EDIT

So, as pointed out by the responders, ruby 1.9.2 does not support access of regex captures via the $1, $2, etc. syntax.

The fix I ended up going with was switching back to ruby 1.8.x, and using gsub with a block as @mu suggested, and used the Time.utc / strftime magic suggested by @jonas.

Here is the final solution (on my system /usr/bin/ruby is 1.8.6):

/usr/bin/ruby -pi.bak -e 'gsub(/(\d{2}):(\d) --> (\d{2}):(\d{2}):(\d{2}),(\d{3})/) {"#{(Time.utc(1970,1,1, $3,$4,$5) - 2).strftime("%H:%M:%S")},#{$6} --> #{$3}:#{$4}:#{$5},#{$6}"}' *.srt

I am now watching my movie with correctly formatted subtitles. Thanks guys 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:09:47+00:00Added an answer on May 23, 2026 at 6:09 am

    You almost have it but you want to use the block form of gsub rather than the two argument form and I think you have your subtraction on the wrong side of --->:

    ruby -pi.bak -e '$_.gsub(/(\d{2}):(\d) --> (\d{2}):(\d{2}):(\d{2}),(\d{3})/) { "#{$3}:#{$4}:#{$5.to_i - 2},#{$6} --> #{$3}:#{$4}:#{$5},#{$6}" }' *.srt
    

    Ruby 1.8 doesn’t need the $_ with the block form of gsub but 1.9 does. The global $1, $2, … aren’t what you think they are except in block form of gsub:

    If replacement is a String it will be substituted for the matched text. […] However, within replacement the special match variables, such as &$, will not refer to the current match.

    In the block form, the current match string is passed in as a parameter, and variables such as $1, $2, $`, $&, and $’ will be set appropriately.

    Jonas Elfström is right in the comments about subtracting 2 from the seconds in “00:04:00” making a mess. So you might want to use one of the time classes to handle your subtraction. Something like this:

    (Time.utc(1970,1,1, $3,$4,$5) - 2).strftime('%H:%M:%S')
    

    in place of your #{$3}:#{$4}:#{$5.to_i - 2},#{$6} should do the trick. Time.utc wants to work with a full date-time rather than just a time so using the Unix epoch (1970-01-01) is a bit of a hack to get around that. Of course, if you try to subtract 2s from 00:00:00 you’ll run into some problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have some data like this: 1 2 3 4 5 9 2 6
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.