sed returning empty line on second pass - url

I have a configuraton file that I am editing and on the first pass the is correctly changed but on the next two lines sed returns the lines blank.
The lines to be edited are
word.word.word.database=dbase
word.word.word.username=someone
word.word.word.password=someone
The sed commands I am using are
cat config.file | \sed -e "s/database/$dbname/" > config.file.1
cat config.file.1 | \sed -e "s/username/$dbuser/" > config.file.2
cat config.file.2 | \sed -e "s/database/$password/" > config.file.3
cp config.file.3 config.file
The end result is
word.word.word.database=dbname
word.word.word.username=
word.word.word.password=
Can't figure out what is going wrong with this. Any help would be great.
Thanks!

$dbname , $dbuser and $password are begin treated like shell variables, hence the leading dollar signs. If you're trying to incorporate shell vars, try:
sed -i -e "s/database/$dbname/" -e "s/username/$dbuser/" -e "s/password/$password/" config.file
Notice that I've added the -i flag to the above command. It enables in-place editing using GNU sed. Other types of sed require an extension to be set, like: -i.bak and this creates a back up file; in your case this would be: config.bak.
If you're just looking to get the full list of results, either drop the dollarsigns or use single quotes. For example:
sed -i -e "s/database/dbname/" -e "s/username/dbuser/" -e "s/password/password/" config.file
or
sed -i -e 's/database/$dbname/' -e 's/username/$dbuser/' -e 's/password/$password/' config.file
EDIT1:
If I've completely mis-understood your question, try this:
sed -i -e "s/\(.*database=\).*/\1dbname/" -e "s/\(.*username=\).*/\1dbuser/" -e "s/\(.*password=\).*/\1password/" config.file
Results:
word.word.word.database=dbname
word.word.word.username=dbuser
word.word.word.password=password
EDIT2:
If you have shell vars labelled $dbname , $dbuser and $password:
sed -i -e "s/\(.*database=\).*/\1$dbname/" -e "s/\(.*username=\).*/\1$dbuser/" -e "s/\(.*password=\).*/\1$password/" config.file

Related

Sed variable substitution not working in a dockerfile

