Skip to content

gsub not works while or lookahead and normal pattern #2532

@fish2bird

Description

@fish2bird

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions