Re: Substring Each Line in a File

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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 .....

.



Relevant Pages

  • Re: Password creating Theories
    ... I just use this little php script from a command line ... // take a 8 characters long substring as a password ... echo $password ...
    (Security-Basics)
  • Re: [opensuse] Colorized output, echo via xargs.
    ... Here you're invoking the shell's built-in echo command, ... You can explicitly turn off the interpretation of the above ... characters with the -E option. ... -e enable interpretation of backslash escapes ...
    (SuSE)
  • Re: [opensuse] Basic Bash Question
    ... It was the result of the 'echo *' statement. ... Why it would interpret the *** is also just as strange. ... interpretation of the following backslash-escaped characters is ... The -E option disables the interpretation of these escape ...
    (SuSE)
  • Re: [opensuse] Basic Bash Question
    ... It was the result of the 'echo *' statement. ... Why it would interpret the *** is also just as strange. ... The -E option disables the interpretation of these escape ... characters, even on systems where they are interpreted by default. ...
    (SuSE)
  • Re: animated characters that present your powerpoint presentation
    ... Echo has nailed it. ... Microsoft PPT MVP ... Featured Presenter, PowerPoint Live 2004 ... Peedy and Robby characters are free. ...
    (microsoft.public.powerpoint)