1832182 Members
2862 Online
110038 Solutions
New Discussion

Re: F$EXTRACT Help

 
Joshua Gould
Advisor

F$EXTRACT Help


I hope this is something pretty basic but I'm having problems with F$EXTRACT break strangely on white space.

Example of the log I am parsing:
Cache (HOST:20200444) Fri Jun 29 11:40:20 2007
Cache (HOST:20200444) Mon Jul 2 06:22:06 2007

The related code snipets:
$ EXACT_TIME = F$ELEMENT(6," ", LOG)
$ MESSAGE_HOUR = F$EXTRACT(0,4,EXACT_TIME)
$ WRITE SYS$OUTPUT MESSAGE_HOUR

The output:
2007
06:2

I wanted only the first two digits for the hour but to ensure that F$EXTRACT was getting confused with the year, I expanded it to four digits.
3 REPLIES 3
Ian Miller.
Honored Contributor

Re: F$EXTRACT Help

element 0 = Cache
element 1 = (HOST:20200444)
element 2 = Mon
element 3 = Jul
element 4 = 2
element 5 = 06:22:06
element 6 = 2007

Your three lines of DCL gives 2007 as the output. Element 5 is the time. Then you could use F$ELEMENT(0,":",EXACT_TIME)
to get the hour.
____________________
Purely Personal Opinion
Hein van den Heuvel
Honored Contributor

Re: F$EXTRACT Help

It seems to me you fail to recognize that F$ELEMENT does not recognize natural words, but simply counts every single seperator.

I suspect that the line "jul 2" actually has an extra space, although that is not visible in this forum output.
If you want to pick up the 6'th word then you first have to compress mulltiple whitespace characters into 1.

Probably something like:
$ log_compressed = F$EDIT(LOG,"COMPRESS")
$ EXACT_TIME = F$ELEMENT(6," ", log_compressed)

Other example:
$ write sys$output f$ele(1," ","a b c")
b
$ write sys$output f$ele(1," ","a b c")

$ write sys$output f$ele(1," ",f$edit("a b c","compress"))
b
$


What problem are you really trying to solve?
I find that parsnig log file is MUCH easier in PERL or AWK once you get going a little bit with those tools.

In this case for example:

$ perl -lne "print $1 if /(\d+):\d\d:\d\d/" tmp.txt
11
06

The "REGULAR EXPRESSION" /(\d+):\d\d:\d\d/ give something to match on.
/ = start RE
( = start remembering in variable $1
\d+ = one or more of decimals (hours)
) = stop remembering
:\d\d = a piece of string starting with a colon and followed by two decimals (for the minutes)
:\d\d = a second piece of string starting with a colon and followed by two decimals (for the seconds)
/ = end RE

-l = print newline after each print
-n = loop through input file
-e = program text following.

Good luck!
Hein.






Joshua Gould
Advisor

Re: F$EXTRACT Help

Yes. The second white space is what threw me off. I went ahead with a solution using F$EXTRACT.