Re: Substring Each Line in a File

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




"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: Days until Dec 1
    ... > echo You have $diffdate days until the big event ... brute force fix is: ... The sed command strips leading zeros, ... tries to treat the two variables as commands ($(varname) ...
    (comp.os.linux)
  • Re: How to output a list to a line?
    ... on one line with quotes around each file name and a space between them. ... SetLocal EnableDelayedExpansion ... echo %Line% ... and ensures that no additional blank characters are present in the output. ...
    (microsoft.public.win2000.cmdprompt.admin)
  • Re: Substring Each Line in a File
    ... know how to echo a substring of each line (I will then redirect each ... This pulls off the 13 characters I am looking for, ... Note that %varname% is the value of varname at the logical line's PARSE ...
    (microsoft.public.win2000.cmdprompt.admin)
  • Re: From a bash array to a csv file line?
    ... the input from the user at runtime for each and then "echo" their ... values with the date to a csv file so that all would be comma-separated ... for varname in FOO BAR BAZ QUX; ... printf "Enter $varname: " ...
    (comp.unix.shell)
  • 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)