Find row, manipulate, put in array and write output

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Find row, manipulate, put in array and write output

#1 Post by darioit » 05 Aug 2010 07:56

Hello,
please help me to this problem

This is a contents of C:\file.txt
A1field1AAAfield2CCCCCCfield3
A2otherotherotherotherotherot
A3otherotherotherotherotherot
A1field1AAAfield2CCCCCCfield3
A2otherotherotherotherotherot
A3otherotherotherotherotherot

I need to take only record that begins 1,2 with A1 and take Field1,2,3
After write this records in a output file


Code: Select all

@Echo OFF
call :find_records C:\file.txt
write array linefound.%count% to newfile.txt

:find_records
SET /A count=0
FOR /F %%j IN ('FINDSTR "A1" %1') DO (
SET /A count=count+1
SET line2=%%j
CALL SET field1=%%line2:~3,6%%%
CALL SET fieldFIX=PIPPO
CALL SET field2=%%line2:~12,6%%%
CALL SET field3=%%line2:~24,6%%%
CALL SET linefound.%count% = %field1% fieldFIX %field2% %field3%
)
GOTO:EOF

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Find row, manipulate, put in array and write output

#2 Post by !k » 05 Aug 2010 09:19

Code: Select all

@echo off
setlocal enableextensions
call :find_records file.txt
for /f "tokens=1* delims==" %%a in ('set linefound.') do echo %%b>>newfile.txt
endlocal
GOTO:EOF

:find_records
SET /A count=0
FOR /F %%j IN ('FINDSTR /b /c:"A1" %1') DO (
SET /A count=count+1
SET "line2=%%j"
CALL SET field1=%%line2:~2,6%%
CALL SET fieldFIX=PIPPO
CALL SET field2=%%line2:~11,6%%
CALL SET field3=%%line2:~23,6%%
CALL SET linefound.%%count%%=%%field1%% %%fieldFIX%% %%field2%% %%field3%%
)
GOTO:EOF

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Find row, manipulate, put in array and write output

#3 Post by darioit » 05 Aug 2010 12:43

Thanks !k
Your modification works fine, but in one case when the row is like this:

A1field1AAAfield2CCCC field3
having a blank before field3 this field is missing, why?

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Find row, manipulate, put in array and write output

#4 Post by !k » 05 Aug 2010 13:42

Change code

Code: Select all

...
FOR /F "delims=" %%j IN ('FINDSTR /b /c:"A1" %1') DO (
SET /A count=count+1
SET "line2=%%j"
call set "line2=%%line2: =%%"
...

Post Reply