0

I want to search a text file for a string with this command

findstr silence_start:* test.txt

here is the out put

C:\Users\brett\Desktop\masterpuppets>findstr  ilence_start:* test.txt
[silencedetect @ 00000000042eb5c0] silence_start: -0.00260771
[silencedetect @ 00000000042eb5c0] silence_start: 310.019
[silencedetect @ 00000000042eb5c0] silence_start: 822.097
[silencedetect @ 00000000042eb5c0] silence_start: 1211.84
[silencedetect @ 00000000042eb5c0] silence_start: 1605.72
[silencedetect @ 00000000042eb5c0] silence_start: 2443.7
[silencedetect @ 00000000042eb5c0] silence_start: 2946.45
[silencedetect @ 00000000042eb5c0] silence_start: 2955.28
[silencedetect @ 00000000042eb5c0] silence_start: 3279.93

What i need are the numbers after silence_start: and i want to store them in an array. Ive never tried this in windows and are stumped can this be done from the command line of batch file? Any help would be great. Im attempting to slice up the mp3 at the silences...

Updated output and the content of the test file.. First the test file im getting the information from

ffmpeg version N-63968-g73d820e Copyright (c) 2000-2014 the FFmpeg developers
  built on Jun 15 2014 00:52:21 with gcc 4.8.3 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-decklink --enable-zlib
  libavutil      52. 89.100 / 52. 89.100
  libavcodec     55. 66.101 / 55. 66.101
  libavformat    55. 43.100 / 55. 43.100
  libavdevice    55. 13.101 / 55. 13.101
  libavfilter     4.  8.100 /  4.  8.100
  libswscale      2.  6.100 /  2.  6.100
  libswresample   0. 19.100 /  0. 19.100
  libpostproc    52.  3.100 / 52.  3.100
Input #0, mp3, from 'mp.mp3':
  Metadata:
    major_brand     : dash
    minor_version   : 0
    compatible_brands: iso6mp41
    encoder         : Lavf55.43.100
  Duration: 00:55:00.08, start: 0.025057, bitrate: 320 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 320 kb/s
Output #0, null, to 'pipe:':
  Metadata:
    major_brand     : dash
    minor_version   : 0
    compatible_brands: iso6mp41
    encoder         : Lavf55.43.100
    Stream #0:0: Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb/s
    Metadata:
      encoder         : Lavc55.66.101 pcm_s16le
Stream mapping:
  Stream #0:0 -> #0:0 (mp3 (native) -> pcm_s16le (pcm_s16le))
Press [q] to stop, [?] for help
[silencedetect @ 00000000042eb5c0] silence_start: -0.00260771
[silencedetect @ 00000000042eb5c0] silence_end: 0.601882 | silence_duration: 0.60449
size=N/A time=00:03:00.19 bitrate=N/A    
[silencedetect @ 00000000042eb5c0] silence_start: 310.019
[silencedetect @ 00000000042eb5c0] silence_end: 311.772 | silence_duration: 1.75388
size=N/A time=00:05:46.04 bitrate=N/A    
size=N/A time=00:08:28.29 bitrate=N/A    
size=N/A time=00:11:17.04 bitrate=N/A    
[silencedetect @ 00000000042eb5c0] silence_start: 822.097
[silencedetect @ 00000000042eb5c0] silence_end: 827.064 | silence_duration: 4.96694
size=N/A time=00:14:09.05 bitrate=N/A    
size=N/A time=00:16:46.36 bitrate=N/A    
size=N/A time=00:19:36.63 bitrate=N/A    
[silencedetect @ 00000000042eb5c0] silence_start: 1211.84
[silencedetect @ 00000000042eb5c0] silence_end: 1214.96 | silence_duration: 3.11224
size=N/A time=00:22:34.29 bitrate=N/A    
size=N/A time=00:25:28.00 bitrate=N/A    
[silencedetect @ 00000000042eb5c0] silence_start: 1605.72
[silencedetect @ 00000000042eb5c0] silence_end: 1610.21 | silence_duration: 4.49673
size=N/A time=00:28:29.95 bitrate=N/A    
size=N/A time=00:31:33.69 bitrate=N/A    
size=N/A time=00:34:28.03 bitrate=N/A    
size=N/A time=00:37:29.50 bitrate=N/A    
size=N/A time=00:40:09.74 bitrate=N/A    
[silencedetect @ 00000000042eb5c0] silence_start: 2443.7
[silencedetect @ 00000000042eb5c0] silence_end: 2445.32 | silence_duration: 1.62327
size=N/A time=00:43:07.09 bitrate=N/A    
size=N/A time=00:45:56.28 bitrate=N/A    
size=N/A time=00:48:43.67 bitrate=N/A    
[silencedetect @ 00000000042eb5c0] silence_start: 2946.45
[silencedetect @ 00000000042eb5c0] silence_end: 2953.51 | silence_duration: 7.05673
[silencedetect @ 00000000042eb5c0] silence_start: 2955.28
[silencedetect @ 00000000042eb5c0] silence_end: 2955.83 | silence_duration: 0.552245
size=N/A time=00:51:42.79 bitrate=N/A    
[silencedetect @ 00000000042eb5c0] silence_start: 3279.93
size=N/A time=00:54:45.44 bitrate=N/A    
size=N/A time=00:55:00.05 bitrate=N/A    

video:0kB audio:568485kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown

Next is my batch file with Michaels example

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=0
FOR /F "tokens=*" %%a IN ('findstr silence_start:* test.txt') DO (
    FOR /F "tokens=2 delims=:" %%b IN ("%%a") DO (
        SET number=%%b
        SET number=!number:~1!
        ECHO !number!
    )

)

