Awk: Print Error While Redirecting output in multiple Files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk: Print Error While Redirecting output in multiple Files
# 1  
Old 05-21-2015
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

Code:
 awk -F, '{ if ( substr($1,26,2)=="02" && substr($1,184,14)=="MTSCC_VALFIRST") {
array1[substr($1,28,14)","substr($1,126,5)]++
array2[substr($1,126,5)]++
array3[substr($1,28,14)]++
 }
else if (substr($1,26,2)=="03" && substr($1,184,14)=="MTSCC_VALFIRST")
array4[substr($1,28,14)","substr($1,126,5)]++
}
END { for (counter1 in array1) {print counter1","array1[counter1] |"sort" } >"ValFirst_AO.txt"
 {for (counter2 in array2) { print counter2","array2[counter2] |"sort" > "ValFirst_MT.txt"} }
{for (counter3 in array3) {print counter3 ","array3[counter3]|"sort" >"ValFirst_SuccMT.txt"} }
{for (counter4 in array4) {print counter4","array4[counter4] |"sort" >"ValFirst_TotalMT.txt"} }
}
' 201505200900*
awk: cmd. line:10: END { for (counter1 in array1) {print counter1","array1[counter1] |"sort" } >"ValFirst_AO.txt"
awk: cmd. line:10:                                                                             ^ syntax error
awk: cmd. line:11:  {for (counter2 in array2) { print counter2","array2[counter2] |"sort" > "ValFirst_MT.txt"} }
awk: cmd. line:11:                                                                        ^ syntax error
awk: cmd. line:12: {for (counter3 in array3) {print counter3 ","array3[counter3]|"sort" >"ValFirst_SuccMT.txt"} }
awk: cmd. line:12:                                                                      ^ syntax error
awk: cmd. line:13: {for (counter4 in array4) {print counter4","array4[counter4] |"sort" >"ValFirst_TotalMT.txt"} }
awk: cmd. line:13:                                                                      ^ syntax error

# 2  
Old 05-21-2015
Hello siramitsharma,

Could you please change following:
Change {print counter1","array1[counter1] |"sort" }>"ValFirst_AO.txt" to {print counter1","array1[counter1] |"sort" >"ValFirst_AO.txt"}, } should be at completion of file redirection not before it.

Hope this will help you.

Thanks,
R. Singh
# 3  
Old 05-21-2015
Still the same

Code:
 awk '{
if( substr($1,26,2)=="02" && substr($1,184,14)=="MTSCC_VALFIRST")
{
array1[substr($1,28,14)","substr($1,126,5)]++
array2[substr($1,126,5)]++
array3[substr($1,28,14)]++
}
else if (substr($1,26,2)=="03" && substr($1,184,14)=="MTSCC_VALFIRST")
array4[substr($1,28,14)","substr($1,126,5)]++
}
END { for (counter1 in array1) {print counter1","array1[counter1] |"sort" >"ValFirst_AO.txt" } }
' 201505200900*
awk: cmd. line:11: END { for (counter1 in array1) {print counter1","array1[counter1] |"sort" >"ValFirst_AO.txt" } }
awk: cmd. line:11:                                                                           ^ syntax error

# 4  
Old 05-21-2015
Hello siramitsharma,

Not sure what you are trying to do with character |, if this is for printing purposes then try "|sort" and let us know if this helps you. Also if this advice wouldn't help you please post complete requirement details with input and expected output too.

Thanks,
R. Singh

Last edited by RavinderSingh13; 05-21-2015 at 03:22 AM..
# 5  
Old 05-21-2015
I think you can redirect printing from awk only once. man mawk:
Quote:
The output of print and printf can be redi‐
rected to a file or command by appending > file, >> file or | command to the end of the print statement.
But you could let the system do the redirection of sort's output for you. Try
Code:
{print counter1","array1[counter1] |"sort > ValFirst_AO.txt" }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

awk script issue redirecting to multiple files after matching pattern

Hi All I am having one awk and sed requirement for the below problem. I tried multiple options in my sed or awk and right output is not coming out. Problem Description ############################################################### I am having a big file say file having repeated... (4 Replies)
Discussion started by: kshitij
4 Replies

3. Shell Programming and Scripting

Compare columns of multiple files and print those unique string from File1 in an output file.

Hi, I have multiple files that each contain one column of strings: File1: 123abc 456def 789ghi File2: 123abc 456def 891jkl File3: 234mno 123abc 456def In total I have 25 of these type of file. (5 Replies)
Discussion started by: owwow14
5 Replies

4. Shell Programming and Scripting

awk, multiple files input and multiple files output

Hi! I'm new in awk and I need some help. I have a folder with a lot of files and I need that awk do something in each file and print a new file with the output. The input file name should be modified when I print the outpu files. Thanks in advance for help! :-) ciao (5 Replies)
Discussion started by: gabrysfe
5 Replies

5. Solaris

Redirecting print to optional output bins

Guys We have a HP P4015 laserjet printer with a 5 bin mailbox attached & configured. We can print to the specific output bins from Oracle e-Business suite, however our print output format is incompatible so it prints out random characters instead of the letter content. I have looked... (2 Replies)
Discussion started by: s1977
2 Replies

6. UNIX for Dummies Questions & Answers

Using AWK: Extract data from multiple files and output to multiple new files

Hi, I'd like to process multiple files. For example: file1.txt file2.txt file3.txt Each file contains several lines of data. I want to extract a piece of data and output it to a new file. file1.txt ----> newfile1.txt file2.txt ----> newfile2.txt file3.txt ----> newfile3.txt Here is... (3 Replies)
Discussion started by: Liverpaul09
3 Replies

7. 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

8. Shell Programming and Scripting

awk print fields to multiple files?

I am trying to print the output of a command to two separate files. Is it possible to use awk to print $1 to one file and $2 to another file? Thanks in advance! (1 Reply)
Discussion started by: TheCrunge
1 Replies

9. UNIX for Dummies Questions & Answers

Redirecting UNIX Print Output

I am a new user to UNIX. We are currently running an application called DISC (www.disclink.com) on UNIX, and I am trying to figure out how to redirect print output to a PC printer. There is one little problem: DISC apparently has its own OS that is sitting on top of the UNIX OS, so you cannot... (1 Reply)
Discussion started by: BlueSpider
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