Solved Keep first 4 and last N lines in a text

July 24, 2017 at 05:49:32
Specs: Windows 10
Hi everybody, I have a rather specific question. I need to modify a number of subtitles (.srt files) by deleting everything except the first 4 and last N lines. The first four shouldn't be a problem, the problem is that N is variable. However the last subtitles always start after a certain point. So, if I have:
.
.
.
284
00:23:51,450 --> 00:23:54,950
blah, blah, blah
blah, blah, blah

285
00:24:01,550 --> 00:24:04,550
blah, blah, blah

286
00:24:04,950 --> 00:24:08,450
blah, blah, blah
blah, blah, blah

I want to search for lines that start with "00:23:5" (so i can find 00:23:51,450 --> 00:23:54,950 as well as 00:23:52,500 --> 00:23:57,500), then copy everything from the previous line (the one with the sub number, in this case 284) to the end of the file. I'd like to do this to all the files in a folder, while keeping the original filename. Needless to say, there isn't a software able to do that, that's why i need your help.
Thank you.

message edited by moff90


See More: Keep first 4 and last N lines in a text

Reply ↓  Report •

#1
July 24, 2017 at 20:48:20
✔ Best Answer
Klunky,but worked in limited testing:

::==== begin script
@echo off & setlocal
set targ=%1
if not defined targ set targ=00:23:5
md temp||pause
echo using temp directory "temp" for ouput...
for %%z in (*.srt) do (
echo looking at: %%z for target string %targ%:
call :test "%%z"
)
goto :eof

:test
for /f "tokens=1 delims=[]" %%a in ('find /n "%targ%"^<%1^|^|echo [NOT]') do (
if %%a equ NOT goto :eof
rem filename line#
call :build %1 %%a
goto :eof
)

:build
set /a x=%2-2
for /f "tokens=1* delims=[]" %%b in ('find /n /v "" ^<%1') do (
>> temp\%1 echo,%%c
if %%b equ 4 (
more +%x% %1>>temp\%1
goto :eof
)
)
::------ end script

message edited by nbrane


Reply ↓  Report •

#2
July 25, 2017 at 01:56:46
Thank you, it works! There's only one little problem, it puts an "ECHO off" message between the first and the last part.

1
00:01:32,200 --> 00:01:37,100
blah, blah, blah
ECHO off.
284
00:23:51,450 --> 00:23:54,950
blah, blah, blah
blah, blah, blah

285
00:24:01,550 --> 00:24:04,550
blah, blah, blah

286
00:24:04,950 --> 00:24:08,450
blah, blah, blah
blah, blah, blah

Not really a big deal, i used this tool to fix that: http://findandreplace.codeplex.com/


Reply ↓  Report •

#3
July 25, 2017 at 19:19:37
Good!
this line made the "echo is off":
>> temp\%1 echo %%c
should have been:
>> temp\%1 echo.%%c
which outputs a blank line to the output-file when %%c is null.
Corrected this oversight in my first post to obtain closure.

Reply ↓  Report •

Related Solutions

#4
July 26, 2017 at 04:31:23
Great, thank you again!

Reply ↓  Report •

#5
July 26, 2017 at 15:25:18
If you can cheat and you know you want the first and last 3 subs, you can do something like this in PowerShell:
dir *.srt | % {
  $subs = gc $_ -Delimiter "`n`n" | select -First 1 -Last 3
  Out-File "$_.2.srt" utf8 -In (-join $subs) -Force
}

EDIT: If you can't cheat, you end up with something slightly longer:

dir *.srt | % {
  $subs = gc $_ -Delimiter "`n`n" 
  $doCap = $false
  $selSubs = -join $subs[0] + ($subs | ? { $doCap = $doCap -or $_ -like '*00:23:5*'; $doCap })
  Out-File "$_.2.srt" utf8 -In $selSubs -Force
}

How To Ask Questions The Smart Way

message edited by Razor2.3


Reply ↓  Report •

Ask Question