running a test command from command line

C:\Users\brett\Desktop\masterpuppets>findstr silence_start: test.txt
[silencedetect @ 00000000042eb5c0] silence_start: -0.00260771
[silencedetect @ 00000000042eb5c0] silence_start: 310.019
[silencedetect @ 00000000042eb5c0] silence_start: 822.097
[silencedetect @ 00000000042eb5c0] silence_start: 1211.84
[silencedetect @ 00000000042eb5c0] silence_start: 1605.72
[silencedetect @ 00000000042eb5c0] silence_start: 2443.7
[silencedetect @ 00000000042eb5c0] silence_start: 2946.45
[silencedetect @ 00000000042eb5c0] silence_start: 2955.28
[silencedetect @ 00000000042eb5c0] silence_start: 3279.93

Output from mybat.bat

C:\Users\brett\Desktop\masterpuppets>mybat.bat
-0.00260771
3
5
4
2
8
3
2955.28
1
Brett
  • 376
  • 7
  • 24
  • I cannot reproduce your result. I copied the example text and the script verbatim into files, and when I run the batch script I get the correct result. – dbenham Jul 06 '15 at 12:05
  • Thanks for helping... im lost as to why its not working either – Brett Jul 06 '15 at 12:11

2 Answers2

1

To extract the numbers behind the : this will work:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=0
FOR /F "tokens=*" %%a IN ('findstr silence_start:* testinput.txt') DO (
    FOR /F "delims=: tokens=2" %%b IN ("%%a") DO (
        SET number=%%b
        SET number=!number:~1!
        ECHO !number!
    )
)

However, there are no arrays in CMD / batch. There is a workaround to realise some data structures described here but it's still ugly. Instead I would store the extracted values in a temporary text file.

Community
  • 1
  • 1
MichaelS
  • 5,941
  • 6
  • 31
  • 46
  • Hi sorry I'm back,, I'm still having a slight issue even thou I'm closer, after trying MichealS solution I'm still not quite getting the result need, here is the out put I'm getting I'm really confused its coming up with numbers that are not there? C:\Users\brett\Desktop\masterpuppets>mybat.bat -0.00260771 3 5 4 2 8 3 2955.28 1 – Brett Jul 06 '15 at 09:11
  • Please adit your main post and add the new incorrect output. – MichaelS Jul 06 '15 at 10:04
  • Why two FOR /F? Your first loop could do the parsing job of your second loop. – dbenham Jul 06 '15 at 11:17
  • @Brett - More important than the ouptut is the exact code you are using. Michael's code is producing the correct numbers for me. – dbenham Jul 06 '15 at 11:18
  • Hi yeh im so confused im totally lost – Brett Jul 06 '15 at 11:22
  • @dbenham You are right, you can combine both loops. I just thought that this way it yould be easier to understand for Brett. – MichaelS Jul 06 '15 at 11:25
  • Im not sure where to go from here – Brett Jul 06 '15 at 11:25
  • Once again: please adit your main post and add the new incorrect output. To YOUR post, not to MINE!!! – MichaelS Jul 06 '15 at 11:25
  • I have ajust my code to show whats happening @MichaelS I hope – Brett Jul 06 '15 at 11:40
0

Your FINDSTR search is not what you think - it is a regular expression, so it looks for "silent_start" followed by 0 or more ":"

I modified your search to be more precise, using a string literal search instead.

Below is a pure batch solution. I pipe the FINDSTR result to a second FINDSTR to prefix each line with the "line number" to be used as an "array" counter.

The second FOR /F loop is used to strip of the leading space.

@echo off
setlocal enableDelayedExpansion
:: Load the "array"
for /f "tokens=1,3 delims=:" %%A in (
  'findstr /c:"] silence_start: " test.txt ^| findstr /n "^"'
) do for /f "tokens=*" %%B in ("%%B") do (
  set "num.%%A=%%B
  set "num.cnt=%%A
)

:: List the "array" members
for /l %%N in (1 1 %num.cnt%) do echo num.%%N=!num.%%N!

The code is even simpler and more efficient with my JREPL.BAT regular expression text processing utility. JREPL.BAT is pure script that runs natively on any Windows machine from XP onward.

The /JBEG option initializes counter n to 0. The /JMATCH treats the replacement string as a JScript expression. The Search looks for and captures a decimal number with optional leading minus sign that follows "] silence_start: ". The Replacement expressions increments the counter and concatenates that value with the found decimal number, with a space delimiter.

So now a single FOR /F can easily parse out the counter and decimal number.

@echo off
setlocal enableDelayedExpansion

:: Load the "array"
for /f "tokens=1,2" %%A in (
  'jrepl "] silence_start: (-?\d+\.\d+)" "++n + ' '+ $1" /jmatch /jbeg "n=0" /f "test.txt"'
) do (
  set "num.%%A=%%B"
  set "num.cnt=%%A"
)

:: List the "array" members
for /l %%N in (1 1 %num.cnt%) do echo num.%%N=!num.%%N!
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • I get this error 'jrepl' is not recognized as an internal or external command, operable program or batch file. – Brett Jul 06 '15 at 12:01
  • C:\Users\brett\Desktop\masterpuppets>mybat.bat num.1=-0.00260771 num.2=03 num.3=05 num.4=14 num.5=22 num.6=28 num.7=43 num.8=2955.28 num.9=51 out put from this example – Brett Jul 06 '15 at 12:04