Using AWK in IF evaluation in KSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using AWK in IF evaluation in KSH
# 1  
Old 06-04-2010
Using AWK in IF evaluation in KSH

Hi -

I have an expression that evaluates to "Alive" or some other condition. e.g. if I run :-

Code:
awk -F \| '{gsub(/[[:space:]]*/,"",$4); print $4 }'

then the output is "Alive".

I want to be able to test this as the result may be some other condition other than "Alive". I have tried the following which doesn't work. Can someone tell me what is wrong please ?

Code:
if [[ `awk -F \| '{gsub(/[[:space:]]*/,"",$4); print $4 }'` != "Alive" ]]; then

Thanks in advance.
# 2  
Old 06-04-2010
The way your code is written it must read from stdin. I think we need to see a few lines above...

What do you mean 'does not work' - what is your input and expected output
# 3  
Old 06-04-2010
OK I have an input file (e.g. imput.txt) which looks something like :-

Code:
Server 1  |  Group 1  |   34586   |   Alive
Server 2  |  Group 1  |   92876   |   Alive
Server 1  |  Group 2  |   87321   |   Alive
Server 2  |  Group 2  |   18763   |   Alive


Then I do :-

Code:
while read line
do
   if [[ `awk -F \| '{gsub(/[[:space:]]*/,"",$4); print $4 }'` != "Alive" ]]; then
       print "Server :" `awk -F\| '{print $1}'` " is " `awk -F\| '{print $4}'`
   fi
done < input.txt

Now this doesn't work in that even though all servers are "Alive", the code still enters the if statement.

Thanks for your help.
# 4  
Old 06-04-2010
You can do the job with 1 awk command:
Code:
awk -F \| '{gsub(/[[:space:]]*/,"",$4)}
$4 != "Alive" {print "Server : " $1 " is " $4}
' input.txt

# 5  
Old 06-04-2010
Genius - thanks very much. Exactly what's needed.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Evaluation of test command

Could somebody please explain to me why and how the highlighted line(s) (?) of code puts the "test" evaluation into "result" and then to $enable_static ? Or does not ? I did comment out the original code and changed it to what I feel is less cryptic , but the "result" is still wrong =... (3 Replies)
Discussion started by: anne
3 Replies

2. Shell Programming and Scripting

Help understanding evaluation order

I have made a simple script to find all programs that use a tcp wrapper, it will supply a reasonable default for my system if none is given. After some digging I realized that the expansion operators pass their default return value single quoted (according to bash -x trace). I have wildcard... (2 Replies)
Discussion started by: phuongnguyen
2 Replies

3. AIX

Older versions of the XL C/C++ Evaluation for AIX 5.3

Hello, IBM offers a evaluation version of their XL C/C++ compiler. Unfortunatly v16.1 (from Developerworks) can not be installed on AIX 5.3 (I can not upgrade my old 32 Bit RS/6000 to a later version) and I have not found any older versions on the IBM and Developerworks pages. Is there a... (8 Replies)
Discussion started by: eh2scqw
8 Replies

4. Shell Programming and Scripting

Help understanding evaluation order

I have made a simple script to find all programs that use a tcp wrapper, it will supply a reasonable default for my system if none is given. After some digging I realized that the expansion operators pass their default return value single quoted (according to bash -x trace). I have wildcard... (0 Replies)
Discussion started by: Riker1204
0 Replies

5. Shell Programming and Scripting

ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded. It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Discussion started by: highnthemnts
1 Replies

6. Cybersecurity

iptables latency evaluation

Hello guys, I'm actually working on my master thesis which has for subject the evaluation of virtual firewall in a cloud environment. To do so, I installed my own cloud using OpenNebula (as a frontend) and Xen (as a Node) on two different machines. The Xen machine is my virtual firewall thanks... (2 Replies)
Discussion started by: Slaughterman
2 Replies

7. UNIX for Dummies Questions & Answers

cp command evaluation

Hi all! I'm writting one script to copy a file in various folders, but there are 2 things to validate. First that the folder where i'll be cpying exists, and second that i have permissions to copy the file in it. so far i have found the way to validate the folder exists, but when trying to... (6 Replies)
Discussion started by: feliperivera
6 Replies

8. Shell Programming and Scripting

AWK equation evaluation.

Hi, Is there a way to evaluate an equation contained in a string within an AWK script? For example: A = "(5*2)-1" (this equation is read from a file and varies line by line) In this example, I can't see any way to get an answer of 9 unless I do: cmd = "awk 'BEGIN{print "A"}'" cmd |... (3 Replies)
Discussion started by: srdgeo
3 Replies
Login or Register to Ask a Question