Pulling more text?

B

Ben Samuals

I need to pull a couple of lines from the following text (it is actually in
a file, that is ouput from uptime.exe). I need the line beginning with
"Uptime Report for:" then the line listing Current system uptime. Also, I
can use for with "delims=:, " but how do I get both lines in the output?

\\server1
Current System Uptime: 89 day(s), 1 hour(s), 46 minute(s), 23 second(s)

\\server2
Current System Uptime: 89 day(s), 1 hour(s), 46 minute(s), 23 second(s)

\\server3
Current System Uptime: 89 day(s), 1 hour(s), 46 minute(s), 23 second(s)

\\server4
Current System Uptime: 89 day(s), 1 hour(s), 46 minute(s), 23 second(s).....


This is the output from the uptime.ext (contained in a file) OUTPUT below:

Uptime Report for: \\server1

Current OS: Microsoft Windows 2000, Service Pack 4, Multiprocessor Free.
Time Zone: Eastern Standard Time

System Events as of 2/12/2004 1:28:35 PM:

Date: Time: Event: Comment:
---------- ----------- -------------------
-----------------------------------

Current System Uptime: 89 day(s), 1 hour(s), 46 minute(s), 23 second(s)


------------------------------------------------------------------------
--------

Since 1/1/2004: (Last 42 Days)

System Availability: 100.0000%
Total Uptime: 42d 13h:28m:35s
Total Downtime: 0d 0h:0m:0s
Total Reboots: 0
Total Bluescreens: 0

Notes:
The remote system may be a member of a cluster.
If the remote system is a member of a cluster the reported availability
information applies only to the currently active node, NOT to the
cluster
as a whole.
Uptime Report for: \\server2

Current OS: Microsoft Windows 2000, Service Pack 4, Uniprocessor Free.
Time Zone: Eastern Standard Time

System Events as of 2/12/2004 1:28:43 PM:

Date: Time: Event: Comment:
---------- ----------- -------------------
 
P

Phil Robyn [MVP]

Ben said:
I need to pull a couple of lines from the following text (it is actually in
a file, that is ouput from uptime.exe). I need the line beginning with
"Uptime Report for:" then the line listing Current system uptime. Also, I
can use for with "delims=:, " but how do I get both lines in the output?

\\server1
Current System Uptime: 89 day(s), 1 hour(s), 46 minute(s), 23 second(s)

\\server2
Current System Uptime: 89 day(s), 1 hour(s), 46 minute(s), 23 second(s)

\\server3
Current System Uptime: 89 day(s), 1 hour(s), 46 minute(s), 23 second(s)

\\server4
Current System Uptime: 89 day(s), 1 hour(s), 46 minute(s), 23 second(s).....


This is the output from the uptime.ext (contained in a file) OUTPUT below:

Uptime Report for: \\server1

Current OS: Microsoft Windows 2000, Service Pack 4, Multiprocessor Free.
Time Zone: Eastern Standard Time

System Events as of 2/12/2004 1:28:35 PM:

Date: Time: Event: Comment:
---------- ----------- -------------------
-----------------------------------

Current System Uptime: 89 day(s), 1 hour(s), 46 minute(s), 23 second(s)


------------------------------------------------------------------------
--------

Since 1/1/2004: (Last 42 Days)

System Availability: 100.0000%
Total Uptime: 42d 13h:28m:35s
Total Downtime: 0d 0h:0m:0s
Total Reboots: 0
Total Bluescreens: 0

Notes:
The remote system may be a member of a cluster.
If the remote system is a member of a cluster the reported availability
information applies only to the currently active node, NOT to the
cluster
as a whole.
Uptime Report for: \\server2

Current OS: Microsoft Windows 2000, Service Pack 4, Uniprocessor Free.
Time Zone: Eastern Standard Time

System Events as of 2/12/2004 1:28:43 PM:

Date: Time: Event: Comment:
---------- ----------- -------------------
-----------------------------------
1/24/2004 11:14:11 AM Shutdown
1/24/2004 11:23:12 AM Boot Prior downtime:0d 0h:9m:1s

Current System Uptime: 19 day(s), 2 hour(s), 6 minute(s), 10 second(s)

- - - - - - - - - - begin screen capture Win2000 - - - - - - - - - -
c:\cmd>demo\UptimeReport
\\server1
Current System Uptime: 89 day(s), 1 hour(s), 46 minute(s), 23 second(s)

\\server2
Current System Uptime: 19 day(s), 2 hour(s), 6 minute(s), 10 second(s)


c:\cmd>rlist demo\UptimeReport.cmd
=====begin c:\cmd\demo\UptimeReport.cmd ====================
01. @echo off
02. setlocal
03. for /f "tokens=*" %%a in (
04. 'findstr /i "uptime" c:\temp\file.txt
05. ^| findstr /v " Total"'
06. ) do call :format %%a
07. goto :EOF
08. :format
09. set rec=%*
10. set new=
11. if "%rec:~0,6%" equ "Uptime" set rec=%rec:~19%&set new=yes
12. echo/%rec%
13. if not defined new echo/
14. goto :EOF
=====end c:\cmd\demo\UptimeReport.cmd ====================
- - - - - - - - - - end screen capture Win2000 - - - - - - - - - -
 
