LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-20-2013, 10:14 AM   #1
gregmcc
Member
 
Registered: Mar 2007
Posts: 41

Rep: Reputation: 1
Awk Query


Actually 2 queries - just realised another problem with my code.

I have the below file:
Code:
1 (local) 10.1.2.3 up
2        10.2.3.4  down
I need to detect the last column where the status = down.
At first I had this:

Quote:
cat file | awk '{ if $5 == "down" ) { print "server down" } }'
I then realized in the above file the up/down status is not always in the 5th column, but the last column. How can I use awk for this?

When that's sorted then in place of printing an error I want to set a variable.

I had this but the variable is never set?

Quote:
stat=false
cat file | awk '{ if $5 == "down" ) { stat=true } }'

if [ $stat == true ]
then
echo "server down - send alert"
# sendmail command here
fi
 
Old 08-20-2013, 10:26 AM   #2
climaxnix
LQ Newbie
 
Registered: Nov 2004
Distribution: Debian GNU/Linux
Posts: 12

Rep: Reputation: 2
should this help?

Quote:
cat file.txt | awk 'BEGIN { FS=" " } { if ($3 == "up" || $3 =="down") print $3;else print $4 }'
 
1 members found this post helpful.
Old 08-20-2013, 10:46 AM   #3
gregmcc
Member
 
Registered: Mar 2007
Posts: 41

Original Poster
Rep: Reputation: 1
Thanks - I didnt know you could use if/elses in awk.

Now just need to solve the variable part.
 
Old 08-20-2013, 10:59 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Firstly, awk reads files just fine so cat is completely useless in this case.

As for your issues, my question back would be, is this part of a much larger script or is the intent to simply send an email?
I ask as awk could also handle that part and so the whole thing could be awk code.

Anyhoo, do you care if the status is up?
Code:
stat=$(awk 'END{if($NF == "down")print "true"}' file.txt)
The other interesting point here is that 'true' and 'false' can actually be tested directly by 'if', so you could simply do:
Code:
if $stat
 
Old 08-20-2013, 11:13 AM   #5
gregmcc
Member
 
Registered: Mar 2007
Posts: 41

Original Poster
Rep: Reputation: 1
Its actually part of a larger script. I'm only really concerned if the status is down, as it could also say up or standby.
I was trying to simplify things saying the input comes from a file, but in actual fact it comes from the output of a command. (cpquery)

So running ./cpquery gives the output as above. Should the line above now be something like:

Quote:
stat=$(`cpquery | awk 'END{if($NF == "down")print "true"}'`)
 
Old 08-20-2013, 12:18 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Yes to all except the extra quotes / backticks you placed in the line
 
Old 08-21-2013, 03:30 AM   #7
gregmcc
Member
 
Registered: Mar 2007
Posts: 41

Original Poster
Rep: Reputation: 1
Ok so now I have this:

Code:
stat=$(cpquery | awk 'END{if($NF == "down")print "true"}')
I get

Code:
./check.sh: [: ==: unary operator expected
 
Old 08-21-2013, 03:39 AM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by gregmcc View Post
Ok so now I have this:

Code:
stat=$(cpquery | awk 'END{if($NF == "down")print "true"}')
I get

Code:
./check.sh: [: ==: unary operator expected
need the rest of your script your stat=$(..) is 'fine'

to help diagnose, add
Code:
#!/bin/bash
set -evx
at the top
https://www.gnu.org/software/bash/ma...t-Builtin.html
 
1 members found this post helpful.
Old 08-21-2013, 03:42 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by gregmcc View Post
Ok so now I have this:

Code:
stat=$(cpquery | awk 'END{if($NF == "down")print "true"}')
I get

Code:
./check.sh: [: ==: unary operator expected
I'm fairly sure that the error shown is not generated by the stat=... command, but rather by something else in the script.

Code:
$ cpquery 
1 (local) 10.1.2.3 up
2        10.2.3.4  down

$ stat=$(cpquery | awk 'END{if($NF == "down")print "true"}')
$ echo $stat
true
EDIT: Too slow........
 
1 members found this post helpful.
Old 08-21-2013, 03:45 AM   #10
gregmcc
Member
 
Registered: Mar 2007
Posts: 41

Original Poster
Rep: Reputation: 1
Aghh - yes you are right. I had a incorrect if statement below that line.

All working now - thanks a ton.
 
Old 08-21-2013, 09:55 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Remember to mark as SOLVED once you have a working solution.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk query jkeertir Linux - Newbie 3 01-17-2014 05:12 AM
Query in awk/bash bioinfo08 Programming 2 01-09-2013 11:17 AM
[SOLVED] Awk query shivaa Linux - Newbie 4 12-01-2012 06:53 AM
awk query jkeertir Linux - Networking 3 04-07-2008 09:07 AM
awk program. a little query! indiancosmonaut Programming 7 07-05-2007 10:48 AM


All times are GMT -5. The time now is 08:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration