Issues running an awk script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issues running an awk script
# 15  
Old 05-11-2012
And what is that "....." actually supposed to be?
# 16  
Old 05-11-2012
The awk code goes like the below, (input.txt and output.txt is provided below). The output prints the same XML four times. The first XML only contains the valid values from the input and the remaining three XML files are printed with no valid values. I am not sure why the awk script writes four XML's to the output. The command I use for executing the awk is as follows,

awk -f test.awk input.txt > output.txt

input.txt
********

2012-01,S27,GC00000T

test.awk:
**********

BEGIN{
FS=",";
requestID=100;
ORS="";
}{
requestID=requestID+1;
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?><TCRMService xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"CMSRequest.xsd\"><RequestControl><requestID>"requestID"</requestID><DWLControl><requesterName>Automated Cleanup TC1</requesterName><requesterLanguage>100</requesterLanguage><clientTransactionName>"$1"</clientTransactionName></DWLControl></RequestControl>";
print "<TCRMTx><TCRMTxType>maintainGlobalClientHierarchy</TCRMTxType><TCRMTxObject>TCRMContractBObj</TCRMTxObject><TCRMObject><TCRMContractBObj><CurrencyType>1</CurrencyType><AgreementName>Moretti New GC</AgreementName><AgreementNickName></AgreementNickName><AgreementStatusType>1000002</AgreementStatusType><AgreementType>1000002</AgreementType>";
print "<TCRMExtension><ExtendedObject>CMSContractBObjExt</ExtendedObject><CMSContractBObjExt><ClientGenType>1000002</ClientGenType></CMSContractBObjExt></TCRMExtension><TCRMContractComponentBObj><ContractStatusType>1000001</ContractStatusType><TCRMContractPartyRoleBObj><RoleType>1000001</RoleType><TCRMOrganizationBObj>";
print "<TCRMAdminContEquivBObj><AdminPartyId>"$2"</AdminPartyId><AdminSystemType>1000001</AdminSystemType></TCRMAdminContEquivBObj></TCRMOrganizationBObj></TCRMContractPartyRoleBObj></TCRMContractComponentBObj><TCRMAdminNativeKeyBObj><AdminContractId>"$3"</AdminContractId><AdminFieldNameType>1000001</AdminFieldNameType></TCRMAdminNativeKeyBObj></TCRMContractBObj></TCRMObject></TCRMTx></TCRMService>\n";
}
END {

}

output.txt:
*********

<?xml version="1.0" encoding="UTF-8"?><TCRMService xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="CMSRequest.xsd"><RequestControl><requestID>101</requestID><DWLControl><requesterName>Automated Cleanup TC1</requesterName><requesterLanguage>100</requesterLanguage><clientTransactionName>2012-01</clientTransactionName></DWLControl></RequestControl><TCRMTx><TCRMTxType>maintainGlobalClientHierarchy</TCRMTxType><TCRMTxObject>TCRMContractBObj</TCRMTxObject><TCRMObject><TCRMContractBObj><CurrencyType>1</CurrencyType><AgreementName>Moretti New GC</AgreementName><AgreementNickName></AgreementNickName><AgreementStatusType>1000002</AgreementStatusType><AgreementType>1000002</AgreementType><TCRMExtension><ExtendedObject>CMSContractBObjExt</ExtendedObject><CMSContractBObjExt><ClientGenType>1000002</ClientGenType></CMSContractBObjExt></TCRMExtension><TCRMContractComponentBObj><ContractStatusType>1000001</ContractStatusType><TCRMContractPartyRoleBObj><RoleType>1000001</RoleType><TCRMOrganizationBObj><TCRMAdminContEquivBObj><AdminPartyId>S27</AdminPartyId><AdminSystemType>1000001</AdminSystemType></TCRMAdminContEquivBObj></TCRMOrganizationBObj></TCRMContractPartyRoleBObj></TCRMContractComponentBObj><TCRMAdminNativeKeyBObj><AdminContractId>GC00000T</AdminContractId><AdminFieldNameType>1000001</AdminFieldNameType></TCRMAdminNativeKeyBObj></TCRMContractBObj></TCRMObject></TCRMTx></TCRMService>



