Shell Script if $2 is greater than 10 then echo $1

Hi,

I have a file output.txt

3258 14
32647 10
32649 10
32650 10
32651 10
32652 10
32653 10
32654 10
32655 10
32656 10
32515 09
32478 08
32555 08
35888 08

I am trying to write a script if $2 i.e. value in column 2 is greater than 10 then if should echo $1 i.e. value of column 1.

Please Help.

 
awk '$2<10{print $1}' input.txt

My Script is:

#/bin/bash
T=`date -d '1 hour ago' "+%H"`
echo $T
 
ps -aef|grep httpd|awk ' { print $2 , $5 } ' | cut -d ':' -f1 > /root/output.txt
 
for data in `awk '$2<$T {print $1}' /root/output.txt`
 
do
echo $data - has been killed
#kill $data
echo "--"
done

However it is ignoring condition and echoing all the values on $1.

change your two lines

1) combine the grep and awk
2) use while loop instead of for loop

 
ps -aef | awk '/httpd/{ print $2 , $5 } ' | cut -d ':' -f1 > /root/output.txt
awk -v t="$T" '$2<t {print $1}' /root/output.txt | while read data

1 Like

use awk with -v option

 ps -aef|grep httpd|awk ' { print $2 , $5 } ' | cut -d ':' -f1 | awk -vT=`date -d '1 hour ago' "+%H"` ' $2<T {print $1}'
1 Like