|
Answer» Hi ;
I need to extract the figures from a file which contains below information using DOS:
2, ,[1024], Application, CBR Server,Throughput (bits/s) = 269338 3, ,[1025], Application, CBR Server,Throughput (bits/s) = 263941 4, ,[1026], Application, CBR Server,Throughput (bits/s) = 266663 5, ,[1027], Application, CBR Server,Throughput (bits/s) = 265108 6, ,[1028], Application, CBR Server,Throughput (bits/s) = 268421 7, ,[1029], Application, CBR Server,Throughput (bits/s) = 269602 8, ,[1030], Application, CBR Server,Throughput (bits/s) = 266973 9, ,[1031], Application, CBR Server,Throughput (bits/s) = 266477 10, ,[1032], Application, CBR Server,Throughput (bits/s) = 270655 11, ,[1033], Application, CBR Server,Throughput (bits/s) = 266822 12, ,[1034], Application, CBR Server,Throughput (bits/s) = 268137 13, ,[1035], Application, CBR Server,Throughput (bits/s) = 267681 14, ,[1036], Application, CBR Server,Throughput (bits/s) = 266535
I have tried: for /f "tokens=7 delims=,=" %%a in ("test.txt") Do Echo %%a >> tgk.txt
I got: %%a was unexpected at this time.
Why is it? Does this codes able to read all the lines in the file?
Many thanks.
The reason you got the error is because on the command line you type in 1 '%', from a BATCH you use 2 '%%'
this appears to solve your problem for /f "delims=*" %a in (test.txt) Do CALL set aa=%a&echo %aa:~-6%
which trims the last 6 chars from each line - ASSUMING your number is always 6 digits
if you run it in a batch, you would do this for /f "delims=*" %%a in (test.txt) Do call set aa=%%a&echo %aa:~-6% C:\test>type zee2.bat Code: [Select]@echo off for /f "tokens=1-7 delims=,=],[" %%a in (test.txt) do ( echo %%a %%c %%g ) Output:
C:\test>zee2.bat 2 1024 269338 3 1025 263941 4 1026 266663 5 1027 265108 6 1028 268421 7 1029 269602 8 1030 266973 9 1031 266477 10 1032 270655 11 1033 266822 12 1034 268137 13 1035 267681 14 1036 266535
C:\test>Quote from: RuZee on May 13, 2010, 08:09:35 AM I need to extract the figures from a file which contains below information using Batch:
If RuZee needs only the last six numbers from each input line, then we were able to use the gpl code with a slight midification:
C:\test>type gpl.bat Code: [Select]@echo off setlocal enabledelayedexpansion for /f "delims=*" %%a in (test.txt) do ( set aa=%%a rem echo aa = !aa! rem echo %%a echo !aa:~-6!
) Output:
C:\test>gpl.bat 269338 263941 266663 265108 268421 269602 266973 266477 270655 266822 268137 267681 266535
C:\test>Quote from: RuZee on May 13, 2010, 08:09:35 AM I have tried: for /f "tokens=7 delims=,=" %%a in ("test.txt") Do Echo %%a >> tgk.txt
I got: %%a was unexpected at this time.
Why is it? Does this codes able to read all the lines in the file?
Zee,
Your code is correct except you asked for a display of token 7 and then tried to use token one.
C:\test>type zee4.bat Code: [Select]@echo off echo > tgk.txt for /f "tokens=7 delims=,=" %%g in (test.txt) Do ( Echo %%g >> tgk.txt )
type tgk.txt Output:
C:\test>zee4.bat 269338 263941 266663 265108 268421 269602 266973 266477 270655 266822 268137 267681 266535
C:\test>Quote from: marvinengland on May 15, 2010, 03:02:35 AMZee,
Your code is correct except you asked for a display of token 7 and then tried to use token one.
C:\test>type zee4.bat Code: [Select]@echo off echo. > tgk.txt 2>null for /f "tokens=1-7 delims=,=" %%a in (test.txt) Do ( echo. %%g >> tgk.txt 2>null )
type tgk.txt Output:
C:\test>zee4.bat
269338 263941 266663 265108 268421 269602 266973 266477 270655 266822 268137 267681 266535
C:\test>
p.s This code is better than the above post. We filter out the echo off message with 2>null
|