1

Can someone help me with this bash command please:

sed -i "s/\$CORPUS_DATA/$CORPUS_DATA/g" conf/*.xml

What does it mean?

Maroun
  • 87,488
  • 26
  • 172
  • 226
pokatore
  • 51
  • 1
  • 5
  • tell us which part you understand and which ones you don't.. you can find some of the information in https://stackoverflow.com/tags/sed/info – Sundeep May 14 '18 at 09:38
  • Well I know it is used to replace values but I have an error : sed: -e expression #1, char 17: unknown option to `s' – pokatore May 14 '18 at 09:44
  • in that case, please add an sample to show the failing case.. we cannot debug with command alone.. need sample input lines and sample values for variables used.. at a guess, the gotchas and links mentioned in these answer should help you: https://stackoverflow.com/a/7680548 and https://stackoverflow.com/questions/9366816/sed-unknown-option-to-s – Sundeep May 14 '18 at 10:09

2 Answers2

2

It replaces the value of the text "$CORPUS_DATA" in the file, to the actual value of the environment variable $CORPUS_DATA.

Test it:

$ CORPUS_DATA=hello
$ echo '$CORPUS_DATA'
$ $CORPUS_DATA
$ echo '$CORPUS_DATA' | sed "s/\$CORPUS_DATA/$CORPUS_DATA/g"
hello

Note that the first part escapes the $ sign, so the regex will match the text "$CORPUS_DATA" literally. The second part doesn't, which means it takes the value of the env variable.

Maroun
  • 87,488
  • 26
  • 172
  • 226
  • Well it's the second $ that is problematic. I have got this error : sed: -e expression #1, char 17: unknown option to `s' – pokatore May 14 '18 at 09:49
  • @pokatore I can't reproduce. – Maroun May 14 '18 at 09:53
  • 1
    The slash is only to escape the `$` _from the shell_. A `$` at the very start of a regular expression is just fine. To escape it in the expression, you would have had to use `\\\$`. – Kusalananda May 14 '18 at 10:14
1

Following is the explanation of code.

sed -i "s/\$CORPUS_DATA/$CORPUS_DATA/g" conf/*.xml

-i is for saving the output into Input_file itself.

s using substitute option changing \$CORPUS_DATA with shell variable $CORPUS_DATA value.

g means do this change to all occurrences in current line.

conf/*.xml loop through all xml files in conf directory.

RavinderSingh13
  • 101,958
  • 9
  • 41
  • 77