I have a bash script that I use to setup a simple php script on my server. I am stuck with how to correctly change a variable with sed with the script. Here is what I have tried:
echo "Enter Portal Password:"
read PORTPASS;
sed -i 's/$ppass =".*"/$ppass ="$PORTPASS"/' includes/config.php
The above changes the variable in the config file but it only changes it to $PORTPASS it does not change it to what I input in the script.
I also tried this and it does change the $PORTPASS correctly, but it remove the " " around the variable in the file.
sed -i 's/$ppass ='".*"'/$ppass ='"$PORTPASS"';/' includes/config.php
Here is what I'm trying to change in the conf.php file: $ppass ="password";
Try:
sed -i "s/\$ppass =\".*\"/\$ppass =\"$PORTPASS\"/" includes/config.php
You have to use double quotes (") around the command so that the shell will evaluate $PORTPASS before passing it to sed, so then you have to "escape" all of the double quotes within the command.
Related
I am new to shell script. I am sourcing a file, which is created in Windows and has carriage returns, using the source command. After I source when I append some characters to it, it always comes to the start of the line.
test.dat (which has carriage return at end):
testVar=value123
testScript.sh (sources above file):
source test.dat
echo $testVar got it
The output I get is
got it23
How can I remove the '\r' from the variable?
yet another solution uses tr:
echo $testVar | tr -d '\r'
cat myscript | tr -d '\r'
the option -d stands for delete.
You can use sed as follows:
MY_NEW_VAR=$(echo $testVar | sed -e 's/\r//g')
echo ${MY_NEW_VAR} got it
By the way, try to do a dos2unix on your data file.
Because the file you source ends lines with carriage returns, the contents of $testVar are likely to look like this:
$ printf '%q\n' "$testVar"
$'value123\r'
(The first line's $ is the shell prompt; the second line's $ is from the %q formatting string, indicating $'' quoting.)
To get rid of the carriage return, you can use shell parameter expansion and ANSI-C quoting (requires Bash):
testVar=${testVar//$'\r'}
Which should result in
$ printf '%q\n' "$testVar"
value123
use this command on your script file after copying it to Linux/Unix
perl -pi -e 's/\r//' scriptfilename
Pipe to sed -e 's/[\r\n]//g' to remove both Carriage Returns (\r) and Line Feeds (\n) from each text line.
for a pure shell solution without calling external program:
NL=$'\n' # define a variable to reference 'newline'
testVar=${testVar%$NL} # removes trailing 'NL' from string
I am trying to change the name of a file using the sed command and it is just not working.
I have tried:
a)root#ubuntu:/test# sed 's;/test/testfile/;/test/testfile1/;'
b)root#ubuntu:/test# sed "s;/test/testfile/;/test/testfile1/;"
and also tried
c)root#ubuntu:/test#sed 's/\test\/testfile\//\/test\/testfile1\//'
d)root#ubuntu:/test#sed "s/\test\/testfile\//\/test\/testfile1\//"
The cursor enters newline and just hangs there. I have to ctrl+c out of it.Is the syntax wrong or am i running it incorrectly?. I am new to this and just testing this command on my linux system.
Appreciate your help.
If you're trying to rename file test to testfile, then you don't want sed at all. You just want
mv test testfile
The sed command is for manipulating a stream of lines of text, not for renaming files.
Ive got an alias to view my apache error log, which uses sed to format newline characters into newlines for better display. The following command works perfectly fine when entered via command line, but wont work as an alias. Every time I run this, I need to open my .profile, copy the contents of the alias, and paste it in command line.
tail -f /var/log/httpd/my-sandbox-error_log | sed -e 's/\\n/\n/g'
alias:
alias elog="tail -f /var/log/httpd/my-sandbox-error_log | sed -e 's/\\n/\n/g'"
Ive tried a number of approaches, have swapped the quotation characters, and experimented with escape characters a'plenty. It seems I can never get my alias to make use of the sed search/replace (error log is tailed, with no newline formatting). Im sure there is something trivial missing, to which I am naive. I am not a unix/linux expert.
Can anyone enlighten me as to why this does not work as an alias?
If you did set -x, echo alias elog=.., alias elog or otherwise got bash to write the result back to you, you'd see why it isn't working. \\ in double quotes becomes \.
The rule of thumb is that if you have to ask, you've exceeded the usefulness of an alias. Use a function instead:
elog() {
tail -f /var/log/httpd/my-sandbox-error_log | sed -e 's/\\n/\n/g'
}
This way you don't need any additional escaping.
All files in a directory of my project has one line:
var $useDbConfig = 'default_dev';
How can I delete this line from all files and then save the same file, with a single line command using sed?
you may try
sed -i.bak '/var \$useDbConfig = .*default_dev.*;/d' *
or you can use awk
awk '/var/&&/\$useDbConfig/&&/default_dev/{next}{print $0>FilENAME}' *
The -i argument to sed edits in place. With an argument, it saves a backup. So you want something like this:
STR="var $useDbConfig = 'default_dev';"
sed -i.bak "/$STR/d" *
If your sed version supports the -i option you could use this command to interactively delete the line from all files in current directory:
sed -i "/var \$useDbConfig = 'default_dev';/d" ./*
Furthermore: The way to quote the string works in bash. In other shells (like csh) you should adjust the pattern.
I set up a variable in the shell:
-bash-3.00$ echo $source_repl
source ../setup_simple.tcl
I then tried to replace a line in a file that started with the string "package require IxLoad" with the variable string (noting that double quotes are the way to get sed to use variable substitution). First I tried with direct substitution (no escaping the $ in the variable):
-bash-3.00$ sed -e "s/package require IxLoad.*/$source_repl/g" smtp_tput191Mb.tcl > tmpwatch.tcl
sed: -e expression #1, char 38: unknown option to `s'
-bash-3.00$
So I thought that escaping the $ would solve the problem but as you can see the line is then replaces by the literal string "$source_repl" rather than the variable stored there:
-bash-3.00$ sed -e "s/package require IxLoad.*/\$source_repl/g" smtp_tput191Mb.tcl > tmpwatch.tcl
-bash-3.00$ diff smtp_tput191Mb.tcl tmpwatch.tcl
11c11
< package require IxLoad
---
> $source_repl
-bash-3.00$
I looked up many sites on how to do variable substitution in sed and they all seem to indicate that the above should work. What am I missing? Is there something in the actual variable that's causing this?
A
There is a / in your variable. Use # as pattern separator and this will go.
sed -e "s#package require IxLoad.*#$source_repl#g" smtp_tput191Mb.tcl > tmpwatch.tcl
I don't know how to work this issue around in general (changing the separators is easy, but you never know what can be in the variable), but if you can use perl instead of sed, you could load the environment variable form inside perl to avoid the escaping issue:
perl -pe 's/package require IxLoad.*/$ENV{source_repl}/g' smtp_tput191Mb.tcl > tmpwatch.tcl
Sorry for wasting your time folks. I found the problem. I needed to escape the dots and slashes in the actual variable in order to replace it with the sed statement:
-bash-3.00$ echo $source_repl
source \.\.\/setup_simple\.tcl
-bash-3.00$ sed -e "s/package require IxLoad.*/$source_repl/g" smtp_tput191Mb.tcl > tmpwatch.tcl
-bash-3.00$ diff smtp_tput191Mb.tcl tmpwatch.tcl 11c11
< package require IxLoad
---
> source ../setup_simple.tcl
-bash-3.00$
Thanks