How to edit a file saved in string using sed? - linux

I need to edit the file saved in a string using sed. As per the below example, I want to change some pattern in the php.ini file. Below example will allow only to change what is saved in the string.
[root#server ~]# PHPINI=/etc/php.ini
[root#server ~]# sed "s/somepattern/changedpattern/" <<< "$PHPINI"
Can somebody help? Thanks in advance.

You're using the herestring syntax which passes a string on standard input. You should instead pass the name of the file as an argument:
sed 's/somepattern/changedpattern/' /etc/php.ini
To overwrite the existing file, the standard method is to write to a temporary file and then overwrite the original:
sed 's/somepattern/changedpattern/' /etc/php.ini > tmp && mv tmp /etc/php.ini
Some versions of sed support "in-place" editing (which does more or less the same in the background):
sed -i.bak 's/somepattern/changedpattern/' /etc/php.ini
This creates a backup of the original file with a .bak suffix.
If the filename is contained within a variable, then simply use that instead
php_ini=/etc/php.ini
sed -i.bak 's/somepattern/changedpattern/' "$php_ini"
Note my use of lowercase variable names. Uppercase ones should be reserved for use by the shell.

Related

Sed how to find and replace a value using a bash variable [duplicate]

I have a configuration file (gpsd.default) containing data with the following format:
# If you must specify a non-NMEA driver, uncomment and modify the next line
GPSD_SOCKET="/var/run/gpsd.sock"
GPSD_OPTIONS=""
GPS_DEVICES=""
I am making a change on the file with sed:
sed -i 's/^GPS_DEVICES="".*/GPS_DEVICES="dev/ttyUSB1"/' /etc/default/gpsd.default
or
sed -i '4s/^.*/GPS_DEVICES="dev/ttyUSB1"/' /etc/default/gpsd.default
The above sed command returns error:
sed: bad option in substitution expression
Because the new line contains "/" in its expression.
How to update my sed command to make it work?
This is because you are using a regex containing /, which is the same character sed uses as delimiter.
Just change the sed delimiter to another one, for example ~:
sed -i 's~^GPS_DEVICES="".*~GPS_DEVICES="dev/ttyUSB1"~' /etc/default/gpsd.default
By the way, since you are changing files in /etc, you may want to use -i.bak, so that the original file gets backed up. It is a good practice to prevent loss of important information.
You should update your sed command to this.
sed -i 's/^GPS_DEVICES=\"\".*/GPS_DEVICES=\"dev\/ttyUSB1\"/' /etc/default/gpsd.default

find string and stored the replaced string in other file shell script

I want to find a string say 'foo' in a file sat '1.txt' using shell script and replace 'foo' with 'bar' and store the output into other file say '2.txt' without any modification in 1.txt.
so '1.txt' would contain 'foo' itself but '2.txt' will now have all the contents of '1.txt' with 'foo' replaced by 'bar'
I am using this command in bash
sed -i "s/foo/bar/g" "1.txt" > 2.txt
but its not working.
Remove the -i option as it stands for in-place operation.
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
(sed(1) man page)
Remove the -i switch, or provide a backup extension (-i.bak), so the modified file will be 1.txt and 1.txt.bak the original backup.
You don't need quotes on "1.txt", unless the filename contains spaces.
sed "s/foo/bar/g" 1.txt > 2.txt
or
sed -i.bak "s/foo/bar/g" 1.txt
Take a look at sed manual

using sed to move dynamic string from one file and appending to second file

i'm trying to move a dynamic string from a file and append it to the second file with sed.
what I have tried so far:
sed -i -e '/To300/{w /home/test/test1.txt' -e 'd}' /home/test/test2.txt
where XXXXTo300 is the string I want to move. Its moving the string to the second file, but when the next string is moved, its overwriting the existing strings in the second file.
thanks
In general, this won't work, as sed overwrites the file each time you use the command. However, if you use GNU sed, you can work around this limitation by writing to stdout and redirecting:
sed -i -e '/To300/{w /dev/stdout' -e ';d }' oldfile >> newfile

How to replace a line (directory path) with a variable containing another line (new directory path) in a Linux file?

I have a file containing contents like:
aaaaaaaaaa
export ORACLE_HOME=/home/oracle/test/prod/db_1
bbbbbbbbbb
Now I want to replace the line of
export ORACLE_HOME=/home/oracle/test/prod/db_1
with something like below:
export ORACLE_HOME=/u01/app/oracle/product/11204/dbhome_1
I tried numerous ways found through Google like using "sed" or "awk" but neither one worked. I tried:
export D1="export ORACLE_HOME=/u01/app/oracle/product/11204/dbhome_1"
sed -i -e "s+export ORACLE_HOME=/home/oracle/test/prod/db_1+$D1+" file.lst
Note: as one Google search says, since the strings contains "/", so a different delimiter like "+" needs to be used.
Can you share with me the right command to do so?
sed -r 's~(ORACLE_HOME=).*~\1new/path/here~'
here ~ is used as the delimiter. If the new path is a bash variable, you can escape the quotes
sed -r 's~(ORACLE_HOME=).*~\1'$D1'~'
If you are still having trouble with the substitution, you can use this variation which will explicitly find the line containing export ORACLE_HOME before attempting the path substitution. The expression makes use of alternate delimiters for the substitution '|'.
To edit in place (Linux), you can use the '-i' option (and add '-i.bak' to make a backup of the original). If you are using a mac or other OS without the '-i' option available, use redirection to create a new file which you can copy over the current file.
sed -i '/^export\sORACLE_HOME/s|=.*$|=/u01/app/oracle/product/11204/dbhome_1|' \
yourfilename
or without '-i':
sed '/^export\sORACLE_HOME/s|=.*$|=/u01/app/oracle/product/11204/dbhome_1|' \
yourfilename > newfilename
cp yourfilename yourfilename.sav && mv -f newfilename yourfilename
(note: the '\' at the end of the sed command line is a simple line-continuation)
Using a Variable for Replacement
From your comment, if you want to use a variable instead of a hardcoded path for the replacement string, then simply replace the single-quotes with double-quotes and include the variable as the replacement string. For example:
npath=/u01/app/oracle/product/11204/dbhome_1
sed -i "/^export\sORACLE_HOME/s|=.*$|=$npath|" dat/opath.txt

How to remove line containing a specific string from file?

I have a file BookDB.txt which stores information in the following manner :
C++ for dummies:Jared:10.67:4:5
Java for dummies:David:10.45:3:6
PHP for dummies:Sarah:10.47:2:7
Assuming that during runtime, the scipt asks the user for the title he wants to delete. This is then stored in the TITLE variable. How do I then delete the line containing the string in question? I've tried the following command but to no avail :
sed '/$TITLE/' BookDB.txt >> /dev/null
You can for example do:
$ title="C++ for dummies"
$ sed -i "/$title/d" a
$ cat a
**Java for dummies**:David:10.45:3:6
**PHP for dummies**:Sarah:10.47:2:7
Note few things:
Double quotes in sed are needed to have you variable expanded. Otherwise, it will look for the fixed string "$title".
With -i you make in-place replacement, so that your file gets updated once sed has performed.
d is the way to indicate sed that you want to delete such matching line.
Your command should be,
sed "/$TITLE/d" file
To save the changes, you need to add -i inline edit parameter.
sed -i "/$TITLE/d" file
For variable expansion in sed, you need to put the code inside double quotes.

Resources