Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

value on some lines but no others, extracting field problem 1

Status
Not open for further replies.

LGJ

Programmer
Mar 7, 2003
50
GB
Please help:

I have a data file which has many values per line eg:

lemur/front-stats-2003102459:<apiCall start=&quot;1066950214507&quot; end=&quot;1066950216147&quot; remoteHost=&quot;xxx&quot; remoteAddr=&quot;xxx&quot; contractID=&quot;oranlc1&quot; serviceName=&quot;BillingServices.Content.ChargeReservation&quot;><apiResponse success=&quot;yes&quot;></apiResponse>
</apiCall>
lemur/front-stats-2003102459:<apiCall start=&quot;1066950219273&quot; end=&quot;1066950219739&quot; remoteHost=&quot;xxx&quot; remoteAddr=&quot;xxx&quot; contractID=&quot;oranlc1&quot; serviceName=&quot;BillingServices.Content.ChargeConfirmation&quot;><apiResponse success=&quot;yes&quot;></apiResponse
></apiCall>
lemur/front-stats-2003102459:<apiCall start=&quot;1066950219903&quot; end=&quot;1066950225208&quot; remoteHost=&quot;xxx&quot; remoteAddr=&quot;xxx&quot; contractID=&quot;akumiitti2orangelc1&quot; serviceName=&quot;BillingServices.Content.ChargeConfirmation&quot;><apiResponse success=&quot;no&quot;><reason><code>
107</code></reason></apiResponse></apiCall>


the above shows 3 lines (start with lemur, end with </apiCall>)

Originally I only wanted the start_t, end_t, charge_request and the success value so the following statement was sufficient:

awk -F\&quot; '{print $2&quot;,&quot;$4&quot;,&quot;$12&quot;,&quot;$14}' $InputFile > $OutputFile

I now need the code value if the success = no (between <code> and </code>) but this is not between double quotes AND is only on the failed transaction lines eg 3rd line of given example.

Please can someone suggest a solution to this, it has been racking my brains and I produced a crude solution using sed and awk to replace unwanted text!! Surely there is something in awk to solve this?

Thanks all

Lee
 
Try this...

awk -F\&quot; '{split($15,a,&quot;[<>]&quot;);print $2&quot;,&quot;$4&quot;,&quot;$12&quot;,&quot;$14&quot;,&quot;a[6]}' $InputFile > $OutputFile
 
Try this :

awk -F\&quot; '
{
if ($14 == &quot;no&quot;/) {
code = $0 ;
sub(&quot;.*<code>&quot;,&quot;&quot;,code);
sub(&quot;</code>.*&quot;,&quot;&quot;,code);
code = &quot;,&quot; code;
} else {
code = &quot;&quot;;
}
print $2&quot;,&quot;$4&quot;,&quot;$12&quot;,&quot;$14 code ;
} ' $InputFile > $OutputFile ;


Jean Pierre.
 
Thanks for your time but I cant seem to get any of these working properly:

Ygor I get this kind of output:

1066950219273,1066950219739,BillingServices.Content.ChargeConfirmation,yes,
1066950219903,1066950225208,BillingServices.Content.ChargeConfirmation,no,/apiResponse>

I have messed around with the array value but still cant get just the code value eg 107

Jean Pierre I get syntax errors being shown, I like this solution as I can understand what is happening but I cannot seem to work out why it wont run? ( i have removed / after no as I dont think this is needed also)

Any ideas

thanks

Lee
 
With / removed, the script works fine on my system (AIX).
The result is :

1066950214507,1066950216147,BillingServices.Content.ChargeReservation,yes
1066950219273,1066950219739,BillingServices.Content.ChargeConfirmation,yes
1066950219903,1066950225208,BillingServices.Content.ChargeConfirmation,no,107

What errors did you get ?

Jean Pierre.
 
And something like this ?
awk '{a=&quot;&quot;;b=&quot;&quot;;c=&quot;&quot;;d=&quot;&quot;}
/start=/{a=$0;sub(/.*start=&quot;/,&quot;&quot;,a);sub(/&quot;.*/,&quot;&quot;,a)}
/end=/{b=$0;sub(/.*end=&quot;/,&quot;&quot;,b);sub(/&quot;.*/,&quot;&quot;,b)}
/serviceName=/{c=$0;sub(/.*serviceName=&quot;/,&quot;&quot;,c);sub(/&quot;.*/,&quot;&quot;,c)}
/<code>/{d=$0;sub(/.*<code>/,&quot;,&quot;,d);sub(/<.*/,&quot;&quot;,d)}
{printf &quot;%s,%s,%s%s\n&quot;,a,b,c,d}
' $InputFile > $OutputFile

Hope This Help
PH.
 
aigles/Jean Pierre:

I get the following:

pin:/export/home/pin/temp/ltemp> awk -F\&quot; '
{
if ($14 == &quot;no&quot;) {
code = $0 ;
sub(&quot;.*<code>&quot;,&quot;&quot;,code);
sub(&quot;</code>.*&quot;,&quot;&quot;,code);
code = &quot;,&quot; code;
} else {
code = &quot;&quot;;
}
print $2&quot;,&quot;$4&quot;,&quot;$12&quot;,&quot;$14 code ;
} ' testFile > OutputFile;
awk: syntax error near line 5
awk: illegal statement near line 5
awk: syntax error near line 6
awk: illegal statement near line 6
 
I think you are using an old version of awk which does not support the sub function.

Try to use nawk instead of awk.

Jean Pierre.
 
cool aigles, it now compiles fine (with nawk) but the output I get is:

1066950214507,1066950216147,BillingServices.Content.ChargeReservation,yes
,,,
1066950219273,1066950219739,BillingServices.Content.ChargeConfirmation,yes
,,,
1066950219903,1066950225208,BillingServices.Content.ChargeConfirmation,no
,,,

The code you produced looks like it should work though?

Thanks

Lee
 
Empty input lines gives &quot;,,,&quot; in output.
Are you sure that your input file contains only valid lines (starting with lemur and ending with </apiCode>.

You can modify the script to select only this lines :

awk -F\&quot; '
/^lemur.*<\/apiCall>$/ {
if ($14 == &quot;no&quot;) {
code = $0 ;
sub(&quot;.*<code>&quot;,&quot;&quot;,code);
sub(&quot;</code>.*&quot;,&quot;&quot;,code);
code = &quot;,&quot; code;
} else {
code = &quot;&quot;;
}
print $2&quot;,&quot;$4&quot;,&quot;$12&quot;,&quot;$14 code ;
} ' txt

I don't understand why the code isn't displayed for the last line.
The script works fine on my system whith your input example.


Jean Pierre.
 
Thank you Jean Pierre (Aigles) for your help.

I had concatenated one of the lines and $0 was incomplete for the line with <code> on it!

I have learnt some new skills today.

Cheers

Lee
 
My earlier post should have looked like this (but it didn't because I left TGML on)...

awk -F\&quot; '{split($15,a,&quot;[<>]&quot;);print $2&quot;,&quot;$4&quot;,&quot;$12&quot;,&quot;$14&quot;,&quot;a[6]}' $InputFile > $OutputFile
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top