If condition satisfies print the column header

Dear All,

Good Day. I would like to solve the following issue and got some strange results, if anyone could help me in this regard, you are most welcome.

Here is the problem:

I have a file like

Header Value1 Value2 Value3 Value4 Value5 Value6 ... Value9
12.144 6 5 5 4 3 2 ...
10.2567 10 8 6 4 4 4 ...
22.2 2 2 1 1 1 1 ...
.................................
.................................

The above-mentioned file have more than 500 rows and 10 columns.

I would like to print a new additional columns (say for example column11 and column12), where the values from column2 to column9 can satisfy the following conditions:

if ($i > 5 && $(i+1) < 5) ------ print the column header (for example, in the first row case, Value 4) should be printed in column11 and at the same time,

if ($i > 3 && $(i+1) < 3) ------ print the column header (for example, in the first row case, Value 6) should be printed in column12 (in the output file with the same format).

I tried the following code, but i couldn't get the same number of rows. here is my code:

awk '{for(i=2; i<=NF; i++) {if($i > 5 && $(i+1) < 5) print $1=(i*5); else print ""}}' inputfile > outputfile

Expecting your reply and thanks in advance.

Warm regards
Fredrick.

Awk variables dont need $ with them.

Sorry, Could you please explain me.

Sorry I mis-read the question :stuck_out_tongue:

---------- Post updated at 12:23 PM ---------- Previous update was at 12:18 PM ----------

If you want to compare or check column 2 and column 9 then use $2 and $9 to get the values from those columns. I am not sure whats the logic behind getting the value 4 and 6 as you mentioned in your post..

Your code is comparing successive columns and replacing first column with column * 5.. else you printed blank line.

awk '{for(i=2; i<=NF; i++) {if($i > 5 && $(i+1) < 5) print $1=(i*5); else print ""}}' inputfile > outputfile

Yes the above-code print i*5; instead of that I would like to print only the header values in the new columns, if the conditions satisfied.
I need your help in this regard.

Expecting your reply and thanks in advance.

-F

Could you please post small piece of actual input file and the output you want? I am unable to understand your requirements with below

If I understand what you are trying to do, the following does it:

$ cat inputfile
12.144 6 5 5 4 3 2 9 9 9
10.2567 10 8 6 4 4 4 9 9 9
22.2 4 2 1 1 1 1 9 9 9
$ cat test.awk
{
x=NF; $11=$12=0
for (i=2; i<x; i++) {
  if ($i > 5 && $(i+1) < 5) {$11=i}
  if ($i > 3 && $(i+1) < 3) {$12=i}
  }
print
}
$ awk -f test.awk inputfile
12.144 6 5 5 4 3 2 9 9 9 0 0
10.2567 10 8 6 4 4 4 9 9 9 4 0
22.2 4 2 1 1 1 1 9 9 9 0 2

Here is my input file (first few line only., it has more than 500 lines)

 Average        10      15      20      25      30      35      40      45
 13.622 25 16 14 8 7 6 4 4
 12.695 15 9 7 6 4 4 4 3
 10.7837 10 6 4 4 2 2 1 1
 8.05027 6 3 3 1 1 1 1 1
 10.9237 9 6 4 4 2 2 2 1

I need my output should be like this:

                     P { margin-bottom: 0.08in; }    Average        10      15      20      25      30      35      40      45      new_col1      new_col2
  13.622 25 16 14 8 7 6 4 4   35  -
  12.695 15 9 7 6 4 4 4 3    25  45  
  10.7837 10 6 4 4 2 2 1 1   15  30
  8.05027 6 3 3 1 1 1 1 1   10  15
  10.9237 9 6 4 4 2 2 2 1   25  30
  

new_col1 should satisfy the first if condition and new_col2 should satisfy the second if condition (as i mentioned before).

Hope I have explained you the problem correctly.

Expecting your reply and thanks in advance.

-F

---------- Post updated at 09:16 AM ---------- Previous update was at 09:15 AM ----------

Here is my input file (first few line only., it has more than 500 lines)

Average        10      15      20      25      30      35      40      45
13.622 25 16 14 8 7 6 4 4
12.695 15 9 7 6 4 4 4 3
10.7837 10 6 4 4 2 2 1 1
 8.05027 6 3 3 1 1 1 1 1
10.9237 9 6 4 4 2 2 2 1

I need my output should be like this:

                     P { margin-bottom: 0.08in; }    Average        10      15      20      25      30      35      40      45      new_col1      new_col2
  13.622 25 16 14 8 7 6 4 4   35  -
  12.695 15 9 7 6 4 4 4 3    25  45  
  10.7837 10 6 4 4 2 2 1 1   15  30
  8.05027 6 3 3 1 1 1 1 1   10  15
  10.9237 9 6 4 4 2 2 2 1   25  30
  

new_col1 should satisfy the first if condition and new_col2 should satisfy the second if condition (as i mentioned before).

Hope I have explained you the problem correctly.

Expecting your reply and thanks in advance.

-F

---------- Post updated at 09:24 AM ---------- Previous update was at 09:16 AM ----------

Thanks hanson44 for your reply. But my input file and the output file i need is bit different. Could you please see my reply in the threat.

Warm regards
-F

I think some of your expected output is incorrect. Anyway, try the following.

$ cat test.awk
NR==1 {split ($0, headers); print; next}
{
x=NF; $11=$12="-"
for (i=2; i<x; i++) {
  if ($i > 5 && $(i+1) < 5) {$11=headers}
  if ($i > 3 && $(i+1) < 3) {$12=headers}
  }
print
}
$ awk -f test.awk inputfile
Average        10      15      20      25      30      35      40      45
13.622 25 16 14 8 7 6 4 4  35 -
12.695 15 9 7 6 4 4 4 3  25 -
10.7837 10 6 4 4 2 2 1 1  15 25
8.05027 6 3 3 1 1 1 1 1  10 -
10.9237 9 6 4 4 2 2 2 1  15 25
1 Like