Conditional Column Value


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Conditional Column Value

Hi Folks,

I'm trying tog ain further experience with shell programming and have set my a small goal of writing a little filesystem monitoring script. So far my output is as follows:

Code:
PACMYDB03
Filesystem                       Size    Used    Avail   Use%    Status
/usr/local/mysql/data            20G     220M    19G     2
/usr/local/mysql/binlogs         15G     52M     14G     1
/usr/local/mysql/backups         25G     44M     24G     1

I'm now trying to figure a way of doing a conditional comparison ( $5 -ge 1 ) on the 'use%' column and based on the result filling in the 'Status' column with either "OK" or "Space Required"

Here is a snippet of code:

Code:
# Get The Filesystem Stats
echo ""
hostname
echo $HEAD | awk '{printf("%-30s\t %s\t %s\t %s\t %s\t %s\t \n", $1, $2, $3, $4, $5, $6)}'
echo $DATA > $TMP
echo $BINLOGS >> $TMP
echo $BACKUPS >> $TMP
cat $TMP | sed 's/%//g' | awk '{printf("%-30s\t %s\t %s\t %s\t %s\t %s\t \n", $5, $1, $2, $3, $4, $6)}'
rm $TMP
exit 0

Any help is appreciated.
# 2  
Welcome to the forum.

Please become accustomed to provide decent context info of your problem.

