Awk script to find the smallest value from a column

I'm starting at AWK , so please help me get to know him. I have a text file called snd and its values

 1 0 141 1 2 223 1 3 250 1 4 280 

I want to print the whole row when the value of the third column is minimal

+4
source share
8 answers

This should do it:

 awk 'NR == 1 {line = $0; min = $3} NR > 1 && $3 < min {line = $0; min = $3} END{print line}' file.txt 

EDIT

What it is:

  • Remember the 1st row and its third field.
  • For other lines, if the third field is smaller than the one found so far, remember the line and its third field.
  • At the end of the script, print a line.

Note that the test NR > 1 may be skipped, since for the 1st line $3 < min will be false. If you know that the third column is always positive (not negative), you can also skip the test NR == 1 ... since the min value at the beginning of the script is zero.

EDIT2

This is shorter:

 awk 'NR == 1 || $3 < min {line = $0; min = $3}END{print line}' file.txt 
+16
source

You do not need awk to do what you want. Use sort

 sort -nk 3 file.txt | head -n 1 

Results:

 1 0 141 
+7
source

short answer for this:

 sort -k3,3n temp|head -1 

since you asked awk:

 awk '{if(min>$3||NR==1){min=$3;a[$3]=$0}}END{print a[min]}' your_file 

But I prefer a shorter option.

+3
source

I think sort is a great answer if for some reason you don't need awk logic to do this in a larger script, or you want to avoid extra pipes, or the goal of this question is to learn more about awk.

 $ awk 'NR==1{x=$3;line=$0} $3<x{line=$0} END{print line}' snd 

Broken into pieces, this is:

  • NR==1 {x=$3;line=$0} - In the first line, set the initial value for comparison and save the line.
  • $3<x{line=$0} - In each line, compare the third field with our stored value, and if the condition is true, save the line. (We could only run this run on NR>1 , but that doesn't matter.
  • END{print line} - At the end of our input, type any line that we saved.

You should read man awk to find out about any parts of this that don't make sense.

+1
source

To calculate the smallest value in any column, let the last column

awk '(FNR == 1) {a = $ NF} {a = $ NF <a? $ NF: a} END {print a} '

this will print only the smallest column value. In case the full line is needed it is better to use sorting, sort -r -n -t [separator] -k [column] [file name]

0
source

awk -F ";" '(NR == 1) {a = $ NF; b = $ 0} {a = $ NFa? B: $ 0} END {print b} 'filename

this will print the line with the smallest value that occurs first.

0
source

awk 'BEGIN {OFS = FS = ","} {if (a [$ 1]> $ 2 || a [$ 1] == "") {a [$ 1] = $ 2;} if (b [$ 1]

|| a [$ 1] == "" because when the first value of field 1 is encountered, it will be null in [$ 1]

0
source

how to run this in a folder with lots of data files. The minimum line of each data file must be stored in a new file. I did my best

 for file in *.dat; do sort -nk 2 $file | head -n 1 >new.dat #awk 'BEGIN{min=0}{if(($2)>min) min=($2)}END {print min}' sample.dat done 

this script will erase and write the new value.

0
source

Source: https://habr.com/ru/post/1442464/


All Articles