Substring Each Line in a File

M

MJ

I have a for loop to process each line of a text file, but I do not
know how to echo a substring of each line (I will then redirect each
line to a separate file). Here's what I have so far:

@echo off
for /f %%a in (myfile.txt) do (
set var=%%a
echo %var:~1,13%
)
@echo on

This pulls off the 13 characters I am looking for, but it echoes the
same value 10 times (for the 10 lines in the file). That value is the
start of the last line in the file, but I want the start of each line.
When I echo %%a, I can see each line of the file, but I need just the
first part of each line.

Any help is greatly appreciated.
 
B

billious

MJ said:
I have a for loop to process each line of a text file, but I do not
know how to echo a substring of each line (I will then redirect each
line to a separate file). Here's what I have so far:

@echo off
for /f %%a in (myfile.txt) do (
set var=%%a
echo %var:~1,13%
)
@echo on

This pulls off the 13 characters I am looking for, but it echoes the
same value 10 times (for the 10 lines in the file). That value is the
start of the last line in the file, but I want the start of each line.
When I echo %%a, I can see each line of the file, but I need just the
first part of each line.

Any help is greatly appreciated.

Erm - no, it doesn't. It produces the 13 characters starting at the SECOND
character of %%a. If you want the FIRST 13, you need to use ~0,13

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /f %%a in (myfile.txt) do (
set var=%%a
echo !var:~1,13!
)
@echo on

See

SETLOCAL /?

from the prompt for documentation.

Note that %varname% is the value of varname at the logical line's PARSE
time. If ENABLEDELAYEDEXPANSION is in effect, !varname! is the current
(run-time or dynamic) value of varname.

SETLOCAL however causes environment changes to be TEMPORARY. The changes are
discarded on a matching ENDLOCAL or at end-of-batch which is an implicit
ENDLOCAL.

It's possible to make the changes permanent by using

ENDLOCAL&set varname=%varname%

(this might not seem relevant for the current task - just trying to pre-empt
the regular next-question)

More NT-batch techniques in alt.msdos.batch.nt .....
 
A

Alexander Suhovey

MJ said:
@echo off
for /f %%a in (myfile.txt) do (
set var=%%a
echo %var:~1,13%
)
@echo on

This pulls off the 13 characters I am looking for, but it echoes the
same value 10 times (for the 10 lines in the file).

MJ,
You need to use delayed expansion or CALL your commands so they are executed
outside FOR loop. Consider following examples:

1.
@echo off
setlocal enabledelayedexpansion
for /f %%a in (myfile.txt) do (
set var=%%a
echo !var:~1,13!
)

2.
@echo off
for /f %%a in (myfile.txt) do (
call :do %%a
)
goto :eof
:do
set var=%1
echo %var:~1,13%
goto :eof


More info on delayed expansion in 'SET /?'
 
M

MJ

Thanks a lot! It is working great now. Actually, I did not want the
first character of the line, but the next 13 characters, so the
substring part was OK. You were looking out for me though. Thanks
also for the explanations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top