-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed
Milestone
Description
Describe the bug
I want output capture pattern if matched else empty
example input
not a cd cmd
cd /ok
I want extract /ok
which follows cd
, and replace invalid lines to empty, expected
""
"/ok"
my try is jq -nR 'inputs|gsub("^(?!cd ).*$|^cd "; "")'
^(?!cd ).*$
works, ^cd
also works, but failed while out them together.
To Reproduce
[root build]#(echo "not a cd cmd"; echo "cd /ok") | ./jq -nR 'inputs|gsub("^cd "; "")'
"not a cd cmd"
"/ok"
[root build]#(echo "not a cd cmd"; echo "cd /ok") | ./jq -nR 'inputs|gsub("^(?!cd ).*$"; "")'
""
"cd /ok"
[root build]#(echo "not a cd cmd"; echo "cd /ok") | ./jq -nR 'inputs|gsub("^(?!cd ).*$|^cd "; "")'
""
""
Expected behavior
jq works likes perl
[root build]#(echo "not a cd cmd"; echo "cd /ok")
not a cd cmd
cd /ok
[root build]#(echo "not a cd cmd"; echo "cd /ok") | perl -pe 's/^(?!cd ).*$|^cd //g'
/ok
[root build]#
Environment (please complete the following information):
- OS: CentOS 7
- jq version: jq-1.6
Additional context