DATA Step, Macro, Functions and more

Conditionally read .log file

Reply
Super Contributor
Posts: 426

Conditionally read .log file

I've a .log file (for example) as below.

Host: 'tmptcmsaslva2', OS: 'LIN X64', Release: '2.6.32-431.3.1.el6.x86_64', SAS Version: '9.03.01M2P08152012', Command: '/usr/sas/sas9.3/SASFoundation/9.3/sasexe/sas -noterminal -netencryptalgorithm SASProprietary -metaserver tmptcmsaslva2.timeinc.com -metaport 8561 -metarepository Foundation -objectserver -objectserverparms "protocol=bridge spawned spp=45106 cid=0 classfactory=15931E31-667F-11D5-8804-00C04F35AC8C server=OMSOBJSmiley FrustratedERVERCOMPONENT/A52GREI3.AV000006 cel=credentials dnsMatch=tmptcmsaslva2.timeinc.com multiuser port=8611 lb saslangrunas=client applevel=3"'

Log continued from /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-19_tmptcmsaslva2_19142.log

2015-08-20T00:00:05,082 INFO  [04086681] :sassr - New out call client connection (74065) for user

2015-08-20T00:00:05,092 INFO  [04086681] :datapo@saspw - New client connection (74064) accepted from server port 8611 for SAS token user Encryption level is Credentials using encryption algorithm SASPROPRIETARY.  Peer IP address and port are [10.176.232.41]:39494.

2015-08-20T00:00:05,094 INFO  [04086689] 74064:datapo@saspw - STP: 36518: Creating New Context

In this log file I need to read the records from line number 6 and I need to produce the output as below. Please note that the field 'userid' is not a fixed width delimited. The value should be read before the hyphen -

Date_With_TimeStamp Status Processid userid Details
2015-08-20T00:00:05,082INFO[04086681]:sassrvNew out call client
  connection (74065) for user
2015-08-20T00:00:05,092INFO[04086681]:datapo@saspwNew client connection
  (74064) accepted from server port 8611 for SAS token user Encryption level is
  Credentials using encryption algorithm SASPROPRIETARY.  Peer IP address
  and port are [10.176.232.41]:39494
2015-08-20T00:00:05,094INFO[04086689]74064:datapo@saspwSTP: 36518: Creating
  New Context

I tried the code like below, but it not producing the output as I look for. Need some tips (or code) to accomplish this task effectively.

data log_analysis;

infile '/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-19_tmptcmsaslva2_19142.log' firstobs=6 dsd truncover;

input Date_With_TimeStamp $ 1-23 Status $ 25-28 Processid $
31-38 userid @ ;

<need code to extract this field as per the rules said above>

run;

Thanks for any help you  offer me.

Super User
Super User
Posts: 7,413

Re: Conditionally read .log file

Hi,

Some tips:

Read the file in as a whole:

input line $2000;

Then use an if statement:

if _n_=6 then do; ... end;

Then split line up based on your crtieria, using string find and substring commands.

Super Contributor
Posts: 426

Re: Conditionally read .log file

Thanks for your tips.

What is '6' in your code? May I request you to elaborate your tips?

I was looking for the do loop where you mentioned as ..

It would be great if you can help me to complete the do loop.

Thanks.

Super User
Super User
Posts: 7,413

Re: Conditionally read .log file

_n_ refers to the observation number, I took six from your text "In this log file I need to read the records from line number 6".

Afraid I don't have time for a complete parsing program, date with timestamp should be straightforward you can probably scan() most of them:

timestamp=scan(line,1,' ');

status=scan(line,2,' ');

Have a read of the documentation on string manipulation = find, index, scan, substr etc.

Respected Advisor
Posts: 3,777

Re: Conditionally read .log file

When I copy your records it looks like you start at line 4 not 6 and you need to include LRECL option to unsure your records are not truncated.  The records can be read with blank delimited list input until you get to DETAILS and then you just want the rest of the records so you can switch to formatted input and let TRUCOVER keep you from FLOWingOVER.

filename FT15F001 temp lrecl=1024;
data log_analysis;
  infile FT15F001 firstobs=4 truncover;
 
input Date_With_TimeStamp :E8601DT. Status :$4. Processid :$12. userid:$16. dash :$1. details $1024.;
 
drop dash;
  format Date: E8601DT24.3;
 
parmcards4;

Host:
'tmptcmsaslva2', OS: 'LIN X64', Release: '2.6.32-431.3.1.el6.x86_64', SAS Version: '9.03.01M2P08152012', Command: '/usr/sas/sas9.3/SASFoundation/9.3/sasexe/sas -noterminal -netencryptalgorithm SASProprietary -metaserver tmptcmsaslva2.timeinc.com -metaport 8561 -metarepository Foundation -objectserver -objectserverparms "protocol=bridge spawned spp=45106 cid=0 classfactory=15931E31-667F-11D5-8804-00C04F35AC8C server=OMSOBJSmiley FrustratedERVERCOMPONENT/A52GREI3.AV000006 cel=credentials dnsMatch=tmptcmsaslva2.timeinc.com multiuser port=8611 lb saslangrunas=client applevel=3"'
Log continued from /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-
08-19_tmptcmsaslva2_19142.log
2015-08-20T00:00:05,082 INFO  [04086681] :sassr - New out call client connection (74065) for user
2015-08-20T00:00:05,092 INFO  [04086681] :datapo@saspw - New client connection (74064) accepted from server port 8611 for SAS token user Encryption level is Credentials using encryption algorithm SASPROPRIETARY.  Peer IP address and port are [10.176.232.41]:39494.
2015-08-20T00:00:05,094 INFO  [04086689] 74064:datapo@saspw - STP: 36518: Creating New Context
;;;;
 
run;
proc print;
 
run;


Contributor
Posts: 65

Re: Conditionally read .log file

data Want;

infile "path\filename.txt" dlm='^';

input var : $ 3000.;

var1 = _infile_;

if var1 = :'2015';

Date_With_TimeStamp = scan(var1,1," ");

Status = scan(var1,2," ");

Processid = scan(var1,3," ");

userid = scan(var1,4," ");

Details = scan(var1,-1,'-');

drop var var1;

run;

Ask a Question
Discussion stats
  • 5 replies
  • 291 views
  • 0 likes
  • 4 in conversation