Announcement

Collapse
No announcement yet.

Search and Replace text via CMD?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Search and Replace text via CMD?

    I have a dga file (which can be opened and edited in notepad) that is generated by a third party application. I need to edit one line in the file. The file is called:

    "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.dga"

    Here is a portion of the file:

    Code:
    DGAVCIndexFile7
    
    C:\Personal\Videos\87943231_1280x532_23.976.h264
    C:\Program Files\DGAVCIndex\
    
    STREAM 0
    FO 2
    YUVRGB 1
    LUMFILT 0 0
    CLIP 0 0 0 0
    RANGE 0 117586579
    AUDIO 
    --------Bulk of the file removed here since it takes up a lot of space--------
    SIZ 1280 x 532 
    FPS 50 / 2
    CODED 3339
    PLAYBACK 3339
    Notice "FPS 50 / 2"? I need to replace that line with "FPS 24000 / 1001". Any help is very much appreciated. Thanks.
    Last edited by rack04; 23rd June 2009, 19:30.

  • #2
    Re: Search and Replace text via CMD?

    Initially I thought I could just use the find command but it adds the following text to the top of the file:


    ---------- %WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.DGA

    Code:
    find /V "FPS " "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.dga" > "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.dga0"
    echo FPS 24000 / 1001>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.dga0"
    move /Y "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.dga0" "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.dga"
    Last edited by rack04; 23rd June 2009, 20:06.

    Comment


    • #3
      Re: Search and Replace text via CMD?

      similar thread: http://forums.petri.com/showpost.php...13&postcount=4


      Code:
      @echo off & setlocal enabledelayedexpansion
      
      Set "inputFile=C:\test\test.txt"
      Set "outputFile=%inputFile%-TMP.txt"
      
      >"%outputFile%" (
         For /f "delims=" %%* in (
           'find.exe /n /v ""^<"%inputFile%"'
           ) Do (
           Call:GoParse "''%%*"
           (echo\!WriteLine!)
         )   
      )
      
      ::# replace original file (! remove 'rem' !)
      rem Move /Y "%outputFile%" "%inputFile%"
      
      :end   ----------
           
      goto:eof
      :GoParse lines
      (Set /a i = %i% +1)&(Set Line=%1)
      (Set Line=!Line:''[%i%]=!)&(Set "WriteLine=")
      
      :: string substitution (is case-insensitive!)
      :: http://www.robvanderwoude.com/ntset.php#StrSubst
      (Set Line=%Line:FPS 50 / 2=FPS 24000 / 1001%)
      
      (Set WriteLine=%Line:~1,-1%)
      goto:eof
      \Rems

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        Re: Search and Replace text via CMD?

        Thanks for all the help. I just have a couple more questions.

        Here is where I stand:

        Code:
        @SET WORKING_DIRECTORY=C:\Personal\Videos
        
        @echo off & setlocal enabledelayedexpansion
        set /p    INPUT_VIDEO="Full path to DGA file: "
        
        Move /Y "%INPUT_VIDEO%" "%WORKING_DIRECTORY%\test.txt"
        
        Set "inputFile=%WORKING_DIRECTORY%\test.txt"
        Set "outputFile=%inputFile%-TMP.txt"
        
        >"%outputFile%" (
           For /f "delims=" %%* in (
             'find.exe /n /v ""^<"%inputFile%"'
             ) Do (
             Call:GoParse "''%%*"
             (echo\!WriteLine!)
           )   
        )
        
        Move /Y "%outputFile%" "%INPUT_VIDEO%"
        
        Del "%inputFile%"
        
        :end   ----------
             
        goto:eof
        :GoParse lines
        (Set /a i = %i% +1)&(Set Line=%1)
        (Set Line=!Line:''[%i%]=!)&(Set "WriteLine=")
        
        (Set Line=%Line:FPS 50 / 2=FPS 24000 / 1001%)
        
        (Set WriteLine=%Line:~1,-1%)
        goto:eof
        Where would I put a PAUSE so not to close the cmd window once complete? Also, is there a way to use variables for the substitution? For example:

        (Set Line=%Line:FPS 50 / 2=FPS %NOMINATOR% / %DENOMINATOR%%)

        Comment


        • #5
          Re: Search and Replace text via CMD?

          Originally posted by rack04 View Post
          Where would I put a PAUSE so not to close the cmd window once complete?
          Just run the script from command line, no pause needed. Othervise, here:

          Code:
          Del "%inputFile%"
          pause
          :end
          -vP

          Comment


          • #6
            Re: Search and Replace text via CMD?

            When you insert a Pause in the batch you still won't be able to see the results on screen because the output is redirected to the file.

            To show output on screen and WRITE it to a file the same time you should insert a TEE routine in the batch.
            However, it would also be possible to Echo %WriteLine% simply twice - one it will be redirected for APPENDING to the file and one time for showing the results on the screen.
            Code:
            @echo off & setlocal enabledelayedexpansion
            
            Set "inputFile=C:\test\test.txt"
            Set "outputFile=%inputFile%-TMP.txt"
            
            Set "NOMINATOR=24000"
            Set "DENOMINATOR=1001"
            
            If exist "%outputFile%" del "%outputFile%"
            For /f "delims=" %%* in (
               'find.exe /n /v ""^<"%inputFile%"'
               ) Do (
               >>"%outputFile%" Call:GoParse "''%%*"
               (echo\!WriteLine!)
            )   
            
            ::# replace original file (! remove 'rem' !)
            rem Move /Y "%outputFile%" "%inputFile%"
            
            echo\&pause
            :end  ----------
                 
            goto:eof
            :GoParse lines
            (Set /a i = %i% +1)&(Set Line=%1)
            (Set Line=!Line:''[%i%]=!)&(Set "WriteLine=")
            
            :: string substitution (is case-insensitive!)
            :: http://www.robvanderwoude.com/ntset.php#StrSubst
            (Set Line=!Line:FPS 50 / 2=FPS %NOMINATOR% / %DENOMINATOR%!)
            
            (Set WriteLine=%Line:~1,-1%)
            (echo\!WriteLine!)
            goto:eof
            \Rems

            This posting is provided "AS IS" with no warranties, and confers no rights.

            __________________

            ** Remember to give credit where credit's due **
            and leave Reputation Points for meaningful posts

            Comment


            • #7
              Re: Search and Replace text via CMD?

              for text processing, please get a good text processing tool, such as sed or gawk for windows. eg

              Code:
              C:\test>more file.txt
              DGAVCIndexFile7
              
              C:\Personal\Videos\87943231_1280x532_23.976.h264
              C:\Program Files\DGAVCIndex\
              
              STREAM 0
              FO 2
              YUVRGB 1
              LUMFILT 0 0
              CLIP 0 0 0 0
              RANGE 0 117586579
              AUDIO
              --------Bulk of the file removed here since it takes up a lot of space--------
              SIZ 1280 x 532
              FPS 50 / 2
              CODED 3339
              PLAYBACK 3339
              
              C:\test>sed -i.bak "s/FPS *50 *\/ *2/FPS 24000 \/ 1001/" file.txt
              
              C:\test>more file.txt
              DGAVCIndexFile7
              
              C:\Personal\Videos\87943231_1280x532_23.976.h264
              C:\Program Files\DGAVCIndex\
              
              STREAM 0
              FO 2
              YUVRGB 1
              LUMFILT 0 0
              CLIP 0 0 0 0
              RANGE 0 117586579
              AUDIO
              --------Bulk of the file removed here since it takes up a lot of space--------
              SIZ 1280 x 532
              FPS 24000 / 1001
              CODED 3339
              PLAYBACK 3339
              
              C:\test>

              Comment

              Working...
              X