Re: Substring Each Line in a File
- From: "billious" <billious_1954@xxxxxxxxxxx>
- Date: Sat, 18 Nov 2006 03:05:19 +0800
"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 .....
.
- Follow-Ups:
- Re: Substring Each Line in a File
- From: MJ
- Re: Substring Each Line in a File
- References:
- Substring Each Line in a File
- From: MJ
- Substring Each Line in a File
- Prev by Date: Substring Each Line in a File
- Next by Date: Re: Substring Each Line in a File
- Previous by thread: Substring Each Line in a File
- Next by thread: Re: Substring Each Line in a File
- Index(es):
Relevant Pages
|