Redirecting the results to different output files

Hi All,

I am trying a shell script and need your help on taking the results to different output files.
I have tried the below code:

nawk '
{CNF = (length()-10)/7
 printf "%9s", substr ($0, 1, 9)
for (i=0; i<=CNF; i++) T[i+2] = substr ($0, 10+i*7, 7)
TMP = 100 - (T[8] + T[9] + T[10] + T[11] + T[12] + T[13] + T[14] + T[15] + T[16] + T[17] + T[18])
for (i=2; i<=CNF+2; i++){printf "%7s", T; if(i==18) {printf "%7.3f",T=TMP}}
printf RS
}' input > output

My Input:

032016501  3.974 96.026 20.605 28.838 21.722 28.835  0.306  0.000 66.204  0.229  0.000 16.707  5.348  0.000  3.142  0.000  5.929 71.299 28.701  6.662 93.338  2.360 97.640  0.000100.000 46.620 53.380 43.192 56.808 66.447 16.775  0.063  0.160 16.554 92.911  1.882  0.159  5.048
032016504  3.262 96.738 13.788 26.191 24.911 35.109  0.613  0.000 57.812  0.068  0.000 21.573  6.325  0.000  5.475  0.089  6.624 77.755 22.245  7.786 92.214  4.759 95.241  0.000100.000 47.223 52.777 43.055 56.945 58.410 21.412  0.124  0.225 19.934 92.965  1.397  0.364  5.273
032016505  2.394 97.606 13.972 22.808 25.249 37.971  0.000  0.000 43.433  0.000  0.000 22.217 12.057  0.221  8.796  0.205 12.087 77.008 22.992 13.163 86.837  7.690 92.310  0.000100.000 56.074 43.926 47.104 52.896 43.433 21.925  0.000  0.292 34.351 91.996  0.808  0.000  7.196

My Output:

032016501  3.974 96.026 20.605 28.838 21.722 28.835  0.306  0.000 66.204  0.229  0.000 16.707  5.348  0.000  3.142  0.000  5.929  2.135 71.299 28.701  6.662 93.338  2.360 97.640  0.000100.000 46.620 53.380 43.192 56.808 66.447 16.775  0.063  0.160 16.554 92.911  1.882  0.159  5.048
032016504  3.262 96.738 13.788 26.191 24.911 35.109  0.613  0.000 57.812  0.068  0.000 21.573  6.325  0.000  5.475  0.089  6.624  1.421 77.755 22.245  7.786 92.214  4.759 95.241  0.000100.000 47.223 52.777 43.055 56.945 58.410 21.412  0.124  0.225 19.934 92.965  1.397  0.364  5.273
032016505  2.394 97.606 13.972 22.808 25.249 37.971  0.000  0.000 43.433  0.000  0.000 22.217 12.057  0.221  8.796  0.205 12.087  0.984 77.008 22.992 13.163 86.837  7.690 92.310  0.000100.000 56.074 43.926 47.104 52.896 43.433 21.925  0.000  0.292 34.351 91.996  0.808  0.000  7.196

With the above code, i am inserting a new field at 19th field in my Input.The new field calculations you can find in the code. I have got my desired results in the output.

Problem is, i want to apply this same code on 7 different files and take output to different files. I have tried but not able to get it done.

Can anyone help me on how to apply the same code on different input files and taking the results to different output files ?

Thanks in advance,
am24

How about adapting/stealing from the proposals in your other thread?

Rudi, i have tried all those but not able to get it. So asking for the help again ...

Regards,
am24

Count the input files, compose the respective output file names, and use redirection to the latter for the print(f) statements.

Sorry if this sounds blunt, but I'm seriously disappointed that the examples of redirection in awk - that could be applied almost 1:1 - and two more or less verbose explanations of the codes given seem to have failed their purpose.

Hi Rudi,

I have tried the below codes before posting here. Please have a look into this.

1st trail:

nawk '
FNR == 1 {
if(fc++)
close(ofn)
ofn = "output" fc
printf > ofn
}
{CNF = (length()-10)/7
 printf "%9s", substr ($0, 1, 9)
for (i=0; i<=CNF; i++) T[i+2] = substr ($0, 10+i*7, 7)
TMP = 100 - (T[8] + T[9] + T[10] + T[11] + T[12] + T[13] + T[14] + T[15] + T[16] + T[17] + T[18])
for (i=2; i<=CNF+2; i++){printf "%7s", T; if(i==18) {printf "%7.3f",T=TMP}}
}' input1 input2