<?xml version="1.0" encoding="UTF-8"?><TCRMService xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="CMSRequest.xsd"><RequestControl><requestID>102</requestID><DWLControl><requesterName>Automated Cleanup TC1</requesterName><requesterLanguage>100</requesterLanguage><clientTransactionName></clientTransactionName></DWLControl></RequestControl><TCRMTx><TCRMTxType>maintainGlobalClientHierarchy</TCRMTxType><TCRMTxObject>TCRMContractBObj</TCRMTxObject><TCRMObject><TCRMContractBObj><CurrencyType>1</CurrencyType><AgreementName>Moretti New GC</AgreementName><AgreementNickName></AgreementNickName><AgreementStatusType>1000002</AgreementStatusType><AgreementType>1000002</AgreementType><TCRMExtension><ExtendedObject>CMSContractBObjExt</ExtendedObject><CMSContractBObjExt><ClientGenType>1000002</ClientGenType></CMSContractBObjExt></TCRMExtension><TCRMContractComponentBObj><ContractStatusType>1000001</ContractStatusType><TCRMContractPartyRoleBObj><RoleType>1000001</RoleType><TCRMOrganizationBObj><TCRMAdminContEquivBObj><AdminPartyId></AdminPartyId><AdminSystemType>1000001</AdminSystemType></TCRMAdminContEquivBObj></TCRMOrganizationBObj></TCRMContractPartyRoleBObj></TCRMContractComponentBObj><TCRMAdminNativeKeyBObj><AdminContractId></AdminContractId><AdminFieldNameType>1000001</AdminFieldNameType></TCRMAdminNativeKeyBObj></TCRMContractBObj></TCRMObject></TCRMTx></TCRMService>


<?xml version="1.0" encoding="UTF-8"?><TCRMService xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="CMSRequest.xsd"><RequestControl><requestID>103</requestID><DWLControl><requesterName>Automated Cleanup TC1</requesterName><requesterLanguage>100</requesterLanguage><clientTransactionName></clientTransactionName></DWLControl></RequestControl><TCRMTx><TCRMTxType>maintainGlobalClientHierarchy</TCRMTxType><TCRMTxObject>TCRMContractBObj</TCRMTxObject><TCRMObject><TCRMContractBObj><CurrencyType>1</CurrencyType><AgreementName>Moretti New GC</AgreementName><AgreementNickName></AgreementNickName><AgreementStatusType>1000002</AgreementStatusType><AgreementType>1000002</AgreementType><TCRMExtension><ExtendedObject>CMSContractBObjExt</ExtendedObject><CMSContractBObjExt><ClientGenType>1000002</ClientGenType></CMSContractBObjExt></TCRMExtension><TCRMContractComponentBObj><ContractStatusType>1000001</ContractStatusType><TCRMContractPartyRoleBObj><RoleType>1000001</RoleType><TCRMOrganizationBObj><TCRMAdminContEquivBObj><AdminPartyId></AdminPartyId><AdminSystemType>1000001</AdminSystemType></TCRMAdminContEquivBObj></TCRMOrganizationBObj></TCRMContractPartyRoleBObj></TCRMContractComponentBObj><TCRMAdminNativeKeyBObj><AdminContractId></AdminContractId><AdminFieldNameType>1000001</AdminFieldNameType></TCRMAdminNativeKeyBObj></TCRMContractBObj></TCRMObject></TCRMTx></TCRMService>


