How does awkArray[NR]=$0 and awkArray[NR]=$1 print the last number

My take on finding the maximum number in a file was written for a windows environment (running under cmd.exe). In this environment only double quotes can be used to “protect” an awk script and even then it kind of prevents you from using double quotes within your script. The following will work, even for cases when there are exclusively negative numbers in the input file. It will return an empty line if the input file is empty:

awk "NR==1{max=$0} $0>max {max=$0} END {print max}" num.log

Special thanks to Ed Morton for his constructive advice (see comments below). Please follow his recommendation of using single quotes when running the script in a shell environment.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top