With the above outputs are getting created but only with 1 line.and my 19th field modification did not happen in the outputs.

2nd trail:

nawk '
FNR == 1 {
if(fc++)
close(ofn)
ofn = "output" fc
}
{CNF = (length()-10)/7
 printf "%9s", substr ($0, 1, 9)
for (i=0; i<=CNF; i++) T[i+2] = substr ($0, 10+i*7, 7)
TMP = 100 - (T[8] + T[9] + T[10] + T[11] + T[12] + T[13] + T[14] + T[15] + T[16] + T[17] + T[18])
for (i=2; i<=CNF+2; i++){printf "%7s", T; if(i==18) {printf "%7.3f",T=TMP}}
printf RS > ofn
}' input1 input2

With the above, outputs are created with 56 empty lines.

3rd trail:

nawk '
FNR == 1 {
if(fc++)
close(ofn)
ofn = "output" fc
}
{CNF = (length()-10)/7
 printf "%9s", substr ($0, 1, 9)
for (i=0; i<=CNF; i++) T[i+2] = substr ($0, 10+i*7, 7)
TMP = 100 - (T[8] + T[9] + T[10] + T[11] + T[12] + T[13] + T[14] + T[15] + T[16] + T[17] + T[18])
for (i=2; i<=CNF+2; i++){printf "%7s", T; if(i==18) {printf "%7.3f",T=TMP}}
print > ofn
}' input1 input2

With the above, outputs are created but 19th field modification did not happen. the outputs are same as inputs.

Where I am lagging not getting. Please suggest me on this.

Regards,
am24

The FNR == 1 part is good!

Did you consider ALL print / printf statements?

Hi Rudi,

with printf > ofn , output is generated like containing all the 56 lines data in one line.

Also output is displaying to the screen.

I really do not know how to proceed further on this. done many trails on this.

Regards,
am24

---------- Post updated at 10:10 AM ---------- Previous update was at 10:07 AM ----------

I am feeling like i am missing to take the loop results into print statement. But do not know exactly how to put it properly.

Regards,
am24

Why is output "displaying to the screen"? You are printf ing the fields to the screen, not redirecting them.

yes. i am redirecting it like printf > ofn .

Results is displaying on screen and in the output file all the 56 lines merged as 1 line without the modification on 19th field .

Did i use the print statement at the right place ?

nawk '
       FNR == 1 {
              if(fc++)
              close(ofn)
              ofn = "output" fc
       }
               {CNF = (length()-10)/7
                printf "%9s", substr ($0, 1, 9)
                for (i=0; i<=CNF; i++) T[i+2] = substr ($0, 10+i*7, 7)
               TMP = 100 - (T[8] + T[9] + T[10] + T[11] + T[12] + T[13] + T[14] + T[15] + T[16] + T[17] + T[18])
       for (i=2; i<=CNF+2; i++){printf "%7s", T; if(i==18) {printf "%7.3f",T=TMP}}
            printf > ofn
      }
      ' input1 input2

Could you please suggest me on this?

Regards,
am24

Please show us the various print / printf statements in your code snippet (e.g. by highlighting them in red).

Hi Rudi,

Please find the below codes:
1st trail:

nawk '
       FNR == 1 {
              if(fc++)
              close(ofn)
              ofn = "test" fc
       }
               {CNF = (length()-10)/7
                printf "%9s", substr ($0, 1, 9)
                for (i=0; i<=CNF; i++) T[i+2] = substr ($0, 10+i*7, 7)
               TMP = 100 - (T[8] + T[9] + T[10] + T[11] + T[12] + T[13] + T[14] + T[15] + T[16] + T[17] + T[18])
               for (i=2; i<=CNF+2; i++){printf "%7s", T; if(i==18){printf "%7.3f",T=TMP}}}
              { printf > ofn }
      ' input1 input2

With the above all the 56 lines in the input1 and input2 are merged as 1 line without the modifictaion on 19th field and resulted as test1 and test2

2nd trail:

nawk '
       FNR == 1 {
              if(NR>1)
              close(ofn)
              ofn = "output" ++nf
       }
               {CNF = (length()-10)/7
                printf "%9s", substr ($0, 1, 9)
                for (i=0; i<=CNF; i++) T[i+2] = substr ($0, 10+i*7, 7)
               TMP = 100 - (T[8] + T[9] + T[10] + T[11] + T[12] + T[13] + T[14] + T[15] + T[16] + T[17] + T[18])
       for (i=2; i<=CNF+2; i++){printf "%7s", T; if(i==18) {printf "%7.3f",T=TMP}}}
           { print > ofn
      }
      ' input1 input2

