1828036 Members
1612 Online
109973 Solutions
New Discussion

awk and else statements

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

awk and else statements

Hi all,

I have created an awk statement that works well:

for i in `etc etc`
do
|awk '/Virtual I\/O Bus/ {line=line+1};END{if(line>1) print serv": virtual server"}'
done

however how can I use else in this statement - the following does not work:

|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"}'

all servers in the list come back with

serv: not a virtual server

any help would be great

Thanks

Chris
hello
15 REPLIES 15
James R. Ferguson
Acclaimed Contributor
Solution

Re: awk and else statements

Hi Chris:

|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"}}'

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: awk and else statements

First, you don't need your line variable because awk has the built-in NR variable.

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.

If it ain't broke, I can fix that.
spex
Honored Contributor

Re: awk and else statements

Hi Chris,

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
Sandman!
Honored Contributor

Re: awk and else statements

Either try parenthesizing the if else or Most likely it is the semi-colon before the END that maybe causing erroneous results so try removing it and see if that fixes it or convert it into a multi-line awk construct:

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"}'
Dennis Handly
Acclaimed Contributor

Re: awk and else statements

I'm not sure whats wrong. As SPEX says, it works for me if the input has multiple instances of that string.

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:
$ |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"
}
}'

>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. ;-)
A. Clay Stephenson
Acclaimed Contributor

Re: awk and else statements

You are correct; I missed the first single quote in the second example. I read all that -v serv=$i/Virtual ... stuff ass the variable actually being passed if so that the line count was unconditional.
If it ain't broke, I can fix that.
Hein van den Heuvel
Honored Contributor

Re: awk and else statements

Like for the rest, it works for me.

I guess the is not delivering exactly what you expect.

Hints... try something simpler

|awk '/Virtual/{line++; print line, $0}'

next:

|awk -v serv=$i '/Virtual I\/O Bus/ {line=line+1};END{ print serv, line}'

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.
Peter Nikitka
Honored Contributor

Re: awk and else statements

Hi,

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
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Sandman!
Honored Contributor

Re: awk and else statements

What should be the output of "" if "serv is a virtual server" should it output any lines that have "Virtual I/O Bus" or not?

~thanks
Dennis Handly
Acclaimed Contributor

Re: awk and else statements

>Sandman: What should be the output of "" if "serv is a virtual server" should it output any lines that have "Virtual I/O Bus" or not?

I assumed that it outputs more than one. And with that assumption, my awk fragment worked.
Sandman!
Honored Contributor

Re: awk and else statements

So does my awk script Dennis but I wanted to clarify whether the author of the thread had made a typo in... if(line>1) ...where the intention was... if(line>0).

~cheers
lawrenzo_1
Super Advisor

Re: awk and else statements

ok thanks all,

looks like I forgot {} for the else statement.

will add this to my never ending notes.

Chris
hello
lawrenzo_1
Super Advisor

Re: awk and else statements

Thanks again all,

sandman you are correct line should be >0 and not >1.

all examples will help in the future.

Cheers

Chris.
hello
Dennis Handly
Acclaimed Contributor

Re: awk and else statements

>line should be >0 and not >1.

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:
| grep -q "Virtual I/O Bus"
if [ $? -eq 0 ]; then
echo "$i: virtual server"
else
echo "$i: not a virtual server"
fi
lawrenzo_1
Super Advisor

Re: awk and else statements

Thanks Dennis,

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
hello