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

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".

Related

How to use sed to replace text with a file path? [duplicate]

This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Closed 2 years ago.
I'm writing a bash script where I need to replace text in a file with a specific file path, but my understanding is that sed does not work with specific characters such as /. I'm wondering if there is some way around this?
Here is my script currently:
currentdir="$PWD"
filepathvar="${currentdir}/settings.ini"
sed -i -e "s/filepath/$filepathvar/g" aimparmstest
When I print out filepathvar everything is as I expect it to be, but it seems the fact that filepathvar contains special characters, it gives me the following error:
sed: -e expression #1, char 13: unknown option to `s'
Is there any way around this? Or perhaps another command I can use? I haven't had any success with changing around the parameters. Any help is greatly appreciated.
You can use any character as the separator (the first character). For example:
echo "a/b/c" | sed -e 's|/|_|g'
In your case:
sed -i -e "s|filepath|$filepathvar|g" aimparmstest

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 '.

How to delete 1 or more matching line(s) while reading a file in bash script [duplicate]

This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Combining two sed commands
(2 answers)
Linux, find replace on a folder of files using a list of items for replacement?
(1 answer)
Closed 4 years ago.
I want to read file using a bash script and delete line(s) which are matching with my specific scenario (line(s) starting with 'z').
my code works fine if the 'inputFile' contains only alphabetic characters.
but, if a line with 'specific characters of sed' (line eg : z-2.10.3.2 x/y/z F (&)[]+* ) then i got an error,(error : sed: -e expression #1, char 29: unterminated `y' command).
#!/bin/bash
inputFile="test.txt"
while IFS= read -r line
do
echo "$line"
if [[ $line == z* ]];
then
sed -i "/$line/d" $inputFile
fi
done < "$inputFile"
i want to delete 'z-2.10.3.2 x/y/z F (&)[]+*' kind of lines, how can i do this...?
As you mentioned you don't need line which has z*
Simply use grep -v
grep -vE "^[[:blank:]]*z" file
I have created one scenario where I have a file which contains
root#ubuntu:~/T/e/s/t# cat file
hello world
sample line 1
sample line 2 world
sample line 3
sample line 4
In my case, I want to remove the line contains "world"
root#ubuntu:~/T/e/s/t# grep -v "world" file
sample line 1
sample line 3
sample line 4
If you want you can redirect your output in another file.

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

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).

Excluding/ignoring (Not Deleting) first and last line based on line number [duplicate]

This question already has answers here:
Delete first and last line or record from file using sed
(5 answers)
Closed 4 years ago.
I'm using sed command to edit some text file.
How can i exclude the first and last line in each text file for being edited.
I went through sed gnu manual but i only found commands to match line ranges i.e. 1,$ or to exclude ranges i.e. 1,$!. i just need to exclude line # 1 and last line $. i'm not sure if its possible to select a range i.e 2, $-1?
Here's my code.
sed -e '1,$ s/.*/<p>&<\/p>/'' file.txt
You can't use maths in sed addresses. But you can tell sed to do nothing on the first and last line:
sed -e '1n; $n; s/.*/<p>&<\/p>/'
where n means "read the next line of input into the pattern space" (in case of the last line, it won't read anything).
(The two single quotes at the end of the expression are probably a typo, right?)

Resources