0

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
3
  • 2
    You are using grep -v option which means it should ignore this line, but from your post I understood you want to print it. Could you please confirm on this one once. Aug 3, 2022 at 3:45
  • 1
    @RavinderSingh13 : We don't know which grep implementation the OP is using (Gnu, BSD,..). Note that -E is not specified. Therefore, {3,4} is taken as literal match unless this grep takes extended regexp as default, and this would then explain the output under -v. Aug 3, 2022 at 5:55
  • 1
    @user1934428, yeah I am aware of that about -E option but still thought to give a check on this one. Since OP accepted the answer so we know now what is OP's grep :) Aug 3, 2022 at 6:01

2 Answers 2

1

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
1

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*"; "")'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.