Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Awk with if statements

Status
Not open for further replies.

Blivo

Technical User
May 2, 2005
9
US
Ok I want to awk certain parts of a file based on whether or not they have certain words. I'll post an example of my file.

**********************
Server1
**********************

Current System Uptime " 6 day(s), 21 hour(s), 13 minute(s), 35 second(s)"


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

Since 9/22/2004

Total Reboots 12
Mean Time Between Reboots 16.51 days
Total Bluescreens 0


Notes
There is insufficient data in the event log to calculate
system availability. Please see UPTIME /help for more detail.

**********************
Server2
**********************

Current System Uptime " 254 day(s), 0 hour(s), 32 minute(s), 9 second(s)"


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

Since 4/14/2003

System Availability 99.97%
Total Uptime 724d 21h 51m 14s
Total Downtime 0d 4h 24m 59s
Total Reboots 22
Mean Time Between Reboots 32.96 days
Total Bluescreens 0

That's part of my file. First I awk between Server1 and Blue screens to return all those lines and put them in a file server1.txt. This is where I'm having the trouble. I need it to first do a command like:

Code:
awk '/Availability/,/Bluescreens/' server1.txt
and if it doesn't find Availability it will do this:
Code:
awk '/Reboots/,/Bluescreens/' server1.txt

I'm sorry if this is a simple question I just started using unix utilities and am trying to teach myself.

Thanks
 
Code:
awk '/Availability|Reboots/,/Bluescreens/'

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

Denis
 
Oh, I don't know if it matters but I'm doing all of this in DOS.

When I type:
Code:
awk '/Availability|Reboots/,/Bluescreens/' file.txt

I get an error message saying:

'Reboots' is not recognized as an internal or external command, operable program or batch file.
awk : not enough arugments

Any suggestions?
 
Try
awk "/Availability|Reboots/,/Bluescreens/" file.txt
The trick is the quoting - Windows doesn't understand ''.
The reason you get that error message is that | is used for redirecting output from one command to another, so what Windows sees is:
Run "awk '/Availability" and send result to "Reboots/,/Bluescreens/' file.txt"
The same goes for < and > - without enclosing them in "" Windows will interpret them as a redirection from/to a file.

- Nis Donatzsky
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top