Petri IT Knowledgebase Forums
 

Petri.co.il forums Home Forums Start Page Forums Frequently Asked Questions FAQ Member List Members List
Go Back   Petri IT Knowledgebase Forums > Windows Scripting > General Scripting
Petri.co.il is happy to award auglan the title of Most Valuable Member !!!
Register Calendar Calendar Search Petri IT Knowledgebase Forums Search Todays Posts Today's Posts Mark Forums Read

Notices

Search and Replace text via CMD?

Search and Replace text via CMD?

this thread has 6 replies and has been viewed 8882 times

Closed Thread
 
Thread Tools Search this Thread Display Modes
  #1  
Old 23rd June 2009, 21:23
rack04 rack04 is offline
Casual
Casual
 
 Join Date: May 2009
  6 month star 12 month star
 Posts: 13
 Reputation: rack04 is on a distinguished road (10)
Default 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 at 21:30..
  #2  
Old 23rd June 2009, 21:34
rack04 rack04 is offline
Casual
Casual
 
 Join Date: May 2009
  6 month star 12 month star
 Posts: 13
 Reputation: rack04 is on a distinguished road (10)
Default 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 at 22:06..
  #3  
Old 23rd June 2009, 22:13
Rems's Avatar
Rems Rems is offline
Moderator
 
 Join Date: Mar 2005
  6 month star 12 month star
 Location: NL
 Posts: 2,265
 Reputation: Rems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to behold (903)
Default Re: Search and Replace text via CMD?

similar thread: http://www.petri.co.il/forums/showpo...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
  #4  
Old 23rd June 2009, 23:23
rack04 rack04 is offline
Casual
Casual
 
 Join Date: May 2009
  6 month star 12 month star
 Posts: 13
 Reputation: rack04 is on a distinguished road (10)
Default 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%%)
  #5  
Old 24th June 2009, 07:21
vonPryz vonPryz is offline
Member
Here to help
 
 Join Date: Oct 2008
  6 month star 12 month star
 Location: Finland
 Posts: 354
 Reputation: vonPryz will become famous soon enough (80)
Default Re: Search and Replace text via CMD?

Quote:
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
  #6  
Old 24th June 2009, 09:12
Rems's Avatar
Rems Rems is offline
Moderator
 
 Join Date: Mar 2005
  6 month star 12 month star
 Location: NL
 Posts: 2,265
 Reputation: Rems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to behold (903)
Default 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
  #7  
Old 24th June 2009, 10:22
ghostdog74 ghostdog74 is offline
Casual
Casual
 
 Join Date: Sep 2007
  6 month star 12 month star
 Posts: 26
 Reputation: ghostdog74 is on a distinguished road (10)
Default 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>
Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
.cmd replace mutliple characters diekstra General Scripting 7 31st May 2008 23:05
Windows text file parser; I need to compare two text files... how? Nonapeptide Misc 10 30th April 2008 14:21
Cmd Key Bindings - Ctrl-D to quit cmd? humbletech99 Windows 2000 Pro, XP Pro 0 28th April 2008 11:10
google search text box alitoday Misc 4 15th January 2006 21:21
text in google search alitoday Misc 3 29th December 2004 04:47


All times are GMT +3. The time now is 05:37.

Steel Blue 3.5.4 vBulletin Style ©2006 vBEnhanced
Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
 

Valid XHTML 1.0!   Valid CSS!

Copyright 2005 Daniel Petri