B

Ben Samuals

Phil, a few questions on your approach. It works great by the way, it is
just hard for me to understand the formatting. What does the "format:" do?

Then these lines, if you could explain these I would appreciate it...:

set rec=%*
set new=
if "%rec:~0,6%" equ "Uptime" set rec=%rec:~19%&set new=yes
echo/%rec%
if not defined new echo/

Thx, Phil
 
M

Matthias Tacke

Phil Robyn said:
- - - - - - - - - - begin screen capture Win2000 - - - - - - - - - -
c:\cmd>demo\UptimeReport
\\server1
Current System Uptime: 89 day(s), 1 hour(s), 46 minute(s), 23 second(s)

\\server2
Current System Uptime: 19 day(s), 2 hour(s), 6 minute(s), 10 second(s)


c:\cmd>rlist demo\UptimeReport.cmd
=====begin c:\cmd\demo\UptimeReport.cmd ====================
01. @echo off
02. setlocal
03. for /f "tokens=*" %%a in (
04. 'findstr /i "uptime" c:\temp\file.txt
05. ^| findstr /v " Total"'
06. ) do call :format %%a
07. goto :EOF
08. :format
09. set rec=%*
10. set new=
11. if "%rec:~0,6%" equ "Uptime" set rec=%rec:~19%&set new=yes
12. echo/%rec%
13. if not defined new echo/
14. goto :EOF
=====end c:\cmd\demo\UptimeReport.cmd ====================
- - - - - - - - - - end screen capture Win2000 - - - - - - - - - -
You bet me in time Phil, my one findstr was a bit simpler
findstr "\\ (s)"
with otherwise same filter effect.
 
P

Phil Robyn [MVP]

Ben said:
Phil, a few questions on your approach. It works great by the way, it is
just hard for me to understand the formatting. What does the "format:" do?

Then these lines, if you could explain these I would appreciate it...:

set rec=%*

All arguments ('%*') passed to the subroutine ':format' are assigned to the
environment variable 'rec'.

The environment variable 'new' is set to no value.
if "%rec:~0,6%" equ "Uptime" set rec=%rec:~19%&set new=yes

If the first six characters of environment variable 'rec' are equal to
'Uptime', then set 'rec' to be a substring of itself starting at the 20th
character. So if the value of 'rec' is 'Uptime Report for: \\server1',

....+....1....+....2....+....3
rec = Uptime Report for: \\server1

you can see that the first six characters are equal to 'Uptime', and thus
the new value of 'rec' becomes '\\server1'.
echo/%rec%

Display the contents of environment variable 'rec' on the screen.
if not defined new echo/

If the environment variable 'new' has no value, then echo a blank line to the
screen.

Try commenting out the first line in the batch file by inserting two colons,
e.g.,

::mad:echo off

and then run the batch file and look at the output. This may help you in
gaining a better understanding what it is doing and how it works.
 
M

Matthias Tacke

Ben Samuals said:
Phil, a few questions on your approach. It works great by the way, it is
just hard for me to understand the formatting. What does the "format:" do?

Then these lines, if you could explain these I would appreciate it...:

set rec=%*
set new=
if equ "Uptime" set rec=%rec:~19%&set new=yes
echo/%rec%
if not defined new echo/

Hello Ben,

for /F "tokens=*" takes the whole line from input into %%a
the called sub receives this as multiple arguments, %* puts all passed
args into rec opposed to %1, %2 etc.

A different possible approach is quoting call :format "%%a"
and dequoting with set rec=%~1

see for details on "%rec:~0,6%" in a cmd window:
set /?
for /?

FWIW my version requiring w2k/XP :
:: UptimeRep.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off & setlocal EnableExtensions EnableDelayedExpansion
for /F "tokens=* delims=:" %%A in ('findstr "\\ (s)" uptime.txt') do (
set Line=%%A & set Line=!Line:*: \=\! & echo/!Line! )
:: UptimeRep.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::::
 
M

Matthias Tacke

Ben Samuals said:
Matthias, can you share it? I need all of the help I can get...
I hope you already found it in the other posting :)

The change in Phil's would simply be to exchange his 2 piped findstr
with my short one. Findstr allows multiple search strings enclosed by
the quotes. Unique elements of your desired lines are "\\" and "(s)" so
it was easy to combine them.
04. 'findstr "\\ (s)" c:\temp\file.txt'
05. ::removed
In my version there was still a delims=: from a previous try which
didn't harm but had no function. So again a corrected version:

:: UptimeRep.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off & setlocal EnableExtensions EnableDelayedExpansion
for /F "tokens=*" %%A in ('findstr "\\ (s)" uptime.txt') do (
set Line=%%A & set Line=!Line:*: \=\! & echo/!Line! )
:: UptimeRep.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::::

The text preceeding the server name is removed by the :
set Line=!Line:*: \=\!
the star representing all leading text up to ": \" and replaced by a
single "\".
Enabledelayedexpansion and the ! are requiring the os's w2k or xp

HTH
 

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