<?xml version="1.0" encoding="UTF-8"?><TCRMService xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="CMSRequest.xsd"><RequestControl><requestID>104</requestID><DWLControl><requesterName>Automated Cleanup TC1</requesterName><requesterLanguage>100</requesterLanguage><clientTransactionName></clientTransactionName></DWLControl></RequestControl><TCRMTx><TCRMTxType>maintainGlobalClientHierarchy</TCRMTxType><TCRMTxObject>TCRMContractBObj</TCRMTxObject><TCRMObject><TCRMContractBObj><CurrencyType>1</CurrencyType><AgreementName>Moretti New GC</AgreementName><AgreementNickName></AgreementNickName><AgreementStatusType>1000002</AgreementStatusType><AgreementType>1000002</AgreementType><TCRMExtension><ExtendedObject>CMSContractBObjExt</ExtendedObject><CMSContractBObjExt><ClientGenType>1000002</ClientGenType></CMSContractBObjExt></TCRMExtension><TCRMContractComponentBObj><ContractStatusType>1000001</ContractStatusType><TCRMContractPartyRoleBObj><RoleType>1000001</RoleType><TCRMOrganizationBObj><TCRMAdminContEquivBObj><AdminPartyId></AdminPartyId><AdminSystemType>1000001</AdminSystemType></TCRMAdminContEquivBObj></TCRMOrganizationBObj></TCRMContractPartyRoleBObj></TCRMContractComponentBObj><TCRMAdminNativeKeyBObj><AdminContractId></AdminContractId><AdminFieldNameType>1000001</AdminFieldNameType></TCRMAdminNativeKeyBObj></TCRMContractBObj></TCRMObject></TCRMTx></TCRMService>

Last edited by adept; 05-11-2012 at 07:00 PM.. Reason: Pasting the original message
# 17  
Old 05-11-2012
What is in your input file? Is it possible that you have one line with valid data and three blank lines? Awk will execute your print statements for each line of input -- whether or not there is information on the line. You could try this:

Code:
BEGIN{
   FS=",";
   requestID=100;
   ORS="";
 }
NF > 1  {
 print "<?xml version=\"1.0\" encoding=\"UTF-8\"?><TCRMService xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \
xsi:noNamespaceSchemaLocation=\"CMSRequest.xsx\"><RequestControl><requestID>"requestID"</requestID><DWLControl>\
<requesterName>Automated Cleanup TC1</requesterName><requesterLanguage>100</requesterLanguage><clientTransactionName>\
"$1"</clientTransactionName></DWLControl></RequestControl>";

 print "<TCRMTx><TCRMTxType>maintainGlobalClientHierarchy</TCRMTxType><TCRMTxObject>TCRMContractBObj</TCRMTxObject>\
<TCRMObject><TCRMContractBObj><CurrencyType>1</CurrencyType><AgreementName>Moretti New GC</AgreementName><AgreementNickName>\
</AgreementNickName><AgreementStatusType>1000002</AgreementStatusType><AgreementType>1000002</AgreementType>";

 print "<TCRMExtension><ExtendedObject>CMSContractBObjExt</ExtendedObject><CMSContractBObjExt><ClientGenType>1000002</ClientGenType>\
</CMSContractBObjExt></TCRMExtension><TCRMContractComponentBObj><ContractStatusType>1000001</ContractStatusType><TCRMContractPartyRoleBObj>\
<RoleType>1000001</RoleType><TCRMOrganizationBObj>";

 print "<TCRMAdminContEquivBObj><AdminPartyId>"$2"</AdminPartyId><AdminSystemType>1000001</AdminSystemType></TCRMAdminContEquivBObj>\
</TCRMOrganizationBObj></TCRMContractPartyRoleBObj></TCRMContractComponentBObj><TCRMAdminNativeKeyBObj><AdminContractId>"$3"\
</AdminContractId><AdminFieldNameType>1000001</AdminFieldNameType></TCRMAdminNativeKeyBObj></TCRMContractBObj></TCRMObject>\
</TCRMTx></TCRMService>\n";

 }' input-file >output-file



Which will execute your print statements only if there are 2 or more tokens on the line.

Last edited by agama; 05-11-2012 at 10:57 PM.. Reason: typo
This User Gave Thanks to agama For This Post:
# 18  
Old 05-11-2012
Issues running an awk script

Thanks agama.

My Bad. As you mentioned, There were three more empty lines after the first line in the input file.

