0

I am not sure if its appropriate and if there is a way to continue posting question the existing thread as this:

sed error: "invalid reference \1 on `s' command's RHS" and Invalid reference \1 using sed when trying to print matching expression

The issue I am facing is with the Jenkins (2.249.1) pipeline script:

pipeline {
    agent any
    stages {
        stage("exp") {
            steps {
                script {
                    def output=sh(returnStdout: true, script: "echo ab4d | sed 's/b\\(([0-9]\\))/B\\1/' ").trim()
                    echo "output=$output";
                }
            }
        }
    }
}

When run the build, I get this o/p where I was expecting only "B4"

+ echo ab4d
+ sed 's/b\(([0-9]\))/B\1/'
[Pipeline] echo
output=ab4d

The second version that I tried was : def output=sh(returnStdout: true, script: "echo ab4d | sed 's/b\([0-9]\)/B\1/' ").trim()

+ echo ab4d
+ sed 's/b\([0-9]\)/B\1/'
[Pipeline] echo
output=aB4d

With the third version: def output=sh(returnStdout: true, script: "echo ab4d | sed 's/b([0-9])/B\1/' ").trim()

+ echo ab4d
+ sed 's/b([0-9])/B\1/'
sed: -e expression #1, char 15: invalid reference \1 on `s' command's RHS

The output I want is just B4. Please let me know the right approach to fix this issue, and help me understand sed in Pipeline

PS: I am not sure what my Jenkins supports and what it doesn't with respective to using -r option, etc... So I purposely avoiding it until I know exactly if its a must.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Chhatrapati
  • 121
  • 1
  • 8
  • 1
    The error message means your regex didn't contain any grouping parentheses (whch in trad. `sed` need a backslash in front of each). Your second attempt looks fine to me; why don't you want to use that? Just extend the regex to actually match the text you want to match. – tripleee Oct 07 '20 at 17:36
  • 1
    Indeed, asking a new question, with a link back to pertinent earlier questions, is exactly the way to go here. (I'm guessing there may be an existing duplicate about how to pass through backslashes in Jenkins pipelines, though.) – tripleee Oct 07 '20 at 17:38
  • 2
    To be explicit, to replace `ab4d` with just B4, in the shell or in Jenkins, the `sed` command would be `s/ab\([0-9]\)d/B\1/` or perhaps `s/.b\([0-9]\)./B\1/` to more generally match any one character before and after b+digit (or even `s/.*b\([0-9]\).*/B\1/` to replace the entire line). In a shell script, you'd put single quotes around the script, to prevent the shell from messing with the backslashes (or save the script in a file and use `sed -f filename`, but that's silly for such a small script, except on Windows where you have to be happy if you can cake it work at all). – tripleee Oct 07 '20 at 17:42
  • Got it! Adding `.*` to match and substitute entire line solved. I was looking at sed in the eyes of grep, though I had used it a few times before. In the actual scenario, I was using just sed to match three digits in the entire file. Had to add "grep | sed" with above syntax to get what I needed. – Chhatrapati Oct 08 '20 at 06:54
  • One confusion I had with many posts is that the comments referred to usage of single '\' to escape `(`, etc. **In the context of using the sed in Jenkins-Groovy**, Jenkins/Groovy doesn't care about the syntax of sed. It throws error as shown below (at runtime apart from inline errors while editing) since it interprets the line for groovy syntax.: `org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 7: unexpected char: '\' @ line 7, column 88. ipt: "printf ab4d | sed 's/.*b\([0-9]\)` Though it may look obvious, it helps learners like me – Chhatrapati Oct 08 '20 at 07:08
  • Regarding the `-r` option, this is not portable. Some `sed` versions support `-E` instead, and some simply have neither. With `-r` or `-E` you don't need a backslash in front of grouping parentheses, but you still need it for back references `\1` `\2` etc. If you know the option is available, it can be useful for reducing backslashitis. – tripleee Oct 08 '20 at 14:49

0 Answers0