parsing text with spaces?

L

LWG

I am trying to find a way to parse the output from an arcserver command. The
command basically dumps the tapes loaded in the 40 available slots. The
output follows: (btw this is only 5 of the 40 slots)

SLOT NAME ID SEQ. SERNO LOADED WP
1 <<Cleaning Cartridge>> 0000 0 -- --
2 BD8671L1 7f4a 1 BD8671L1 NO NO
3 BD8698L1 902a 4 BD8625L1 NO NO
4 6/19/04 5:50 PM bf50 1 BD8568L1 NO NO
5 9/24/04 6:47 PM b49d 1 BD8975L1 YES NO

the columns are sorted like:
slot name id seq. serno loaded wp

the diffuculty is when there are spaces in the name. The slot 1 has the
cleaning cartridge which has no serial number and creates another porblem.
What I want as an ouput is the slot and serno columns. So it would look
like:

1 bd8569l1
2 bd8975l1
3 bd1234l1
4 bd3338l1...

Thx for the help in advance...

L
 
M

Matthias Tacke

:
SLOT NAME ID SEQ. SERNO LOADED WP
1 <<Cleaning Cartridge>> 0000 0 -- --
2 BD8671L1 7f4a 1 BD8671L1 NO NO
3 BD8698L1 902a 4 BD8625L1 NO NO
4 6/19/04 5:50 PM bf50 1 BD8568L1 NO NO
5 9/24/04 6:47 PM b49d 1 BD8975L1 YES NO
01234567890123456789012345678901234567890123456789012345678901234567890
1 bd8569l1
2 bd8975l1
3 bd1234l1
4 bd3338l1...
Read with a for and use set to extract the data from offset, length

@echo off
setlocal
for /f "delims=" %%A in ('findstr "YES NO" ^<infile.txt'
) do call :sub "%%A"
goto :eof
:sub
set line=%~1
set SLOT=%line:~0,2%
set SERNO=%line:~43,8%
echo %SLOT% %SERNO%

Untested!

HTH
 
L

LWG

I have to say this worked, if you have moment could you explain from this
point?

for /f "delims=" %%A in ('findstr "YES NO" ^<infile.txt'> ) do call :sub
"%%A"

1. correct me if I am wrong- for read in infile.txt and scans for yes no
then?
2. goto :eof?
3. set line=%1 ?
4.set slot=?
5.set serno?

btw, this worked with nothing other than me changing the file from
infile.txt to myname.txt.

thank you,
Larry
 
M

Michael Bednarek

I have to say this worked, if you have moment could you explain from this
point?

for /f "delims=" %%A in ('findstr "YES NO" ^<infile.txt'> ) do call :sub
"%%A"

1. correct me if I am wrong- for read in infile.txt and scans for yes no
then?

I don't understand the question.
2. goto :eof?

CMD's way for a QUIT command. See "GOTO /?".
3. set line=%1 ?
4.set slot=?
5.set serno?

See "SET /?" and "FOR /?".

[snip]
 
M

Matthias Tacke

LWG said:
I have to say this worked, if you have moment could you explain from
this point?

for /f "delims=" %%A in ('findstr "YES NO" ^<infile.txt') do call :sub "%%A"

1. correct me if I am wrong- for read in infile.txt and scans for yes
no then?

I used findstr to filter the relevant lines which contain either YES or
NO. The for iterates through the output of findstr reading the whole
lines into %%A, instructed by the "delims=" *not* to split into args.

I used an internal sub to ease the following positional extraction (An
alternative is to use delayed expansion possible from w2k or newer).

The call :sub with quoted "%%A" is necessary to pass the line as one
argument. The arguments passing is the same as with external batches,
the first arg being %1.
2. goto :eof?
Leave the batch, an alternative would be: exit /B 0
3. set line=%1 ?
In fact it is "set line=%~1" to put the content with removed outmost
quotes into the environment variable line
4.set slot=?
set SLOT=%line:~0,2%
gets two places from offset zero into env var SLOT
5.set serno?
set SERNO=%line:~43,8%
gets eight places from offset 43 into env var SERNO

I'm just glad you didn't ask for this :)
echo %SLOT% %SERNO%
btw, this worked with nothing other than me changing the file from
infile.txt to myname.txt.
Fine, you are able to tweak that and use batch tricks in future. For
details on the used commands follow my signature and Michaels suggestion.
 

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