Home > Software engineering >  How to do you invert search using grep perl?
How to do you invert search using grep perl?

Time:08-24

I've been trying to get this but i don't know how, I have been searching the internet but i cannot seem to find the solution for this, Any help would be appreciated,

My reference How to invert result using grep?

gh pr view 6095 --repo freshGrad/main --json body | jq -r .body > temp.txt

#check file is not empty
if [ -s temp.txt ]; then
        # The file is not-empty.
        DESCRIPTION=$(grep -Po 'JCC-[0-9]{3,4}:\K.*' temp.txt) || echo "Something went wrong"
        echo "$DESCRIPTION"
        rm -f temp.txt
else
        # The file is empty.
        DESCRIPTION="No Description Provided"
        echo "$DESCRIPTION"
        rm -f temp.txt
fi

test number 1 JCC is not hyperlink PR body string

JCC-452:Fix ServiceDate-OrderItem to correct drawdown order value calculation

output is good

Fix ServiceDate-OrderItem to correct drawdown order value calculation

test number 2 with hyperlink Pr body string

[JCC-365]: Add new Variation Codes on Quote

[JCC-365]: https://myplace.atlassian.net/browse/JCC-365?atlOrigin=eyJpIjoi3NTljNzY6NjVmND23DlhMDU42fshYzA5YTRkg1LCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

Output

Something went wrong

Expected Output

Add new Variation Codes on Quote

CodePudding user response:

Change you grep to this:

grep -m1 -Po 'JCC-\d{3,4}]?:\h*\K.*' temp.txt

In your script:

DESCRIPTION=$(grep -m1 -Po 'JCC-\d{3,4}]?:\h*\K.*' temp.txt) ||
echo "Something went wrong"

Note that JCC string has optional ] afterwards and you also need to use -m1 to grab only first match. \h* is also good to use to disallow leading spaces in output.

CodePudding user response:

The pattern search is looking for JCC-nnn:, the second file has the JCC-nnn wrapped in '[' and ']' - the pattern fail looking for the ':' after the digit. To support both format, add ? in the pattern, to allow for option ']' before the ':'

grep -Po 'JCC-[0-9]{3,4}]?:\K.*' ...
  • Related