Computing.Net > Forums > Programming > | was unexpected at this time.

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

| was unexpected at this time.

Reply to Message Icon

Name: sforsasi
Date: July 2, 2008 at 11:34:54 Pacific
OS: XP
CPU/Ram: 1 GB
Product: Windows
Comment:

I'm using the following command
for /F %x in ('findstr /r /n "Output" C:\test\sasi.log | find /C ":"')
do set XXX=%x
echo %x

and get the error message as
"| was unexpected at this time."

Actually I'm trying to get the number of lines which contains the word "Output" in the file "sasi.log" and trying to store the result in a variable. (which will be finally used as exit code of the bat file)

Any help would be appreciated.

Skumar.



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: July 2, 2008 at 12:35:17 Pacific
Reply:

The pipe and redirector when coded inside the For /F clause must be prefixed by the caret (^), i.e.
C:\test\sasi.log ^| find /C ":"

You are in a Win XP/2K window NOT under DOS (that doesn't exist at all in NT based systems) and For /F is a NT batch statement unknown to plain DOS.

Code %%x too NOT %x and at first sight more errors may be present.


0

Response Number 2
Name: sforsasi
Date: July 2, 2008 at 12:46:13 Pacific
Reply:

Thanks much!!! Adding the ^ symbol worked.

Yes. I'll use %%x when it is used in BAT file. But when used enter it from command prompt %x has to be used. Correct me if I am wrong.

Once again thanks much for your response.

Skumar.


0

Response Number 3
Name: IVO
Date: July 2, 2008 at 13:18:33 Pacific
Reply:

Why do you not code the following?

for /F %x in ('find /C "Output" ^< C:\test\sasi.log')do set XXX=%x
echo. %XXX%

as you are not looking for "regular expressions".


0

Response Number 4
Name: sforsasi
Date: July 2, 2008 at 15:12:30 Pacific
Reply:

I was under the impression that

find /C "Output" ^< C:\test\ftp_troy.log

will return the value in the below format. Since I needed only the count I went for the other option.

---------- C:\TEST\sasi.LOG: 2


But now I tried your options and it works good. Just one more question. Can it me used to search multiple words???

For Example

find /C "Output sasi" ^< C:\test\ftp_troy.log

Will it search for two individual words "Output" and "sasi" and retrieve the total count???

Skumar.


0

Response Number 5
Name: IVO
Date: July 3, 2008 at 06:40:30 Pacific
Reply:

set XXX=0
for %i in (Output sasi) do (
for /F %j in ('find /C "%i" ^< C:\test\ftp_troy.log') do set /A XXX+=%j
)
echo.%XXX%

As you can see the goal is achieved by embracing the parsing For /F statement with a plain For to trig a loop and performing arithmetics on the var XXX via the /A switch.

Type set /? to know more and beware dynamic variables in for statements (beyond this short note).


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: July 3, 2008 at 07:01:11 Pacific
Reply:

Hi IVO,

I love it


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 7
Name: klint
Date: July 3, 2008 at 07:23:12 Pacific
Reply:

If the file ftp_troy.log is very large, finding multiple strings using Ivo's code will read the same file multiple times and would therefore take longer than doing it using findstr, which only reads through the file once while searching for multiple strings. The only problem is findstr can't count the lines. Which is why Skumar piped it to "find", which can count.


0

Response Number 8
Name: IVO
Date: July 3, 2008 at 09:27:08 Pacific
Reply:

I want to point out that the findstr/find and find standalone methods lead to different results as they actually count the number of LINES holding the strings NOT the strings themselves.

So if you have the file

After Print Print
Print
After After

the findstr/find results in 3 while find outputs 4. The first counts lines holding After OR Print while the second takes into account the SUM of the lines with After and those with Print.

So better to clarify accurately what we want to count and how.


0

Response Number 9
Name: klint
Date: July 3, 2008 at 10:00:04 Pacific
Reply:

Good point Ivo. There's also the possibility of needing the count of individual occurrences of all the words, i.e. 6 in your example. Neither the "findstr/find" nor the "find" method gives us this.


0

Response Number 10
Name: sforsasi
Date: July 3, 2008 at 10:18:03 Pacific
Reply:

I really appreciate your explanation on this IVO. My requirement is to find either of words present in the file. So I would go for findstr.

And for Klint question beleive we need to introduce counter for calculating the individual occurrences of all words.

Skumar.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: | was unexpected at this time.

' was unexpected at this time error www.computing.net/answers/programming/-was-unexpected-at-this-time-error/18860.html

was unexpected at this time www.computing.net/answers/programming/was-unexpected-at-this-time/19145.html

0x0 was unexpected at this time www.computing.net/answers/programming/0x0-was-unexpected-at-this-time/19963.html