View unanswered posts | View active topics It is currently 18 Sep 2014 07:42



Post new topic Reply to topic  [ 8 posts ] 
Find/Replace Loops Help 
Author Message

Joined: 28 Jun 2012 14:02
Posts: 3
Post Find/Replace Loops Help
I have a log file that is giving me results, line by line, similar to the one listed below:
Code:
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:
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?


28 Jun 2012 14:18
Profile

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
Post Re: Find/Replace Loops Help
Hi, tL_betheal
If what I understood was Correct, The log file will look like that:
Quote:
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:
Quote:
@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


28 Jun 2012 14:36
Profile

Joined: 28 Jun 2012 14:02
Posts: 3
Post Re: Find/Replace Loops Help
The log file looks like this:

Quote:
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:

Quote:
log1.txt
log2.txt
log3.txt
log4.txt
log5.txt
log6.txt


28 Jun 2012 15:03
Profile

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
Post Re: Find/Replace Loops Help
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:

Quote:
"log1.txt"
"log2.txt"

here it is:

Quote:
@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


28 Jun 2012 16:33
Profile

Joined: 28 Jun 2012 14:02
Posts: 3
Post Re: Find/Replace Loops Help
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.


28 Jun 2012 16:41
Profile

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
Post Re: Find/Replace Loops Help
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:


28 Jun 2012 16:55
Profile

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
Post Re: Find/Replace Loops Help
OK i found it we have to enable the delayed expansion like that in the batch file and the set will work

Code:
@echo off
cls
setlocal EnableDelayedExpansion
:: all your code here


28 Jun 2012 16:59
Profile

Joined: 10 Feb 2012 02:20
Posts: 4181
Post Re: Find/Replace Loops Help
This is the same but it sorts the data, if that is needed.

Code:
@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


29 Jun 2012 01:58
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Yahoo [Bot] and 20 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.