It is always helpful to carefully and detailedly phrase a request, and to support it with system info like OS and shell, related environment (variables, directory structures, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two including your own attempts at a solution, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.

In this special case:
- Structure and contents of input data missing (e.g. the HEAD variable, and others).
- where to insert the conditional comparison? BTW, why not use awk for the entire task, now that you use it anyhow?


Aside: thanks for using CODE tags.
# 3  
OK, let me try again. I appreciate my code thus far mat be sloppy, forgive me, but as I previously stated, this is something I am learning and hoping to improve upon.

This is my code so far:

Code:
#!/bin/bash

# Fixed Variables
TMP=/tmp/tmpx
HEAD="Filesystem Size Used Avail Use% Status"
DATA=$(df -h | gawk '$5 ~ "/data"')
BINLOGS=$(df -h | gawk '$5 ~ "/binlogs"')
BACKUPS=$(df -h | gawk '$5 ~ "/backup*"')


# Get The Filesystem Stats
echo ""
hostname
echo $HEAD | awk '{printf("%-30s\t %s\t %s\t %s\t %s\t %s\t \n", $1, $2, $3, $4, $5, $6)}'
echo $DATA > $TMP
echo $BINLOGS >> $TMP
echo $BACKUPS >> $TMP
cat $TMP | sed 's/%//g' | awk '{printf("%-30s\t %s\t %s\t %s\t %s\t %s\t \n", $5, $1, $2, $3, $4, $6)}'
rm $TMP
exit 0

And it produces this output:

Code:
PACMYDB03
Filesystem                       Size    Used    Avail   Use%    Status
/usr/local/mysql/data            20G     220M    19G     2
/usr/local/mysql/binlogs         15G     52M     14G     1
/usr/local/mysql/backups         25G     44M     24G     1

I want to be able to add a value of "OK" or "Space Required" under the 'Status' colum of each line based on a result of a conditional test of column $4 (something along of the lines of {if ($4 -ge 1){$6="OK"} or {if ($4 -ge 2){$6="Space Required"}.

Am I making sense?

I'm using Red Hat 6

Many Thanks
# 4  
The way you approach your problem is not quite evident to me. You may do this as an exercise in the frayed way shown, but how about a "monlithic" approach, using one single awk script only, in lieu of expensively running 11 external commands? Something like


Code:
df | awk 'NR == 1 || /\/(data|binlogs|backup)/ {printf("%-30s\t %s\t %s\t %s\t %s\t %s\t \n", $6, $2, $3, $4, $5, NR==1?"Status":($5+0 > 80)?"Space required":"OK" )}'

# 5  
Thanks.

I managed to figure a way myself in the end. However your approach is much more elegant and this is clearly something i need to work on and understand more. Would you mind breaking down your code to me.

Appreciate your time.
# 6  
Code:
awk '
NR == 1                                                                 # On first line
||                                                                      # OR
/\/(data|binlogs|backup)/                                               # lines with respective mount points
{printf("%-30s\t %s\t %s\t %s\t %s\t %s\t \n", $6, $2, $3, $4, $5,      # print fields 2 - 6, AND, for field 7:
NR==1                                                                   # conditional assignment operator: 1. line
?"Status"                                                               # TRUE branch: print header field
:                                                                       # FALSE branch: another ...
($5+0 > 80)                                                             # cond.assg.op.: if percent > 80%
?"Space required"                                                       # TRUE branch
:"OK"                                                                   # FALSE branch
)}'

Please post the solution you found for others to learn, and for optional discussion.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Gadgets
Difficulty: Easy
The communication protocol NFC stands for Near-Field Control.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX - 2 tab delimited files, conditional column extraction

Please know that I am very new to unix and trying to learn 'on the job'. I'm only manipulating large tab-delimited files (millions of rows), but I'm stuck and don't know how to proceed with the following. Hoping for some friendly advice :) I have 2 tab-delimited files - with differing column &... (10 Replies)
Discussion started by: GTed
10 Replies

2. Shell Programming and Scripting

Filter on one column and then perform conditional calculations on another column with a Linux script

Hi, I have a file (stats.txt) with columns like in the example below. Destination IP address, timestamp, TCP packet sequence number and packet length. destIP time seqNo packetLength 1.2.3.4 0.01 123 500 1.2.3.5 0.03 44 1500 1.3.2.5 0.08 44 1500 1.2.3.4 0.44... (12 Replies)
Discussion started by: Zooma
12 Replies

3. Shell Programming and Scripting

Difference of the same column when two other column matches and one column differs less than 1 hour

This is my input file : # cat list 20130430121600, cucm, location,76,2 20130430121600,cucm1,location1,76,4 20130430122000,cucm,location,80,8 20130430122000,cucm1,location1,90,8 20130430140000,cucm1,location1,87,11 20130430140000, cucm,location,67,9 This is the required output ... (1 Reply)
Discussion started by: Lakshmikumari
1 Replies

4. Shell Programming and Scripting

awk , conditional involving line and column

Dear All, I indeed your help for managing resarch data file. for example I have, data1.txt : type of atoms z vz Si 34 54 O 20 56 H 14 13 Si 40 17 O ... (11 Replies)
Discussion started by: ariesto
11 Replies

5. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

6. Shell Programming and Scripting

Replace a column with a value conditional on a value in col1

Hi, Perhaps a rather simple problem...? I have data that looks like this. BPC0013 ANNUL_49610 0 0 1 1 BPC0014 ANNUL_49642 0 0 2 1 BPC0015 ANNUL_49580 0 0 1 1 BPC0016 ANNUL_49596 0 0 2 1 BPC0017 VULGO_49612 0 0 1 1 BPC0018 ANNUL_49628 0 0 1 1 BPC0019 ANNUL_49692 0 0 2 1 170291_HMG... (4 Replies)
Discussion started by: genehunter
4 Replies

7. Shell Programming and Scripting

Conditional aggregation and print of a column in file

Hi My input file looks like field1 field2 field3 field4 field5 field1 field2 field3 field4 field5 field1 field2 field3 field4 field5 :::::::::::: :::::::::::: There may be one space of multiple spaces between fields and no fields contains spaces in them. If field 1 to 4 are equal for... (3 Replies)
Discussion started by: bittoo
3 Replies

8. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

9. Shell Programming and Scripting

If conditional

Hi, I am new to unix and shell scripting.In my script,there is a line using the "if" conditional - if && ; then do something Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while... (12 Replies)
Discussion started by: abhinavsinha
12 Replies

10. UNIX for Dummies Questions & Answers

conditional

conditional is not wworking can any one figure out what goes wrong xx1=`$ORACLE_HOME/bin/sqlplus -s apps/ostgapps1 2>/dev/null << EOF WHENEVER SQLERROR EXIT 1 set head off feedback off ; WHENEVER SQLERROR EXIT SQL.SQLCODE; select count(*) from CMS_INVOICE_ALL... (2 Replies)
Discussion started by: u263066
2 Replies

Featured Tech Videos