Page 1 of 1
Find/Replace Loops Help
Posted: 28 Jun 2012 14:18
by tL_betheal
I have a log file that is giving me results, line by line, similar to the one listed below:
This continues with different dates and different log files for several thousands of lines.
I'd like to set a FOR loop that views the text file, but deletes everything before value.
Code: Select all
FOR /F "tokens=* delims=" %%i in (C:\test\master_logs.txt) DO (
set line=%%i
set sort=%line:*value%
echo %sort% >> sorted_logs.txt
)
The above loop is what I currently have, but only outputs ECHO is off in the sorted_logs.txt file
I believe my issue is with the first set, in that it is trying to set a parameter value as a variable, and for some reason it doesn't like that.
Does anyone know how this can be easily accomplished?
Re: Find/Replace Loops Help
Posted: 28 Jun 2012 14:36
by abc0502
Hi, tL_betheal
If what I understood was Correct, The log file will look like that:
AC01012011 value="log1.txt"
AC01012011 value="log1.txt"
AC01012011 value="log1.txt"
AC01012011 value="log1.txt"
If so Try this i tested and it work fine:
@echo off
cls
set "log=C:\log.txt"
set "sorted_log=C:\sorted_log.txt"
For /f "tokens=2 delims= " %%a in (%log%) do echo.%%a >>%sorted_log%
Just Remember to Change the Line in Red
Re: Find/Replace Loops Help
Posted: 28 Jun 2012 15:03
by tL_betheal
The log file looks like this:
AC01012011 value="log1.txt"
AC01022011 value="log2.txt"
AC01032011 value="log3.txt"
AC01042011 value="log4.txt"
AC01052011 value="log5.txt"
AC01062011 value="log6.txt"
This is only a small portion, as this continues on over 1000 times.
The sets you provided worked, but the FOR loop seems to have an issue. Maybe I misunderstand tokens, but giving token a value of 2, doesn't that just look at the second line? When I replace 2 with * to process all lines, it doesn't change anything. My goal is to have the above log file look like the below file:
log1.txt
log2.txt
log3.txt
log4.txt
log5.txt
log6.txt
Re: Find/Replace Loops Help
Posted: 28 Jun 2012 16:33
by abc0502
you are correct there somthing wrong every time i try to set the %%i to a variable it gibe ECHO OFF
I Don't know what is wrong but i made another code that give this result:
"log1.txt"
"log2.txt"
here it is:
@echo off
cls
set "log=C:\log.txt"
set "sorted_log=C:\sorted_log.txt"
FOR /F "tokens=2 delims==" %%i in (%log%) DO echo %%i >>%sorted_log%
sorry if i coudn't help
Re: Find/Replace Loops Help
Posted: 28 Jun 2012 16:41
by tL_betheal
I just completed it using a little bit of what you gave me, some more tweaking, more FOR loops, and some research on how to get rid of double quotes with delimiters. I have completed my script and it works wonderful. Thank you for the guidance.
Re: Find/Replace Loops Help
Posted: 28 Jun 2012 16:55
by abc0502
that is great but the problem with the set %%i to a variable happen every time u use the () in the for loop and that didn't happen with me before and now it happen in all my batches ????
Re: Find/Replace Loops Help
Posted: 28 Jun 2012 16:59
by abc0502
OK i found it we have to enable the delayed expansion like that in the batch file and the set will work
Code: Select all
@echo off
cls
setlocal EnableDelayedExpansion
:: all your code here
Re: Find/Replace Loops Help
Posted: 29 Jun 2012 01:58
by foxidrive
This is the same but it sorts the data, if that is needed.
Code: Select all
@echo off
del file.tmp 2>nul
for /f "tokens=2 delims==" %%a in (log.txt) do (
>>file.tmp echo %%~a
)
sort <file.tmp >sorted_logs.txt
del file.tmp