Last edited by adept; 05-11-2012 at 11:50 PM.. Reason: Resolved
# 19  
Old 05-11-2012
No problem. For what it's worth -- I wasn't able to pull your attachments, but it seems you've figured it out.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem running awk script in Debian 6.0.2

Hello to all, May be some expert could help me. I have the below awk script that works correctly in Cygwin: awk -F\" 'FNR==NR && FNR>1 {gsub(",","",$5); N=$5;next} $10 in N && FNR>1 { Y=$8 Z=(N==$8)?"Yes:"No"; } END{ for(k in N) {print k,Y,N,Z?Z:"Not_Found"} }' file2 file1But when I... (11 Replies)
Discussion started by: Ophiuchus
11 Replies

2. UNIX for Dummies Questions & Answers

Facing issues while running a cronjob !

Hi, I am trying to run a cronjob. But while doing so I am getting the following error message :- can't open yourfile in /var/spool/cron/crontabs directory. No such file or directory How can I resolve this issue ? Please help. Thanks Please view this code tag video for... (14 Replies)
Discussion started by: acidburn_007
14 Replies

3. UNIX for Dummies Questions & Answers

awk: issues for writing a script

%%%%% (7 Replies)
Discussion started by: lucasvs
7 Replies

4. Shell Programming and Scripting

Problem with running awk script in pbs file

Hi guys I have a problem with one module of my script. It's awk script running in pbs file - It's working when I make a call from pbs to separate awk only file like this but when I try to execute this code in pbs file it fails awk 'BEGIN { max = -1000; min = 1000 } { {$4 == "TCO"} ... (2 Replies)
Discussion started by: grincz
2 Replies

5. Shell Programming and Scripting

AWK - Issues with script running at start of month.

I have a script that runs on an AIX 5.3.10.0. It runs perfectly on days that are double digit numbers, but from the 1st to the 9th of the month it runs but does not report anything. The command in the script is as follows: awk -v d=$(date '+%b%d') '/user:warn/ && /has shutdown/ && ($1$2 ==... (2 Replies)
Discussion started by: jimbojames
2 Replies

6. UNIX for Dummies Questions & Answers

swap issues, system is running at 99%

Hi All, I am trying to understand why my system is running at very high. This system is almost out of memory. See below. swapon -s Filename Type Size Used Priority /dev/mapper/VolGroup00-LogVol02 partition 8388600 8235088 -1... (2 Replies)
Discussion started by: samnyc
2 Replies

7. Red Hat

postgresql issues - system running really slow

Hi All, I'm new here. i was wondering if anyone could shed a light on the problem i am having. I use a system for distributing broadband amongst users of for example a hotel, the system was designed by someone in the US and it is based on redhat 2.4 (i know its old) and the system uses... (3 Replies)
Discussion started by: clive306
3 Replies

8. Shell Programming and Scripting

Issues with exit after running jobs in background

I have the following sample script to run a script the jobs with the same priority(in this case field3) in parallel; wait for the jobs to finish and run the next set of jobs in parallel.When all the lines are read exit the script. I have the following script which is doing evrything I want... (1 Reply)
Discussion started by: hyennah
1 Replies

9. UNIX for Advanced & Expert Users

Problem in running an AWK script

Hi Everyone, I am required to write an AWK script that can be run from any directory. This script finds subscription rates for each subscriber at an interval of 10 mins. I want that just by copy pasting the whole script in shell prompt and giving the input arguments, script must work. Currenty... (4 Replies)
Discussion started by: rachana8p
4 Replies

10. Shell Programming and Scripting

is running this command via ssh possible? (formatting issues)

Here is the command I want to run: for pkg in `pkginfo | grep -i VRTS | awk '{print $2}'`; do showrev -p | grep $pkg; done | awk '{print $2 "\t" $7}' | uniq It returns the package info in a form such as: 113210-03 VRTSfspro 112392-06 VRTSvmman 113596-03 VRTSvmpro... (1 Reply)
Discussion started by: LordJezo
1 Replies
Login or Register to Ask a Question