Re: Substring Each Line in a File
- From: "MJ" <mmurphy@xxxxxxxxxxxxxxxxxx>
- Date: 17 Nov 2006 11:35:47 -0800
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.
billious wrote:
"MJ" <mmurphy@xxxxxxxxxxxxxxxxxx> wrote in message
news:1163785330.851380.33380@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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 .....
.
- References:
- Substring Each Line in a File
- From: MJ
- Re: Substring Each Line in a File
- From: billious
- Substring Each Line in a File
- Prev by Date: Re: Substring Each Line in a File
- Next by Date: Applications listing
- Previous by thread: Re: Substring Each Line in a File
- Next by thread: Re: Substring Each Line in a File
- Index(es):
Relevant Pages
|