With the above code, output1 and output2 are generated as same as the input1 and input2.

In both trails output displayed to screen. If i open the test1 from first trail its showing data and "test1" [Incomplete last line] 1 line, 15400 characters. where as the wc's are showing below.

wc test1 test2 output1 output2
       0    2080   15400 test1
       0    2055   15400 test2
      56    2135   15456 output1
      56    2110   15456 output2
     112    8380   61712 total

Regards,
am24

OK, brilliant. You show us FOUR print / printf statements of which ONE is redirected to your desired output file(s) and prints but a <NL> character, and THREE print the result's fields to the default stdout which happens to be the screen.

EDIT: How about reconsulting the other thread and compare what is presented there to your script?

Rudi , I am sorry . I really do not have any hopes that i can get through this by my own. I am not understanding where i am doing mistake in the code.

Please let me know what is the mistake.

Regards,
am24

Redirect EVERY SINGLE print/printf to ofn.

Hi Rudi,

I have tried like below. Now the output is not displaying to teh screen but Output1 and output2 are generated with 56 lines input data as 1 line without modification on 19th field.

nawk '
       FNR == 1 {
              if(NR>1)
              close(ofn)
              ofn = "output" ++nf
       }
               {CNF = (length()-10)/7
                printf "%9s", substr ($0, 1, 9) > ofn
                for (i=0; i<=CNF; i++) T[i+2] = substr ($0, 10+i*7, 7)
               TMP = 100 - (T[8] + T[9] + T[10] + T[11] + T[12] + T[13] + T[14] + T[15] + T[16] + T[17] + T[18])
       for (i=2; i<=CNF+2; i++){printf "%7s", T > ofn} if(i==18) {printf "%7.3f",T=TMP > ofn}
      }
      ' input1 input2

Please suggest me on this.

Regards,
am24

Where has the

           { print > ofn
          } 

gone? And, was the output correct when it occurred on the screen?

Hi Rudi,

I have tried with print > ofn . Then the outputs are generated as below:

032016501  3.974 96.026 20.605 28.838 21.722 28.835  0.306  0.000 66.204  0.229  0.000 16.707  5.348  0.000  3.142  0.000  5.929 71.299 28.701  6.662 93.338  2.360 97.640  0.000100.000 46.620 53.380 43.192 56.808 66.447 16.775  0.063  0.160 16.554 92.911  1.882  0.159  5.048032016501  3.974 96.026 20.605 28.838 21.722 28.835  0.306  0.000 66.204  0.229  0.000 16.707  5.348  0.000  3.142  0.000  5.929 71.299 28.701  6.662 93.338  2.360 97.640  0.000100.000 46.620 53.380 43.192 56.808 66.447 16.775  0.063  0.160 16.554 92.911  1.882  0.159  5.048

Each line in output is repeating twice and 19th field modification did not happen

I tried with printf > ofn . All the 56 lines merged as 1 line in the output.

Also tried with printf RS > ofn . Outputs generated are same as the inputs.

The output is not correct when it printed on the screen.its same like input.19th field modification did not reflect.

I am missing some where to take the modification done on 19th field. Please suggest me on this.

Thanks in advance,
am24

Use printf RS > ofn or print "" > ofn .

Where does

       for (i=2; i<=CNF+2; i++){printf "%7s", T > ofn} if(i==18) {printf "%7.3f",T=TMP > ofn}
                                                        ^--- this   ^--- and this 

come from, when the working solution had

for (i=2; i<=CNF+2; i++){printf "%7s", T; if(i==18) {printf "%7.3f",T=TMP}} 

You NEED to learn to do careful one-to-one copies, and, if modifying proposals, to know EXACTLY what you are doing!

And, don't rely on being spoon fed in these forums for every problem occurring in your IT career.

Rudi, as per your suggestions , i have modified the first code below

for (i=2; i<=CNF+2; i++){printf "%7s", T; if(i==18) {printf "%7.3f",T=TMP}}

as

for (i=2; i<=CNF+2; i++){printf "%7s", T > ofn} if(i==18) {printf "%7.3f",T=TMP > ofn}

I have tried printf RS > ofn and print "" > ofn .In all of the trails ,the modification done on 19th field is not reflecting in the output.

Thanks for all of your suggestions.

Regards,
am24

Sorry, is it working now, or failing?