0

I have a file like this:

0.0451660231
0.0451660231
0.0527343825
0.3933106065
0.3970947862
0.0489502028
0.3592529595
0.3592529595
0.3592529595
0.3630371392
0.3630371392
0.3668213189
0.4008789659
0.1397705227

and I want to divide each line by the maximum value.

I did

cut -f1 -d"," CVBR1_hist | sort -n | tail -1 > maximum
awk -v c=$maximum '{print $1/c}' CVBR1_hist > CVBR1_norm

I have this error:

awk: cmd. line:1: (FILENAME=CVBR1_hist FNR=1) fatal: division by zero attempted

I don't know how to solve it. Can anyone help me?

  • 2
    You should assign to a variable named maximum not redirect to a file named maximum. And what are you cutting there ? There's only one field per line in your file – don_crissti Jan 24 '17 at 14:26
  • If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you! – Jeff Schaller Nov 26 '17 at 18:10
0

Awk give you an error because the variable "c" is set equal to an empty variable. $maximum isn't yet set.

You should do:

awk -v c=`cat maximum` '{print $1/c}' CVBR1_hist > CVBR1_norm

Here is where your command failed.

Better way: don't go through a temporary file but store the maximum value in a variable, like the Kusalananda's answer.

2

Let's assume that sorting the contents of the file CVBR1_hist numerically generates the correct data:

$ sort -n CVBR1_hist
0.0451660231
0.0451660231
0.0489502028
0.0527343825
0.1397705227
0.3592529595
0.3592529595
0.3592529595
0.3630371392
0.3630371392
0.3668213189
0.3933106065
0.3970947862
0.4008789659

Then we may store the maximum in a variable using a command substitution like this:

maximum="$( sort -n CVBR1_hist | tail -n 1 )"

The normalized values may then be had with awk:

awk -v m="$maximum" '{ print $1/m }' CVBR1_hist >CVBR1_norm

So the only thing you were missing was the proper storing of the maximum in a variable.

1

Here's a one-pass awk solution, but it stores all of the data in-memory in an array. Run it like: awk -f thisprogram.awk < CVBR1_hist > CVBR1_norm

{
  elements[NR]=$1
  if ($1 > largest) {
    largest = $1
  }
}
END {
  for(i=1; i <= NR; i++)
    printf "%.10f\n", elements[i]/largest
}
  • Begin statement wasn't necessary, since variable largest will be created dynamically. Also, I don't quite see what's the purpose of PROCINFO, but you generally got the right idea - linear search and arrays - therefore +1 – Sergiy Kolodyazhnyy Jan 24 '17 at 20:52
  • Thanks, Serg; I don't work in awk often enough to be good at it. I wasn't sure if there was a better way of sorting the array numerically (keeping the original contents in order). – Jeff Schaller Jan 24 '17 at 20:58
  • 1
    Serg made me stop and think a little bit more; the BEGIN stanza is gone, and the END stanza is greatly simplified and no longer uses a GNU-ism. – Jeff Schaller Jan 24 '17 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.