Beelzebub
Beelzebub

Reputation: 13

How to invert result using grep?

How to use grep

The string "[TEST-4902]: This is a long placeholder string"

command

gh pr view 1  --repo joe/test-gh-cli --json body --jq .body | grep -v "TEST-[0-9]{3,4}"

Result

[TEST-4902]: This is a long placeholder string

how to return only "This is a long placeholder string"

expected output

This is a long placeholder string

Upvotes: 0

Views: 370

Answers (2)

rowboat
rowboat

Reputation: 428

According to https://cli.github.com/manual/gh_pr_view, the --jq option for that gh pr view command accepts a jq expression (see man jq or https://stedolan.github.io/jq/manual/). A non-grep solution may be to build on your --jq .body, using sub() for example:

gh ...  --jq '.body | sub("^\\[TEST-\\d{3,4}\\]:\\s*"; "")'

Upvotes: 1

sseLtaH
sseLtaH

Reputation: 11247

You are using extended functionality ERE in a BRE grep so there is no match hence, your original line is returned.

If the -E flag is added in conjunction with your -v flag, then nothing will be printed as the match is now made so the line is ignored (not the part that is matched).

Using grep with perl style regex will do what you are looking for;

$ grep -Po 'TEST-[0-9]{3,4}]: \K.*' input_file
This is a long placeholder string

Upvotes: 1

Related Questions