简体   繁体   中英

Using AWK to compare values in the same column

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

$ 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.

像这样:

awk 'int($6) && $6 > n{n=$6}END{print n}' file

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} NR>1{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} NR>1{if ($6>current) current=$6} END {print current}' dataFile.csv
$ awk 'NR==2  {max=$6} 
       $6>max {max=$6} 
       END    {print max}' file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM