1

Suppose the following columns, delimiter is one space (here for more readability I added more spaces):

col1 col2 col3 col4 col5  col6  col7 col8 col9 col10
1    P    0    0    1500  15000 0.5  0.0  200  name1
2    P    0    0    400   5000  0.7  0.2  150  name2
3    R    0    0    2000  2000  0.4  0.1  250  name3
4    R    0    0    5000  10000 0.2  0.3  600  name4
5    P    0    0    0     0     0.0  0.0  150  name5
6    R    0    0    6000  10000 0.1  0.0  120  name6
7    P    0    0    8     60    0.9  0.4  180  name7

The desired output should be something like this :

file1 (if {col6 / col5 equals or greater than 8}):

ratio for name1 is {ratio}.
ratio for name2 is {ratio}.

file2 (if {col6 / col5 is between 1 or 6}):

ratio for name3 is {ratio}.
ratio for name4 is {ratio}.
ratio for name6 is {ratio}.

file3 (if {col6 / col5 is zero}) (in this file, I mean if one of numbers is zero and we'll have something like zero division error, they should be here:

ratio for name5 is Zero

file4 (if none of above if clauses matched, then it goes here) (in this case, it is between 6.00000...1 to 7.9999...):

ratio for name7 is {ratio}.

This is my try but I can only differentiate if is zero, is less than 10 or greater than 10.

awk 'NR!=1{printf "ratio for %s is %s.\n", $10, $6==0?"ZeroDivision":($6/$5>1)?$($6/$5):($6/$5)}' < /root/test_file

I prefer having multiple files, but if that's a single file, it works too.

Saeed
  • 3,255
  • 4
  • 17
  • 36
  • `5000 / 400` is `12.5` so how come `name2` is in `file1` ? – anubhava Jun 03 '23 at 17:35
  • Similarly `10000 / 7500` is `1.33333` so `name7` should also be in `file2` since ratio is between `1` to `6` – anubhava Jun 03 '23 at 17:37
  • if the division results in a value `> 10` then what file should it go into? or do you discard the row? – markp-fuso Jun 03 '23 at 17:37
  • 1
    @anubhava yes you're right. I miscalculated that. But I edited the question to have `equal or greater than 8`, so `name2` fits in `file1`. I also edited `name7`. Since numbers are not real and I wrote them just now, they may have other mistakes. Thanks for correcting. – Saeed Jun 03 '23 at 17:42
  • Regarding `$6==0?"ZeroDivision"` - you have no requirement stated to print `"ZeroDivision"` and `$6 / $5` wouldn't be division by zero if $6 was zero, it'd be division by zero if $5 was zero. Do you need to have `"ZeroDivision"` printed somewhere? Do you really want that when $6 is zero? Please [edit] your question to clarify your requirements. – Ed Morton Jun 04 '23 at 12:45

1 Answers1

3

This awk should work for you:

awk '
NR==1 {next}
{
   r = ($5 ? $6/$5 : 0)
   fn = "file4"
}
r >= 8 {fn="file1"}
r >= 1 && r<= 6 {fn="file2"}
!r {fn="file3"}
{
   print "ratio for", $NF, "is", r > fn
}' file

Output:

awk 'FNR == 1 {print "::", FILENAME, "::"} 1'  file{1..4}

:: file1 ::
ratio for name1 is 10
ratio for name2 is 12.5
:: file2 ::
ratio for name3 is 1
ratio for name4 is 2
ratio for name6 is 1.66667
:: file3 ::
ratio for name5 is 0
:: file4 ::
ratio for name7 is 7.5
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Can you explain more what exactly does `r = (!$5 ? 0 : $6/$5)`? I didn't understand that well. I just know the last part divides `$6/$5`. I also read a lot about `NF` and `NR`, but I didn't understand either how it detects the `nameX`. Does it always detect the last column? – Saeed Jun 04 '23 at 11:34
  • 1
    It evaluates a condition `!$5` which returns true if `$5 == 0`. When that condition is true meaning `$5` is `0` then we assign `0` to `r` otherwise we divide `$6` by `$5` and assign the result to `r`. This is to avoid divide by `0` error. – anubhava Jun 04 '23 at 11:46
  • Thanks a lot. Can you give me a hint if I want to open `fileA` and `fileB` (now we're opening only `fileA`) and set a variable of `column 5` from `fileB`, like `file_B_var = "$5", then print `print "ratio for", $NF, "is", r, "the column5 of fileB is ", file_B_var > fn`? – Saeed Jun 04 '23 at 11:56
  • Can you hint me about the other part of my question? I mean how does it understand to show `nameX` with variable `$NF`? It is `number of fields`, but I don't understand how it works here. – Saeed Jun 04 '23 at 11:58
  • 1
    `$NF` is value of the last field. – anubhava Jun 04 '23 at 12:04
  • Other comment is not very clear to me. `awk` can work on multiple files but one file at a time. May be you need to post a new question for that part. – anubhava Jun 04 '23 at 12:08
  • 1
    Thanks, another question is here: https://stackoverflow.com/questions/76400092 – Saeed Jun 04 '23 at 12:09