Missing Operator.

November 16, 2009 at 14:39:24
Specs: Windows Vista
i have this batch file.
==================
SETLOCAL ENABLEDELAYEDEXPANSION
set /a count=0
set /a total=0

for /f "eol= tokens=2" %%G in (test.txt) do (
set /a firstchar=%%G
if "!firstchar:~0,1!" neq "-" (
if /i %%G gtr 0.0 (
if %%G neq 147356 (
set /a count=!count!+1
set /a total=!total!+%%G
)
)
)
)
set /a avg=!total!/!count!
echo !avg!
=================
i keep getting the error "Missing Operator." and it appears after each iteration of the for loop and before the next. however, all of my calculations and variables seem fine.

what am i doing wrong?


See More: Missing Operator.

Report •


#1
November 16, 2009 at 15:54:28
What's in test.txt?


=====================================
Helping others achieve escape felicity

M2


Report •

#2
November 16, 2009 at 16:12:06
just numbers
i'm grabbing the second field. the layout is something similar to

(junk, esc char?) 0.0 lbs blah blah blah
(junk, esc char?) 4.5 lbs blah blah blah
(junk, esc char?) 7.8 lbs blah blah blah
(junk, esc char?) -0.1 lbs blah blah blah
(junk, esc char?) 0.0 lbs blah blah blah


Report •

#3
November 16, 2009 at 16:13:11
oops, the layout of the text in the original post is all wrong. i
hope you can read it. thanks.

Report •

Related Solutions

#4
November 16, 2009 at 17:33:27
With tokens=2 %%G is set to esc, which is no good for an arithmetic set.


=====================================
Helping others achieve escape felicity

M2


Report •

#5
November 16, 2009 at 17:44:38
if you can download stuff, use gawk for windows, (see my sig)
c:\test> gawk '{gsub(/.*) /,\"\");gsub(/ .*/,\"\")}1' file
0.0
4.5
7.8
-0.1
0.0

GNU win32 packages | Gawk


Report •

#6
November 16, 2009 at 20:18:39

ghost,
are you saying i should use gawk in my script? or, use gawk to
re-format the file, then run my script on the cleaner file?

mechanix,
what do you suggest then? a different language? can i do this
with vbs? i've probably forgotten everything i may have learned.


Report •

#7
November 16, 2009 at 21:24:08
With the lines you posted and assuming you want the chunks like:

0.0

you need token 4.

But you can not set /a to a decimal number; integers only.

Here's a workaround.

=================================
@echo off & setLocal enableDELAYedexpansion

set /a count=0
set /a total=0

for /f "tokens=4" %%G in (test.txt) do (
set s=%%G
set s=!s:.=!
if !s! gtr 0 (
if !s! neq 147356 (
set /a count+=1
set /a total=!total!+s
)
)
)
set /a avg = total / count
echo total:!total!
echo count:!count!
echo avg:!avg!

echo real avg is: !avg:~0,-1!.!avg:~-1!


=====================================
Helping others achieve escape felicity

M2


Report •

#8
November 16, 2009 at 21:35:49
@MayhemMehod

gawk is just an executable, much like ping.exe , ftp.exe. You can run it inside your batch file as per normal eg

@echo off
....
gawk '{gsub(/.*) /,\"\");gsub(/ .*/,\"\")}1' file  > newfile
for /f %%i in ('type newfile') do (
  rem do processing...
)

if you find it a hassle to download stuff, you can use native vbscript

Set objFS=CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
	strLine=objFile.ReadLine
	s = InStr(strLine,")")
	If s > 0 Then
		splitted = Split(Trim(Mid(strLine,s+1))," ")
		WScript.Echo splitted(0)
	End If
Loop
objFile.Close

output

C:\test>cscript /nologo test.vbs
0.0
4.5
7.8
-0.1
0.0

GNU win32 packages | Gawk


Report •

#9
November 17, 2009 at 06:35:57
interesting work around Mechanix2Go. thanks for your time and patience with me. two things though...

1. i still had to use tokens=2
i'm sure i described the file wrong. the first column of data was a single character that looks like a backwards arrow <-. i can't even find it on the character map

2. i still do not fully understand the MissingOperator. error? i had a similar version of the program doing arithmetic on decimals, but i wasn't getting that message. i can't find it now, so i won't bother trying to re-create.


Report •

#10
November 17, 2009 at 06:37:23
2. maybe it was before i learn the setlocal command the the ! vars.

Report •

#11
November 18, 2009 at 04:36:07
Keep in mind this constrains the precision.


=====================================
Helping others achieve escape felicity

M2


Report •

#12
November 20, 2009 at 07:55:59
Warning: CMD doesn't do floating point. What does this mean? Text comparisons.

H:\>if -1.0 gtr 0.0 @echo Y
Y

H:\>if -1 gtr 0 @echo Y

H:\>

And Integer math:

H:\>set /a 1 / 2
0
H:\>


Report •

#13
November 20, 2009 at 07:58:12
thanks, we've covered that and i have researched elsewhere as well.

Report •

#14
November 20, 2009 at 08:04:10
As for your error, assuming you leave ECHO on, you should be able to see exactly what the script is trying to do when it produces an error. Perhaps you should show us that?

Report •

#15
November 20, 2009 at 08:43:38
i type like a 3 year old (reading my previous posts with typos)

this isn't exactly it since i made some changes since the original post. you can close this thread if you'd like. i was doing all kinds of stupid newbie stuff like math operations on strings. i probably shouldn't have brought up the missing operator again after mechanix got me straightened out.

C:\ftp>rem @echo off

C:\ftp>SETLOCAL ENABLEDELAYEDEXPANSION

C:\ftp>echo Fri 11/20/2009 10:24:29.71 1>temp.txt

C:\ftp>echo. 1>>temp.txt

C:\ftp>set /a count=0

C:\ftp>set /a total=0

C:\ftp>for /F "eol= tokens=2" %G in (test.txt) do (
set /a firstchar=%G
if "!firstchar:~0,1!" NEQ "-" (if /I %G GTR 0.0 (if %G NEQ 147356 (
set /a count=!count!+1
set /a total=!total!+%G
) ) )
pause
)

C:\ftp>(
set /a firstchar=147356
if "!firstchar:~0,1!" NEQ "-" (if /I 147356 GTR 0.0 (if 147356 NEQ 147356 (
set /a count=!count!+1
set /a total=!total!+147356
) ) )
pause
)
Press any key to continue . . .

C:\ftp>(
set /a firstchar=-5.0
if "!firstchar:~0,1!" NEQ "-" (if /I -5.0 GTR 0.0 (if -5.0 NEQ 147356 (
set /a count=!count!+1
set /a total=!total!+-5.0
) ) )
pause
)
Missing operator.
Press any key to continue . . .


Report •

#16
November 20, 2009 at 09:07:23
Are you still in need or is this just FYI?


=====================================
Helping others achieve escape felicity

M2


Report •

#17
November 20, 2009 at 09:09:55
it was just fyi. i have a working version now.

Report •

#18
November 20, 2009 at 09:12:18
Just so everyone's clear, you can only do integer math with SET /A:
F:\Cpp\Dev-Cpp\include\c++\3.4.5>set /a 3 + 5.0
Missing operator.

The other two don't make it past all the IF statements, so they bomb on the SET.


Report •


Ask Question