Find/Replace Loops Help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tL_betheal
Posts: 3
Joined: 28 Jun 2012 14:02

Find/Replace Loops Help

#1 Post by tL_betheal » 28 Jun 2012 14:18

I have a log file that is giving me results, line by line, similar to the one listed below:

Code: Select all

AC01012011 value="log1.txt"

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?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Find/Replace Loops Help

#2 Post by abc0502 » 28 Jun 2012 14:36

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

tL_betheal
Posts: 3
Joined: 28 Jun 2012 14:02

Re: Find/Replace Loops Help

#3 Post by tL_betheal » 28 Jun 2012 15:03

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

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Find/Replace Loops Help

#4 Post by abc0502 » 28 Jun 2012 16:33

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

tL_betheal
Posts: 3
Joined: 28 Jun 2012 14:02

Re: Find/Replace Loops Help

#5 Post by tL_betheal » 28 Jun 2012 16:41

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.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Find/Replace Loops Help

#6 Post by abc0502 » 28 Jun 2012 16:55

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 ???? :cry:

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Find/Replace Loops Help

#7 Post by abc0502 » 28 Jun 2012 16:59

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

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Find/Replace Loops Help

#8 Post by foxidrive » 29 Jun 2012 01:58

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

Post Reply