LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 09:14 AM   #1
gregmcc
Member
 
Registered: Mar 2007
Distribution: opensuse, ubuntu, debian
Posts: 43

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, 09: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, 09:46 AM   #3
gregmcc
Member
 
Registered: Mar 2007
Distribution: opensuse, ubuntu, debian
Posts: 43

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, 09:59 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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, 10:13 AM   #5
gregmcc
Member
 
Registered: Mar 2007
Distribution: opensuse, ubuntu, debian
Posts: 43

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, 11:18 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Yes to all except the extra quotes / backticks you placed in the line
 
Old 08-21-2013, 02:30 AM   #7
gregmcc
Member
 
Registered: Mar 2007
Distribution: opensuse, ubuntu, debian
Posts: 43

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, 02:39 AM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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, 02:42 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 02:45 AM   #10
gregmcc
Member
 
Registered: Mar 2007
Distribution: opensuse, ubuntu, debian
Posts: 43

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, 08:55 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

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


Reply



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 04:12 AM
Query in awk/bash bioinfo08 Programming 2 01-09-2013 10:17 AM
[SOLVED] Awk query shivaa Linux - Newbie 4 12-01-2012 05:53 AM
awk query jkeertir Linux - Networking 3 04-07-2008 08:07 AM
awk program. a little query! indiancosmonaut Programming 7 07-05-2007 09:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:26 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
Open Source Consulting | Domain Registration