0

I have a file like:

available_space:1232334343
capacity:123456432
total_space:1232323232

I want to calculate capacity/total_space so I need to calculate 123456432/1232323232

I can imagine I need to use something like:

cat my_file | awk -F:'FNR==2 {print$2}' 

But cannot write the division itself, I'm not quiet sure about the syntax. So how I can do that?

2 Answers 2

1

According to your initial approach, the crucial lines are the 2nd and 3rd line only:

awk -F':' 'NR==2{ c=$2 }NR==3{ print c/$2 }' my_file
0.100182
0

When the first field is capacity, save the value of the a second field to a variable. Then when the first field is total_space, divide the value of the variable by that of the second field:

awk -F: '$1 == "capacity" {c = $2} $1 == "total_space" && c != 0 {print c/$2}' my_file
0.100182

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .