0

I have the following sed command:

sed -i 's#^.*BRANCH_NAME=.*$#\tBRANCH_NAME=$(egrep \\"${project}\\" localFile.xml | sed -n 's_.*changeset=\"\([^\"]\+\).*_\1_p')#' myCfgFile.cfg

basically I'm trying to substitute a line starting with BRANCH_NAME= from myCfgFile.cfg with something else like: BRANCH_NAME=$(egrep \\"${project}\\" localFile.xml | sed -n 's_.*changeset=\"\([^\"]\+\).*_\1_p')

The bad news is that I can't preserve the content of BRANCH_NAME=$(egrep \\"${project}\\" localFile.xml | sed -n 's_.*changeset=\"\([^\"]\+\).*_\1_p') literally, because I get it interpreted.

So, is there any way that I can keep the content as it is, without having it interpreted by sed? As I said above, I'm trying to replace a line (BRANCH_NAME=) with something else (*BRANCH_NAME=$(egrep \"${project}\" localFile.xml | sed -n 's_.changeset=\"([^\"]+)._\1_p')*)

I would really appreciate your help. Thanks

EDIT

Actually I'm dealing with 3 files:

  1. One List of paths to projects (below is the format)

    • /home/myDir/proj1

    • /home/myDir/proj1/extended

    • (...)

  2. An .xml file containing project names and their corresponding changesets

<project name="/home/myDir/proj1/extended" path="/home/myDir/proj1/extended" changeset="3c57f20f89de07e1490aac56d1e8aba5848150b5" />

By executing a for loop I get following:

for project in (cat projectlis); do
   egrep \\"${project}\\" localFile.xml | sed -n 's_.*changeset=\"\([^\"]\+\).*_\1_p')#' myCfgFile.cfg
done
  1. A bash script that I'm not allowed to change and commit :) this script does contain a wrong line defining a variable

BRANCH_NAME=*

and before executing the script that I'm not allowed to commit, I'd like to change (from Jenkins inline script) the wrong line

BRANCH_NAME=*

with

BRANCH_NAME=$(egrep \\"${project}\\" localFile.xml | sed -n 's_.*changeset=\"\([^\"]\+\).*_\1_p')`

I hope this is a bit more clear.

Thanks

Asgard
  • 602
  • 1
  • 7
  • 18
  • http://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern – riteshtch Feb 24 '16 at 17:39
  • Don't you find that command just a wee bit complicated? You CAN escape all the sed metacharacters, see http://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed, but if you edit your question to include some sample input and expected output and tag your question with `awk` I'm sure someone can show you a concise, simple script to do whatever it is you are trying to do without jumping through all the hoops with the wacky escaping, chains of piped commands, subshells, etc. – Ed Morton Feb 24 '16 at 17:46
  • 1
    Your edit helps a bit but now it's still kinda vague. Is the file you describe in item 1 the "projectlis" file you refer to in item 2? Does your bash file only contain the one line with BRANCH_NAME in it? Is BRANCH_NAME always at the start of the line? Does your XML file only contain 1 line or do you want multiple projects in it to be changed? I can't imagine what `from Jenkins inline script` means. Please edit your question to contain a clear set of concise but truly representative input files and THE output you want to get given those input files. – Ed Morton Feb 24 '16 at 19:19

1 Answers1

2

Without sample input and expected output it's very much a guess but it LOOKS like what you're trying to do would be just this with awk:

awk -v project="$project" '
NR==FNR {
    if ($0 ~ project) {
        gsub(/.*changeset="|".*/,"")
        branchName = $0
    }
    next
}
/BRANCH_NAME/ { $0 = "\tBRANCH_NAME=" branchName }
{ print }
' localFile.xml myCfgFile.cfg > tmp && mv tmp myCfgFile.cfg

It might be even simpler than that once we see your input files.

I still cant figure out from your question what your input files really contain or what you really want your script to output but maybe this will show you how to do whatever it is you are trying to do given what you have told us so far:

$ cat projectlis   
/home/myDir/proj1
/home/myDir/proj1/extended

$ cat localFile.xml                                    
<project name="/home/myDir/proj1/extended" path="/home/myDir/proj1/extended" changeset="3c57f20f89de07e1490aac56d1e8aba5848150b5" />
<project name="/home/myDir/proj1" path="/home/myDir/proj1" changeset="something-made-up" />

$ cat myCfgFile.cfg                                     
BRANCH_NAME=*

$ cat tst.awk                                           
FILENAME==ARGV[1] {
    proj2branch[$0]
    next
}
FILENAME==ARGV[2] {
    for (proj in proj2branch) {
        if ( index($0,"name=\"" proj "\"") ) {
            gsub(/.*changeset="|".*/,"")
            proj2branch[proj] = $0
        }
    }
    next
}
/BRANCH_NAME/ {
    for (proj in proj2branch) {
        print proj":", "BRANCH_NAME=" proj2branch[proj]
    }
}

$ awk -f tst.awk projectlis localFile.xml myCfgFile.cfg                                                                                
/home/myDir/proj1/extended: BRANCH_NAME=3c57f20f89de07e1490aac56d1e8aba5848150b5
/home/myDir/proj1: BRANCH_NAME=something-made-up
Ed Morton
  • 188,023
  • 17
  • 78
  • 185