Solved

How to return specific lines from log file based on regex?

  • 16 December 2022
  • 3 replies
  • 53 views

Userlevel 1
Badge +8

I want to extract specific lines from a log file, based on an regex. However, instead of getting a whole line as match returned, or even better a list of matches, I am getting <null> returned.

 

What I want from the log file are all the warnings. I used an AttributManager and set the Attribute Value for the new attribute to 

@SubstringRegularExpression(@Value(_log_attr),^.*\|WARN\s{2}\|.*$,0,0,5)

The last parameter is already misleading, since I usually do not have an idea of how many warnings there might be in an arbitrary log file, but for the sake of my test it would work.

I would expect that the first match (out of 5) is returned. Ideally, a startIdx of -1 would give me the last of the five matches, but that doesn't work either.

 

For the fun of it I was testing the same expression within a StringSearcher and here, there is a match. However, the attribute containing the matched result, who is supposed to return "the portion of the original search string that matched the regular expression" is returning the entire content of the log file.

 

Does somebody know how to parse a log-file within FME to separate warnings and errors into lists?

 


https://docs.safe.com/fme/2022.0/html/DataInterop_Documentation/FME_Transformers/Transformers/stringsearcher.htm

icon

Best answer by dustin 16 December 2022, 13:35

View original

3 replies

Userlevel 3
Badge +26

I originally answered differently, but then I noticed you needed a list. Apologies if that caused confusion. Using this regex should get what you want from the StringSearcher.

\|warn\s{2}\|(.*?)$

 

Userlevel 1
Badge +21

The issue is in FME .* matches a newline character, so your regex is greedy and hence why you see the whole string in the string searcher, it's matching everything in front of the word WARN from the start of the first line to everything after to the end of the last line

 

You could do something like this in the StringSearcher to get a list, set Case Sensitive to Yes

[^\n]+WARN[^\n]+

which would match everything in front of and behind the word WARN except for new line characters, i.e. it will return the whole line and return a list of those matches.

 

 

Userlevel 1
Badge +8

I originally answered differently, but then I noticed you needed a list. Apologies if that caused confusion. Using this regex should get what you want from the StringSearcher.

\|warn\s{2}\|(.*?)$

 

I saw you replied something, but couldn't open it in the forum...

my simplified workflow

workflow with StringSearcherIt works in the StringSearcher, and the explanation from @ebygomm​ tells the reason why I am getting the entire log file. I love the possibility to use regex in FME, but these non-standard behaviour makes it prettyawkward to work with FME in this context.

 

 

Reply