2

I want start my starts with "["(square bracket). After that I need to find constant string which is "FIELDS THROWING ERROR =>"(constant string), the string will occur after some lines in string. Next, I need to take one word(the word will be dynamic) after constant string then I have to stop after successfully matches the pattern.

sample string: 

------------------------------------------------
Start Method SYNC DATA :: xxx : 5/19/2022 11:09:28 PM : Total Sync Time : 0.00
----------------------------------------------
[xxx][xxx] Upsert Failed : 
     RECORD NUMBER => ABC:000000
     ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry
     FIELDS THROWING ERROR => Bilcntry
[xxx][xxx] Upsert Failed : 
    RECORD NUMBER => ABC:000000
    ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry
    FIELDS THROWING ERROR => Bilcntry
[xxx][xxx] Upsert Failed : 
    RECORD NUMBER => ABC:000000
    ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry
    FIELDS THROWING ERROR => Bilcntry
[xxx][xxx] Upsert Failed : 
    RECORD NUMBER => ABC:000000
    ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry
    FIELDS THROWING ERROR => Bilcntry

Desire Output:

[xxx][xxx] Upsert Failed : 
     RECORD NUMBER => ABC:000000
     ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry
     FIELDS THROWING ERROR => Bilcntry

Can anyone help me?

Thanks

Michael
  • 3,510
  • 1
  • 11
  • 23
  • Do you need it in some program, or would bash script help you as well? For examle grep can give you some previous lines with mached line as well -- from what i see as sample, you want to get matching row and three rows before it. – Lauri May 25 '22 at 07:31
  • Not using grep I only need regex for matching first string occurs –  May 25 '22 at 07:45

3 Answers3

2

This may be help as this will match the first occurrence of given patterns.

\[.*]\[.*] .*: \n(?<message>(.|\n)*?) FIELDS THROWING ERROR => (\w.*)

The results is like this enter image description here

Sam
  • 516
  • 3
  • 7
1
^\[.*([A-Z\s]*=>[ A-Za-z0-9\:\[\]\-\_\,\.\?\'\/\“\”\"\(\)\;\!\@\#\$\%\^\&\*\{\}\|\\\+]*)*

It will work after matches specific word after some lines. I attached output image below. Thanks. enter image description here

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 01 '22 at 17:35
0

I don't know if it helps with SUMO, but realisation in JAVA could be something like this:

    Pattern pattern = Pattern.compile(
        "^\\[[^]]+]\\[[^]]+][^\\[]+FIELDS THROWING ERROR => (\\w+)$", 
        Pattern.MULTILINE | Pattern.DOTALL);
    Matcher matcher = pattern.matcher("------------------------------------------------\n" +
            "Start Method SYNC DATA :: xxx : 5/19/2022 11:09:28 PM : Total Sync Time : 0.00\n" +
            "----------------------------------------------\n" +
            "[xxx][xxx] Upsert Failed : \n" +
            "     RECORD NUMBER => ABC:000000\n" +
            "     ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry\n" +
            "     FIELDS THROWING ERROR => Bilcntry\n" +
            "[xxx][xxx] Upsert Failed : \n" +
            "    RECORD NUMBER => ABC:000000\n" +
            "    ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry\n" +
            "    FIELDS THROWING ERROR => Bilcntry\n" +
            "[xxx][xxx] Upsert Failed : \n" +
            "    RECORD NUMBER => ABC:000000\n" +
            "    ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry\n" +
            "    FIELDS THROWING ERROR => Bilcntry\n" +
            "[xxx][xxx] Upsert Failed : \n" +
            "    RECORD NUMBER => ABC:000000\n" +
            "    ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry\n" +
            "    FIELDS THROWING ERROR => Bilcntry\n");
    matcher.find();
    System.out.println("entire match = " + matcher.group(0));
    System.out.println("just the dynamic word = " + matcher.group(1));

Note that I have used MULTILINE and DOTALL flags, these are key to getting the entire match in a way you need it. And to extract just that dynamic word, you need to group it somehow (in JAVA regex it is done with brackets, maybe SUMO is similar) Also, i assume that there is no '[' in the message anywhere else except in two places in the start of first maching line.

Output would be:

entire match = [xxx][xxx] Upsert Failed : 
     RECORD NUMBER => ABC:000000
     ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry
     FIELDS THROWING ERROR => Bilcntry
just the dynamic word = Bilcntry
Lauri
  • 1,859
  • 2
  • 14
  • 17
  • Not working in my case. @Lauri. –  May 26 '22 at 06:39
  • @ArpitPatel sorry to hear that, i have no idea about SUMO, so cannot help further, i'll leave the answer here for anyone to improve on or use, in case they need help with similar task in JAVA – Lauri May 27 '22 at 08:04