How to replace a line with special characters of a file with Linux Bash instructions? [duplicate] - linux

This question already has answers here:
Escape a string for a sed replace pattern
(17 answers)
Closed 4 years ago.
I am trying to modify the line of a phpmyadmin configuration file, precisely, the file /etc/phpmyadmin/config.inc.php, in the line on the database server, where I need to change it with a bash instruction. I tried to do it with the sed and awk commands, but I still have no results.
The line that I want to modify is the following:
$cfg['Servers'][$i]['host'] = $dbserver;
For the following value:
$cfg['Servers'][$i]['host'] = '192.168.0.10';

cat ini
"$cfg['Servers'][$i]['host'] = $dbserver;"
cat ini | sed "s/\$dbserver/'192.168.0.10'/"
"$cfg['Servers'][$i]['host'] = '192.168.0.10';"
In other words, a
sed -i.bak-e "s/\$cfg\['Servers'\]\[\$i\]\['host'\] = \$dbserver;/\$cfg['Servers'][\$i]['host'] = '192.168.0.10';/" /etc/phpmyadmin/config.inc.php
should do the job. (with a .bak copy as a precaution).

Related

How to change file parmater by input in bash [duplicate]

This question already has answers here:
How do I use sed to change my configuration files, with flexible keys and values?
(8 answers)
Closed 2 years ago.
I have a file that contain this line
SELINUX = enforce
I want to change this by given input to permissive
How i do that without demage?
If [[ "$1" == "Y"]]
then
sed -ri 's/(^.*SELINUX=)(.*$)/\1enforce/' file
else
sed -ri 's/(^.*SELINUX=)(.*$)/\1permissive/' file
fi
If the first passed parameter ($1) is equal to "Y" use sed to split SELINUX line into to 2 sections. Substitute the line for the first section followed by "enforce". If the passed parameter is not "Y" substitute the line for the first section followed by "permissive".

How to source a key value paired file in bash escaping whitespace? [duplicate]

This question already has answers here:
Use key/value data from a file in a shell script
(1 answer)
Reading key/value parameters from a file into a shell script
(1 answer)
Closed 3 years ago.
$ cat foo.txt
a=1one
b=2two
c=3 three
d=4four
$ source foo.txt
bash: three: command not found...
Need to set all the variable listed in foo.txt, how to source this file by escaping the space character? foo.txt comes from other application, which I cannot control, or is there an alternative to source ?
If the output is so regular, you could try to preprocess the file using sed like this:
$ sed -e "s/=/='/;s/$/'/" < foo.txt >sourced.env
and then source sourced.env. This will add a ' just after the = and add an ending '.

why questionmark comes in the end of filename when i create .txt file through shell script? [duplicate]

This question already has answers here:
Shell Scripting unwanted '?' character at the end of file name
(2 answers)
Closed 6 years ago.
I am writing one shell script in which I am supposed to create 1 text file. When I do this, a question mark comes at the end of file name. what is the reason?
I am trying below methods in bash script.
1) grep ERROR a1* > text.txt
2) touch text.txt
In both the methods, instead of text.txt, there is a file generated as text.txt?
what should I do to overcome this?
Sounds like you script uses \r\n as line endings, this is typical DOS style line endings. Unix like systems uses \n. You should try to change the line feeds, eg with your favorite text editor:
vim +'set ff=unix | x' my_script
Or with dos2unix:
dos2unix my_script
Or with GNU sed:
sed -i 's/\r$//' my_script

Why is SED echoing rather than editing: sed '/^;text1/!b;:a;n;//ba;i\text2' [duplicate]

This question already has answers here:
How do I use a new-line replacement in a BSD sed?
(4 answers)
Closed 7 years ago.
sed '/^;date.timezone =/!b;:a;n;//ba;i\date.timezone = Europe/London' /etc/php.ini
You can probably guess Im creating a script for setting up LAMP servers.
In the above example the text is not replaced but instead the changes are displayed on the console.
I my goal was to insert date.timezone = Europe/London the last occurence of ;date.timezone =
Etan Reisner:
You aren't using the -i flag to tell sed to modify in place.

How to add a character to the end of each line in a file in shell script [duplicate]

This question already has answers here:
sed edit file in place
(15 answers)
Closed 8 years ago.
I need a script that add a particular character to the end of each line . I am using the command
sed 's/$/ foo/' r.txt
It adds foo to end of each line in the file r.txt and displays in my terminal .
What do i need to do if i want to save this existing file with this new record appended after the end of each line .
To save to a new file:
sed 's/$/ foo/' r.txt > newfile.txt
To edit in place
sed -i 's/$/ foo/' r.txt

Resources