Regex to extract content from each line of a log file output from '_m' to the end of the line

StackOverflow https://stackoverflow.com/questions/15839254

  •  01-04-2022
  •  | 
  •  

Question

Format of log line:

Xxx x xx:xx:xx xmmxxx XXXXXX: XXXXXXX:XXX: xxx_Mxxx_Xxxxxx_mxxxxxmmxx [XXX xxxx.

I want to extract from '_m' to the end of the line, removing the '_' before the 'm'.

New to regex...

Thanks!

Was it helpful?

Solution

if your tool/language support look-behind, this works: match the first _m till EOL. also ignore the leading _

(?<=_)m.*

test with grep:

kent$  echo "Xxx x xx:xx:xx xmmxxx XXXXXX: XXXXXXX:XXX: xxx_Mxxx_Xxxxxx_mxxxxxmmxx [XXX xxxx."|grep -Po '(?<=_)m.*' 
mxxxxxmmxx [XXX xxxx.

OTHER TIPS

With sed:

sed -n 's/^.*_\(m.*$\)/\1/p' file

It is quite easy:

This example is written in C# however the regex is quite general and will probably work anywhere:

Regex regex = new Regex(@"_(m.*)");  // If you look for _M the regex should be @"_(M.*)"

Match match = regex.Match(logLine);
if (match.Success)
    Console.WriteLine(match.Groups[1].Value);

Hope this will help you on your quest.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top