What my logic is:
- To divide
(column 3 by column 2)
and show the result (these calculation is read fromfileB
). - To show the
the actual values
(untouched, read fromFileA
) - If the result is zero (zero division error), write it to
zero_file
, if result isbetween 1 and 6
, write it tobetween_file
, if ismore than 8
, write it togreat_file
, if it'sneither of those
, write it toneither_file
.
fileA
:
col1 col2 col3 col4
1 1K 2K name1
2 0 3K name2
3 1K 20M name3
4 2K 14K name4
fileB
:
col1 col2 col3 col4
1 1000 2000 name1
2 0 3000 name2
3 1000 20000000 name3
4 2000 14000 name4
Why two files?
These two above files are generated somewhere else and the values are not fixed (so, let us skip how them are made).
fileB
is calculated from fileA
and all K
and M
s are converted to bytes
.
Expected output
I am trying to fileB --> col3 divide by col2
(if it's zero or greater than 8 or between 1 and 4 (different conditions)), then show the values from fileA --> it is calculated from col3 by col2
:
The name1's rate is 2 (it is calculated from 2K by 1K).
The name2's rate is 0 (it is calculated from 3K by 0).
The name3's rate is 20000 (it is calculated from 20M by 1K).
The name4's rate is 7 (it is calculated from 14K by 2K).
My attempts
This is my another attempt (with double quotes):
awk "
(NR == FNR) {
rate = (!\$2 ? 0 : \$3/\$2)
rx = (\$2)
tx = (\$3)
file_name = \"$PWD/file\"
next
}
rate >= 1 && rate <= 4 {file_name=\"$PWD/file\"}
rate >= 8 {file_name=\"$PWD/file\"}
!rate {file_name=\"$PWD/file\"}
{
first = (\$2)
second = (\$3)
}
END {
print \"ratio for\", \$NF, \"is\", rate, \"(result original receive:\", first \", original transfer:\", second \")\" > file_name
}" col3 col2
This is my another attempt (with single quotes and no variables):
awk '
(NR == FNR) {
rate = (!$2 ? 0 : $3/$2)
rx = ($2)
tx = ($3)
file_name = "/path/file"
next
}
rate >= 1 && rate <= 4 {file_name="/path/file"}
rate >= 8 {file_name="/path/file"}
!rate {file_name="/path/file"}
{
first = ($2)
second = ($3)
}
END {
print "ratio for", $NF, "is", rate, "(result original receive:", first ", original transfer:", second ")" > file_name
}' col3 col2
My issue with my attempt
ratio for name4 is 7 (result original receive: 2K, original transfer: 14K)
It only shows the last column, but not all columns.
This was my latest but unsuccessful attempt.