t=: 0 : 0
att0=4010&att7=2457&att2=439
att3=902&att2=413&att5=4262&att4=4967
att5=4040&att1=465

att4=2733
att3=2397&att2=1104&att6=2625
)

NB. y: log file lines, may be terminated by CRLF or LF
NB. x: required attributes
tab=: 4 : 0
 y=. y-.CR
 av=. a: -.~ (y e. '&',LF) <;._2 y  NB. att=value strings
 k=. av i.&> '='
 a=. k {.&.> av                     NB. attribute names
 v=. (1+k) }.&.> av                 NB. values
 b=. a e. x                         NB. attributes of interest
 n=. (y e. LF) +/;._2 y='='         NB. # attributes on each line
 m=. (;n{.&.>1) +/;.1 b             NB. # attributes of interest in each line
 x , (m,&#x)$(b#v) ((m#(#x)*i.#m)+x i. b#a)}(m*&#x)$a:
)

   (<;._1 ' att0 att1 att2') tab t
+----+----+----+
|att0|att1|att2|
+----+----+----+
|4010|    |439 |
+----+----+----+
|    |    |413 |
+----+----+----+
|    |465 |    |
+----+----+----+
|    |    |    |
+----+----+----+
|    |    |1104|
+----+----+----+

If lines are separated by CRLF or LF rather than terminated,
you have to append a LF to the right argument of tab .



----- Original Message -----
From: "Lettow, Kenneth" <[EMAIL PROTECTED]>
Date: Tuesday, November 11, 2008 12:38
Subject: [Jprogramming] Extracting values from log strings
To: [email protected]

> I have some log file data stored as long strings of (attribute=value)
> pairs.  Here is some dummy data representing a few lines: 
> ------------------------------------------------------------
> att0=4010&att7=2457&att2=439                                       
> att3=902&att2=413&att5=4262&att4=4967                              
> att5=4040&att1=465
> 
> att4=2733                                                          
> att3=2397&att2=1104&att6=2625   
> ------------------------------------------------------------
> Each of the (attribute=value) pairs is delimited by "&", and the
> attribute name is separated from the value by an "=" character.
> 
> Each of the lines can have an arbitrary number of attributes, 
> includinglines with no attributes.
> 
> Values can be strings or numbers, necessitating boxed results.
> 
> Attributes can appear in any order on a line (i.e. att0 can occur
> before/after att1 etc.).
> 
> What I want to do is to take the values from certain attributes 
> and put
> them into a table, i.e. take the values of att0, att1 and att2 
> in dummy
> data (above) and create the following table.
> 
> +----+----+----+
> |att0|att1|att2|
> +----+----+----+
> |4010|    |439 |
> +----+----+----+
> |    |    |413 |
> +----+----+----+
> |    |465 |    |
> +----+----+----+
> |    |    |    |
> +----+----+----+
> |    |    |1104|
> +----+----+----+
> 
> Below is what I have so far. I cannot figure out the last steps.
> 
> NB. Verb to generate dummy data
> genattdat=: 3 : 0
>    NB. y = #lines testdata to generate
>   numlines=. y
>   
>   NB. Maximum attributes per line (for demo purposes)
>   MAXATTS=. 8
>   MAXVAL=. 5000  NB. Max dummy value
>   
>   NB. Create attribute list
>   attrlist=: 'att'&, each cut":i.MAXATTS
>   
>   res=. ''
>   for. i.>:y do.
>     randatts=. (~.(? (?#attrlist) $ 
> (#attrlist))){attrlist    randvals=. ":each 
> <"0 ((#randatts) ?MAXVAL)
>     res=. res,(<}:;;<"1 
> randatts,.(<'='),.randvals,.(<'&'))  end.
>   ]res=. >}.res
> )
> 
> NB. Generate 5 lines of dummy data
>    ]testdata=: genattdat 5
> att5=1830                                        
> att2=3371&att6=3188&att3=536&att4=1176&att7=1832 
> att0=1732&att7=4102&att3=797                     
> att6=4278&att1=3480&att2=4333&att5=2933&att7=1160
> att1=808&att2=2321&att4=3544&att5=3794   
> 
> NB. Parsed attributes
> 
>    ]atts=: deb each >{."1 each > each <"1 '='&cut 
> each '&'&cut"1
> testdata
> +----+----+----+----+----+
> |att5|    |    |    
> |    |
> +----+----+----+----+----+
> |att2|att6|att3|att4|att7|
> +----+----+----+----+----+
> |att0|att7|att3|    |    |
> +----+----+----+----+----+
> |att6|att1|att2|att5|att7|
> +----+----+----+----+----+
> |att1|att2|att4|att5|    |
> +----+----+----+----+----+
> 
> NB. Parsed values
>    ]vals=: deb each >,@}.@|: each > each <"1 
> '='&cut each '&'&cut"1
> testdata
> +----+----+----+----+----+
> |1830|    |    |    
> |    |
> +----+----+----+----+----+
> |3371|3188|536 |1176|1832|
> +----+----+----+----+----+
> |1732|4102|797 |    |    |
> +----+----+----+----+----+
> |4278|3480|4333|2933|1160|
> +----+----+----+----+----+
> |808 |2321|3544|3794|    |
> +----+----+----+----+----+
> 
> NB. Attributes I want to keep and put into a table
> 
>    ]keepattributes=: 'att'&, each cut":i.3
> +----+----+----+
> |att0|att1|att2|
> +----+----+----+
> 
> NB. mask of attributes to keep
> 
>    ]keepmsk=: (#keepattributes)>keepattributes i. atts
> 0 0 0 0 0
> 1 0 0 0 0
> 1 0 0 0 0
> 0 1 1 0 0
> 1 1 0 0 0
> 
> NB. attributes to keep
>  ]keepatts=: keepmsk #"1 1 atts
> +----+----+
> |    |    |
> +----+----+
> |att2|    |
> +----+----+
> |att0|    |
> +----+----+
> |att1|att2|
> +----+----+
> |att1|att2|
> +----+----+
>    
> NB. values to keep
>   ]keepvals=: keepmsk #"1 1 vals
> +----+----+
> |    |    |
> +----+----+
> |3371|    |
> +----+----+
> |1732|    |
> +----+----+
> |3480|4333|
> +----+----+
> |808 |2321|
> +----+----+
> 
> It is here that I get stuck.  How do I get keepatts and 
> keepvals to this
> form?
> 
> NB. Hand built result
> +----+----+----+
> |att0|att1|att2|
> +----+----+----+
> |    |    |    |
> +----+----+----+
> |    |    |3371|
> +----+----+----+
> |1732|    |    |
> +----+----+----+
> |    |3480|4333|
> +----+----+----+
> |    |808 |2321|
> +----+----+----+
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to