regexgrepfull-text-search

How to get matched pattern in ugrep


I have created a ugrep command and file

ugrep -Hn -e 'Error:XX234 - \S+\.txt : cannot find user' -e 'Error:XX235 - \S+\.pdf : cannot find data' --format='%f:%n:%o%~'  *

File content

1_Error0: rwPqdEPE.txt - YjTccJITSpALx1B8cORN
ErrorXX235 - xyz.pdf : cannot find data
Error:XX235 - xyz.pdf : cannot find data
Error:XX234 - abc.txt : cannot find user

Output

enter image description here

I am expecting below in the output:

  1. display which pattern was matched with error

Above is just a sample ugrep command. my actual command will be quite big like below

ugrep -Hn -e pattern1 -e pattern2 -e patternN --format='%f:%n:%o%~' *

Expected output

issue.txt:12:Error:XX235 - xyz.pdf : cannot find data:<pattern2>
issue.txt:13:Error:XX234 - abc.txt : cannot find user:<patternN>

Problem Statement to be solved

I am working on a solution to help my clients who use my application (AppX) troubleshoot issues. For example, when clients try to install AppX, there are certain services that must be running. If these services are not running, AppX logs an error message in the log files.

This is just one type of issue; there can be many such issues reflected in the log files.

Currently, the Support Team analyzes these log files manually to identify the issues and provide resolutions to the clients. Given that there could be thousands of such issues and the log files can be large and in formats like .tar or .zip, this manual process is time-consuming and inefficient.

I am developing a tool to automate this process. The tool will:

This will streamline the troubleshooting process, making it faster and more efficient for the Support Team to provide resolutions to clients.

So I am saving such issues as a part of string or regex pattern and their resolution link mapped in DB and later on I want to fetch the error matched in log file and present the team with resolution link


Solution

  • We can print the matched pattern in the output using '%G' option.

    ugrep -rHPn -o -e '(?<pattern1>ErrorXX234 - \S+\.txt : cannot find user)' -e '(?<pattern2>ErrorXX235 - \S+\.pdf : cannot find data)' -e '(?<pattern3>pattern1 pattern2)' --format='--%M--%F:%n:%o:%G---%~' *
    

    Output

    --1--issue.txt::1:ErrorXX234 - abc.txt : cannot find user:pattern1---
    
    --2--issue.txt::2:ErrorXX235 - xyz.pdf : cannot find data:pattern2---
    
    --3--issue.txt::4:pattern1 pattern2:pattern3---
    
    --4--issue.txt::7:ErrorXX234 - abc.txt : cannot find user:pattern1---
    
    --5--issue.txt::9:ErrorXX234 - abc.txt : cannot find user:pattern1---
    
    --6--issue.txt::10:ErrorXX235 - xyz.pdf : cannot find data:pattern2---
    
    --1--testfile copy 2.txt::14:ErrorXX234 - abc.txt : cannot find user:pattern1---