- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- awk and else statements
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2007 03:55 AM
05-18-2007 03:55 AM
I have created an awk statement that works well:
for i in `etc etc`
do
done
however how can I use else in this statement - the following does not work:
all servers in the list come back with
serv: not a virtual server
any help would be great
Thanks
Chris
Solved! Go to Solution.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2007 04:08 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2007 04:17 AM
05-18-2007 04:17 AM
Re: awk and else statements
Now try this:
ls | awk 'END { if (NR > 1) {print "More"} else {print "Less than two"} }'
echo "One thing" | awk 'END { if (NR > 1) {print "More"} else {print "Less than two"} }'
I'm just using ls and echo to supply more than one line and one line respectively.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2007 04:20 AM
05-18-2007 04:20 AM
Re: awk and else statements
Both cases of your if-else statement work properly--unaltered--on my system. Make sure you have specified the correct search pattern and look out for any unexpected input.
PCS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2007 04:54 AM
05-18-2007 04:54 AM
Re: awk and else statements
awk -v serv=$i '/Virtual I\/O Bus/ {line=line+1}END{if(line>1){print serv": virtual server"}else print serv": not a virtual server"}'
...or...
awk -v serv=$i '/Virtual I\/O Bus/ {line=line+1}
END {if(line>1)
print serv": virtual server";else print serv": not a virtual server"}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2007 06:24 PM
05-18-2007 06:24 PM
Re: awk and else statements
You may want to remove that ";: as JRF says?
You may also want to use a better awk formating style and not use just one line:
$
/Virtual I\/O Bus/ {line=line+1}
END {
if (line>1) {
print serv": virtual server"
} else {
print serv": not a virtual server"
}
}'
>Clay: First, you don't need your line variable because awk has the built-in NR variable.
Sure it does. line is counting matches, not lines. Of course if the name was called "matches", Clay would be happier. ;-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2007 02:43 AM
05-19-2007 02:43 AM
Re: awk and else statements
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2007 07:40 AM
05-19-2007 07:40 AM
Re: awk and else statements
I guess the
Hints... try something simpler
next:
Those \escaped characters are always tricky.
If there is no risk for false positives, then just replace by a '.' for 'any' character.
Consider this:
$ awk -v serv=$i '/Virtual I.O Bus/{line++} END { not=(line<2)? ":not a" : ":"; print serv not "vi
rtual server"}'
Here I conditionally modify a variable in the print line instead of conditionally selecting the 'right' print line.
Why? Because now the rest of the print statement will be exactly the same. Equally broken or equally correct. Easier to maintain!
Cheers,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2007 04:16 AM
05-21-2007 04:16 AM
Re: awk and else statements
Hein's remark is important!
>>
Those \escaped characters are always tricky.
If there is no risk for false positives, then just replace by a '.' for 'any' character.
<<
But don't use '.' in that case but '[/]' like this:
awk '/Virtual I[/]O Bus/ {..
This can always be used as a save 'esacpe' mechanism.
mfG Peter
- Tags:
- quoting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2007 04:55 AM
05-21-2007 04:55 AM
Re: awk and else statements
~thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2007 05:39 AM
05-21-2007 05:39 AM
Re: awk and else statements
I assumed that it outputs more than one. And with that assumption, my awk fragment worked.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2007 07:23 AM
05-21-2007 07:23 AM
Re: awk and else statements
~cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2007 08:20 PM
05-21-2007 08:20 PM
Re: awk and else statements
looks like I forgot {} for the else statement.
will add this to my never ending notes.
Chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2007 08:28 PM
05-21-2007 08:28 PM
Re: awk and else statements
sandman you are correct line should be >0 and not >1.
all examples will help in the future.
Cheers
Chris.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2007 08:48 PM
05-21-2007 08:48 PM
Re: awk and else statements
If this is the case, there is no need to count them up. Just do:
/Virtual I\/O Bus/ { print serv ": virtual server"; exit}
END { print serv ": not a virtual server"}
You can also make it simpler by just using grep:
if [ $? -eq 0 ]; then
echo "$i: virtual server"
else
echo "$i: not a virtual server"
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2007 08:51 PM
05-21-2007 08:51 PM
Re: awk and else statements
I am pretty handy with shell scripting so I am attempting to learn awk / sed and later on perl ....
but thanks for the examples.
Chris