Redirecting the results to different output files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirecting the results to different output files
# 1  
Old 05-17-2016
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:

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[i]; if(i==18) {printf "%7.3f",T[i]=TMP}}
printf RS
}' input > output


My Input:

Code:
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:
Code:
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
# 2  
Old 05-17-2016
How about adapting/stealing from the proposals in your other thread?
# 3  
Old 05-17-2016
Rudi, i have tried all those but not able to get it. So asking for the help again ...

Regards,
am24
# 4  
Old 05-17-2016
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.
# 5  
Old 05-17-2016
Hi Rudi,

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

1st trail:

Code:
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[i]; if(i==18) {printf "%7.3f",T[i]=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:
Code:
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[i]; if(i==18) {printf "%7.3f",T[i]=TMP}}
printf RS > ofn
}' input1 input2

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

3rd trail:

Code:
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[i]; if(i==18) {printf "%7.3f",T[i]=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
# 6  
Old 05-17-2016
The FNR == 1 part is good!

Did you consider ALL print/printf statements?
# 7  
Old 05-17-2016
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: Print Error While Redirecting output in multiple Files

Hi, I have a following code in which I am unable to redirect to multiple files. Can anybody please help with some corrections awk -F, '{ if ( substr($1,26,2)=="02" && substr($1,184,14)=="MTSCC_VALFIRST") { array1++ array2++ array3++ } else if (substr($1,26,2)=="03" &&... (4 Replies)
Discussion started by: siramitsharma
4 Replies

2. Post Here to Contact Site Administrators and Moderators

Redirecting grep output to multiple files

Hi All, I am trying to redirect the grep output to multiple files, can you please help with that. Below is the command im using to match my pattern grep \<proxyType\>$PxyType $DIR/EndureFiles.json > File_Name*.json Note : $DIR and $PxyType is already defined in my script Im able... (0 Replies)
Discussion started by: Deena1984
0 Replies

3. Shell Programming and Scripting

Redirecting the output

For example, if we run the below command, symcfg list -thin -pool , results in an output most of the times and if the out is generated i'm able to redirect the output to a file. but sometimes it doesnt result any output and even though the output is being redirected, i can see "No Thin Pools "... (2 Replies)
Discussion started by: web2moha
2 Replies

4. Shell Programming and Scripting

awk help: Match data fields from 2 files & output results from both into 1 file

I need to take 2 input files and create 1 output based on matches from each file. I am looking to match field #1 in both files (Userid) and create an output file that will be a combination of fields from both file1 and file2 if there are any differences in the fields 2,3,4,5,or 6. Below is an... (5 Replies)
Discussion started by: ambroze
5 Replies

5. Shell Programming and Scripting

Redirecting output to file

Hi, Below is the whole string which is to be redirected to the new file. su - oracle -c "exp $user/$pass file=/oracle/oradata/backup/exp_trn_tables_`date +%d_%b_20%y_%H_%M_%S`.dmp log=/oracle/oradata/backup/exp_trn_tables_`date +%d_%b_20%y_%H_%M_%S`.log tables=table1,table2 statistics=none" ... (3 Replies)
Discussion started by: milink
3 Replies

6. Shell Programming and Scripting

Redirecting to different output files with awk.

Well, it didn't take me long to get stumped again. I assure you that I'm not mentally deficient, just new to scripting. So, here's the gist. I want to redirect output from awk based off of which branch of an if-else statement under which it falls. #!/bin/bash #some variables... (2 Replies)
Discussion started by: mikesimone
2 Replies

7. Shell Programming and Scripting

Redirecting echo output to 2 flat files

Hello all, I have the following 2 questions.. 1) I would like to capture the output of an echo command to 2 different files at the same time. This does not seem to work. Any ideas? echo ==== started on `date` ==== >> res1.log res2.log 2) Would it be possible to just get the 5th... (2 Replies)
Discussion started by: luft
2 Replies

8. Programming

execl() + redirecting output to text files

Im currently using execl() to run the ls command and redirect the output to a text file. Unfortunately, when I check the text file, no information has been written to it. I am calling execl() with the ls command like this execl( "/bin/ls" , "-al" , '>' , "dirlist.txt" ,(char *) 0 ); ... (5 Replies)
Discussion started by: JamesGoh
5 Replies

9. Shell Programming and Scripting

Redirecting OUTPUT

Hi, I want to move the output of a command/script to a file as well as to to be displayed on stdout. Can anybody help me in this. Thanks in advace .. -Chanakya M (1 Reply)
Discussion started by: Chanakya.m
1 Replies

10. UNIX for Dummies Questions & Answers

Redirecting output to multiple log files?

If I wanted to redirect output to multiple log files, what would be the best way to do that? echo "Unix is awesome" >>unixgod.log >>unixgod.log (3 Replies)
Discussion started by: darthur
3 Replies
Login or Register to Ask a Question