learnnewinfy
learnnewinfy

Reputation: 23

How to find the maximum value for the field by ignoring the lines with characters using awk?

Since am newbie to the awk , please help me with your suggestions. I tried the below command to filter the maximum value and ignore the first & last lines from the sample text file separately. They work when I try them separately.

My query:

I need to ignore the last line and first few lines and from the file and then need to take the maximum value for the field 7 using awk .

I also need to ignore the lines with the characters . Can anyone suggest me the possibilities two use both the commands together and get the required output.

Sample file:

Linux 3.10.0-957.5.1.el7.x86_64 (j051s784)      11/24/2020      _x86_64_        (8 CPU)

 

12:00:02 AM kbmemfree kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty

12:10:01 AM   4430568  61359128     93.27   1271144  27094976  66771548     33.04  39005492  16343196      1348

12:20:01 AM   4423380  61366316     93.28   1271416  27102292  66769396     33.04  39012312  16344668      1152

12:30:04 AM   4406324  61383372     93.30   1271700  27108332  66821724     33.06  39028320  16343668      2084

12:40:01 AM   4404100  61385596     93.31   1271940  27107724  66799412     33.05  39031244  16344532      1044

06:30:04 PM kbmemfree kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty

07:20:01 PM   3754904  62034792     94.29   1306112  27555948  66658632     32.98  39532204  16476848      2156

Average:      4013043  61776653     93.90   1293268  27368986  66755606     33.03  39329729  16427160      2005

Commands used:

cat testfile | awk '{print $7}'  | head -n -1 | tail -n+7
awk 'BEGIN{a=   0}{if ($7>0+a) a=$7} END{print a}' testfile

Expected output:

Maximum value for the column 7 by excluding the lines wherever alphabet character is available

Upvotes: 2

Views: 240

Answers (2)

Raman Sailopal
Raman Sailopal

Reputation: 12917

awk '$7 ~ ^[[:digit:]]+$/ && $1 != "Average:" { 
                      max[$7]="" 
              } 
     END      { 
                      PROCINFO["sorted_in"]="@ind_num_asc";
                      for (i in max) { 
                                       maxtot=i 
                                     } 
                      print maxtot 
               }' file    

One liner:

awk '$7 ~ /^[[:digit:]]+$/ && $1 != "Average:" { max[$7]="" } END { PROCINFO["sorted_in"]="@ind_num_asc";for (i in max) { maxtot=i } print maxtot }' file

Using GNU awk, search for lines where field 7 is only numbers and field one is not "Average:" In these instances, create an array entry with field 7 as the index. At the end, sort the array in index ascending number order. Loop through the array setting a maxtot variable. The last entry in the max array will be the highest kbcached and so print maxtot

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133770

1st solution(Generic solution): Adding one Generic solution here, where sending field name to an awk variable(which we want to look for for maximum value) it will automatically find out its field number from very first line and will work accordingly. Considering that your first line has that field name which you want to look for.

awk -v var="kbcached" '
FNR==1{
  for(i=1;i<=NF;i++){
   if($i==var){ field=i }
  }
  next
}
/kbmemused/{
  next
}
{
  if($2!~/^[AP]M$/){
    val=$(field-1)
  }
  else{
    val=$field
  }
}
{
  max=(max>val?max:val)
  val=""
}
END{
  print "Maximum value is:" max
}
' Input_file


2nd solution(As per shown samples only): Could you please try following, based on your shown samples only. I am assuming you want the field value of column kbcached.

awk '
/kbmemfree/{
  next
}
{
  if($2!~/^[AP]M$/){
    val=$6
  }
  else{
    val=$7
  }
}
{
  max=(max>val?max:val)
  val=""
}
END{
  print "Maximum value is:" max
}
'  Input_file

Upvotes: 2

Related Questions