I have set up as entrypoint for my docker image.
CMD ["/bin/bash","/usr/local/sbin/wrapper.sh"]
which contains
set -m
sed -i "s/DBPASSWORD/$DBPASSWORD/g" db_config.php
/usr/sbin/php-fpm --nodaemonize &
/usr/sbin/httpd -D FOREGROUND
and then when I debug i see that
[root#edoc2-7bdc69555-ftbzt ctdocs]# cat db_config.php
<?php
$dbhost = "10.100.16.11";
$dbuser = "demo-docs";
$dbpass = "DBPASSWORD";
$db = "demodocsdb";
?>
Add RUN before the sed command as following:
RUN sed -i "s/DBPASSWORD/$DBPASSWORD/g" db_config.php
what I did was that I set up: set -x before the sed and it worked, thanks all for your help.

Makefile multiline command does not work in Docker properly

I am trying to compile HTML from Markdown. My makefile looks like:
MD = pandoc \
--from markdown --standalone
# ...
$(MD_TARGETS):$(TARGET_DIR)/%.html: $(SOURCE_DIR)/%.md
mkdir -p $(#D); \
$(MD) --to html5 $< --output $#; \
sed -i '' -e '/href="./s/\.md/\.html/g' $#
When I run this on local machine everything works.
When I run the same in Docker I get the following error:
mkdir -p /project/docs; \
pandoc --from markdown --standalone --to html5 /project/source/changelog.md --output /project/docs/changelog.html; \
sed -i '' -e '/href="./s/\.md/\.html/g' /project/docs/changelog.html
sed: can't read : No such file or directory
makefile:85: recipe for target '/project/docs/changelog.html' failed
make: *** [/project/docs/changelog.html] Error 2
Consequent call of make gives the same error but with another file:
sed: can't read : No such file or directory
makefile:85: recipe for target '/project/docs/todo.html' failed
Obviously, make somehow tries sed earlier than HTML is done.
But I use multiline syntax ; \ of make so as to avoid using subshell.
I also tried && \ but neither of them works. What should I do?
Unfortunately you have been misled by your "obvious" conclusion Obviously, make somehow tries sed earlier than HTML is done :) That's not at all what the problem is. Review your error message more carefully:
sed: can't read : No such file or directory
Note the can't read :; there's supposed to be a filename there. It should say something like can't read foobar:. So clearly sed is trying to read a file with the name of empty string. Here's the line you're running:
sed -i '' -e '/href="./s/\.md/\.html/g' /project/docs/changelog.html
The clear culprit here is the empty string argument to -i. The sed man page describes the -i option as:
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
Note how (a) the SUFFIX is optional, and (b) there's no space between the argument and its option. In your example you added a space, which causes sed to believe that the SUFFIX was not provided and to treat the next argument ('') as the filename to be operated on.
I'm not sure why it worked when you ran it from the command line: I have to assume that your command line version didn't include the empty string or else it didn't include a space between the -i and the ''.
Use one of:
sed -i'' -e '/href="./s/\.md/\.html/g' $#
or
sed --in-place= -e '/href="./s/\.md/\.html/g' $#

grep combine -f and -E

I want to combine the content of a patter file with a regular expressions, i.e. grep -E -f.
The input file has the format
2 List_of_anthropologists<!!>Q1279970
3 List_of_Governors_of_Alabama<!!>Q558677
2027476 12th_Dalai_Lama<!!>Q25240
etc..
and the pattern file has the format:
13th_Dalai_Lama
5th_Dalai_Lama
etc...
I can make it work by manually putting in the pattern "13th_Dali_Lama"
grep -E "^(\d*)(?:\t)13th_Dalai_Lama" input_file
But how to I combine the -f option so that 13th_Dalai_Lama is replace by the lines in the pattern file?
With GNU grep, GNU sed and bash:
grep -f <(sed 's/.*/\\b&\\b/' pattern_file) input_file

calling ansible-playbook from execute shell of jenkins adds extra quotes

While tryting to execute an ansible-playbook from jenkins execute shell, extra quotes are being added by jenkins which causes the ansible-playbook execution to fail. Any work around on this ?
/usr/local/bin/ansible-playbook -i $env $role -e"var1=$var1, var2=$var2, var3=$var3"
The output of the above is :
/usr/local/bin/ansible-playbook -i env-value role-value '-evar1=var1-value, var2=var2-value, var3=var3-value'
If i escape quotes as follows:
/usr/local/bin/ansible-playbook -i $env $role -e\"var1=$var1, var2=$var2, var3=$var3\"
the output of the above is :
/usr/local/bin/ansible-playbook -i env-value role-value '-e"var1=var1-value,' 'var2=var2-value,' 'var3=var3-value"'
What I would do to avoid this quoting issue is to use a -e for every argument:
extra_args+="-e var1=$var1 -e var2=$var2 -e var3=$var3"
ansible-playbook -i $env $role $extra_args
It's also useful when specifying an optional argument that's read in from a jenkins parameter, for example:
if [[ -z $var1 ]]; then
extra_args+="-e var1=$var1"
fi
Also note that if you specify multiple extra vars using -e on the command line you should separate them with spaces, not commas.

Xcode Pre-Action Scripts not working?

Problem
I've attempted to add pre-action shell scripts that would switch on/off certain definitions in my .pch file depending on what I was building for.
However, when running a build, nothing happens. I'm not a fluent shell scripter, so the solution may be my incorrect syntax, but Xcode won't tell me anything.
Details
Here's some code:
prefix=${PROJECT_DIR}/${GCC_PREFIX_HEADER}
sed -i 's/source/working/' $prefix
sed -i 's/\/\/#define\ HOCKEYAPP_BUILD/#define\ HOCKEYAPP_BUILD' $prefix
sed -i 's/\/\/#define\ FLURRY_ENABLED/#define\ FLURRY_ENABLED' $prefix
sed -i 's/\/\/#define\ PRODUCTION_BUILD/#define\ PRODUCTION_BUILD' $prefix
I added the first line to test if it would even remove a basic word I know is in the .pch file. It didn't. This leads me to believe that my path is invalid.
I've tried several different variations of the .pch file's path and have failed with all of them, though they all could have been wrong.
Thank you for your help
Your sed lines seem not to be correct. Try:
prefix=${PROJECT_DIR}/${GCC_PREFIX_HEADER}
sed -i -e 's/source/working/' $prefix
sed -i -e 's/\/\/#define\ HOCKEYAPP_BUILD/#define\ HOCKEYAPP_BUILD/' $prefix
sed -i -e 's/\/\/#define\ FLURRY_ENABLED/#define\ FLURRY_ENABLED/' $prefix
sed -i -e 's/\/\/#define\ PRODUCTION_BUILD/#define\ PRODUCTION_BUILD/' $prefix

Resources