# Conditional Column Value

**URL:** <https://community.unix.com/t/conditional-column-value/374076>\
**Category:** Shell Programming and Scripting\
**Created:** [November 4, 2018, 2:52pm UTC](https://community.unix.com/t/conditional-column-value/374076 "2018-11-04T14:52:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Axleuk](https://community.unix.com/user_avatar/community.unix.com/axleuk/32/10067_2.png) [@Axleuk](https://community.unix.com/u/Axleuk)\
**Post date:** [November 4, 2018, 2:52pm UTC](https://community.unix.com/t/conditional-column-value/374076/1 "2018-11-04T14:52:25Z")

</div>

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:

```nohighlight
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:

```nohighlight
# 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.

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_22734026db12803ebb3e059622ac3534.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [November 4, 2018, 4:33pm UTC](https://community.unix.com/t/conditional-column-value/374076/2 "2018-11-04T16:33:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Axleuk](https://community.unix.com/user_avatar/community.unix.com/axleuk/32/10067_2.png) [@Axleuk](https://community.unix.com/u/Axleuk)\
**Post date:** [November 4, 2018, 4:52pm UTC](https://community.unix.com/t/conditional-column-value/374076/3 "2018-11-04T16:52:20Z")

</div>

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:

```nohighlight
#!/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:

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_22734026db12803ebb3e059622ac3534.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [November 4, 2018, 5:11pm UTC](https://community.unix.com/t/conditional-column-value/374076/4 "2018-11-04T17:11:00Z")

</div>

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

```nohighlight
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" )}'

```

---

<div class="post-metadata">

**Author:** ![Axleuk](https://community.unix.com/user_avatar/community.unix.com/axleuk/32/10067_2.png) [@Axleuk](https://community.unix.com/u/Axleuk)\
**Post date:** [November 4, 2018, 5:18pm UTC](https://community.unix.com/t/conditional-column-value/374076/5 "2018-11-04T17:18:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_22734026db12803ebb3e059622ac3534.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [November 5, 2018, 7:14am UTC](https://community.unix.com/t/conditional-column-value/374076/6 "2018-11-05T07:14:21Z")

</div>

```nohighlight
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.
