I'm using AWK and I've been trying to compare the previous value in a column with the next one until it finds the highest but I haven't been able to.
awk '$6 >= $6 {print $6}'
With the above, it returns me every single value
For example:
money:
49
90
30
900
I would like it to return 900
CodePudding user response:
Like this:
awk 'int($6) && $6 > n{n=$6}END{print n}' file
CodePudding user response:
Assuming the columns are separated by the default (white-space) separator, the following awk line checks the 6th value in each data row to see if it is greater than any previously seen values (current
set in a BEGIN
block to zero, gets updated each time a greater 6th value is encountered). The END
block prints the final value held in current
(which is the greatest value for the 6th field)"
awk 'BEGIN {current=0} {if ($6>current) current=$6} END {print current}' dataFile.txt
For .csv
data, the file separator can be defined as a comma with FS=","
in the BEGIN
block:
awk 'BEGIN {FS=","; current=0} {if ($6>current) current=$6} END {print current}' dataFile.csv
CodePudding user response:
$ awk '(NR>1) && ((NR==2) || ($1>max)){max=$1} END{if (max != "") print max}' file
900
The above is based on your posted example (including the money:
header line) but would also work even if all input values were 0
or negative or the input file was empty. Change $1
to $6
if the real field number you're interested in is 6.
Also consider:
$ tail -n 2 file | cut -f1 | sort -rn | head -1
900
and change -f1
to -f6
.
Set separator chars in awk with -F'<char>'
and cut with -d'<char>'
if necessary.