Skip to main content
Solved

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

  • December 16, 2022
  • 3 replies
  • 175 views

tb09114
Supporter
Forum|alt.badge.img+22

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

Best answer by dustin

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}\|(.*?)$

 

View original
Did this help you find an answer to your question?

3 replies

dustin
Influencer
Forum|alt.badge.img+30
  • Influencer
  • Best Answer
  • December 16, 2022

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}\|(.*?)$

 


ebygomm
Influencer
Forum|alt.badge.img+32
  • Influencer
  • December 16, 2022

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.

 

 


tb09114
Supporter
Forum|alt.badge.img+22
  • Author
  • Supporter
  • December 16, 2022
dustin wrote:

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.

 

 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings