Page 1 of 1

Find row, manipulate, put in array and write output

Posted: 05 Aug 2010 07:56
by darioit
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

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

Posted: 05 Aug 2010 09:19
by !k

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

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

Posted: 05 Aug 2010 12:43
by darioit
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?

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

Posted: 05 Aug 2010 13:42
by !k
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: =%%"
...