How can I replace a line of text in a file with a single command when the line contains a variable? - linux

I have a script called script.sh which has the contents
export HELLO=$WORLD
I want to change it using a single command to
export HELLO=${WORLD:2:3}
I'm attempting to use the sed command but i'm new to linux and can't quite get it right. Here's what I have
sed -i 's/export HELLO=$WORLD/export HELLO=${WORLD:2:3}' script.sh
How should this line be written to replace the text correctly?

You just missed a / at the very end:
sed -i 's/export HELLO=$WORLD/export HELLO=${WORLD:2:3}/' script.sh
^
this / is missing

The $ is a special symbol that means end of line, also as #yvesonline points out you are missing the trailing /. This is how I would do it:
sed -i 's/\(export HELLO=\$\)\(WORLD\)/\1{\2:2:3}/' script.sh

Related

Not getting replaced using the sed using the sed -i command

I am trying to do the following thing using the sed
cp src/config/template.js src/config/index.js
export API_GATEWAY_ENDPOINT="http://localhost:9000/"
sed -i "" "s|{{API_GATEWAY_ENDPOINT}}|$API_GATEWAY_ENDPOINT|g" src/config/index.js
But when I run this script, then I am getting this error,
sed: can't read s|{{API_GATEWAY_ENDPOINT}}|http://localhost:9000/|g: No such file or directory
I am using a Linux machine.
Can any tell me why this is happening?
The first argument to sed should be the script. So when you have that extra "", the second argument is interpreted as a file name.
And you don't need to copy the file and then do an inplace replacement. Instead use > operator to redirect the output from sed to the proper file.
>src/config/index.js <src/config/template.js sed "s|{{API_GATEWAY_ENDPOINT}}|$API_GATEWAY_ENDPOINT|g"

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

Add a kernel parameter with sed to grub

I'm writing a bash script to non-interactively enable hibernation on a linux system. To this end, I need to insert a shell variable that contains slashes on a specific line of a while, inside quotes that are on that line.
The relevant part of the file to b edited looks like this:
GRUB_DEFAULT=0
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="Manjaro"
GRUB_CMDLINE_LINUX_DEFAULT="quiet"
GRUB_CMDLINE_LINUX=""
I need to change it to this:
GRUB_DEFAULT=0
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="Manjaro"
GRUB_CMDLINE_LINUX_DEFAULT="quiet resume=/dev/disk/by-partuuid/c5a552c2-fe8f-423a-9037-c35bf090d9c3"
GRUB_CMDLINE_LINUX=""
The added parameter is provided by a shell variable.
I tried this:
sed -i '\*^GRUB_CMDLINE_LINUX_DEFAULT* s*"$* '"$(grub_resume_boot_option)"'"*' /etc/default/grub
Logic that I was aiming for was: "Look for a line that starts with pattern "GRUB_CMDLINE_LINUX_DEFAULT", and replace the last " with the given pattern. Use * as delimiter to preserve the slashes in the expanded variable."
However, the command fails if there are any spaces at the end of the line. Is there any way to make it not take spaces at the end of the line into account?
Also, if there is more simple or readable solution to this, I would be very interested.
You can do:
sed -i 's#^\(GRUB_CMDLINE_LINUX_DEFAULT="quiet\)"$#\1 resume=/dev/disk/by-partuuid/c5a552c2-fe8f-423a-9037-c35bf090d9c3"#' /etc/default/grub
Example:
$ sed 's#^\(GRUB_CMDLINE_LINUX_DEFAULT="quiet\)"$#\1 resume=/dev/disk/by-partuuid/c5a552c2-fe8f-423a-9037-c35bf090d9c3"#' <<<'GRUB_CMDLINE_LINUX_DEFAULT="quiet"'
GRUB_CMDLINE_LINUX_DEFAULT="quiet resume=/dev/disk/by-partuuid/c5a552c2-fe8f-423a-9037-c35bf090d9c3"
From: https://serverfault.com/questions/885684/editing-the-value-of-grub-cmdline-linux-default-thru-bash-script
sed -i 's/^GRUB_CMDLINE_LINUX_DEFAULT="/&newstuff /' /etc/default/grub
This will add "newstuff" to the beginning of the list of params. Examples:
sed 's/^GRUB_CMDLINE_LINUX_DEFAULT="/&newstuff /' <<< 'GRUB_CMDLINE_LINUX_DEFAULT="quiet"'
GRUB_CMDLINE_LINUX_DEFAULT="newstuff quiet"
sed 's/^GRUB_CMDLINE_LINUX_DEFAULT="/&newstuff /' <<< 'GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"'
GRUB_CMDLINE_LINUX_DEFAULT="newstuff quiet splash"
sed -i "s/GRUB_CMDLINE_LINUX_DEFAULT="[^"]*/& $(grub_resume_boot_option)/" /etc/default/grub

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

Sed Append Line

Does sed have a command to append a line after a matched line? I tried searching around but was a bit confused with the results.
Basiclly I want it to match
#address=/doubleclick.net/127.0.0.1
And add a line below it like
#address=/doubleclick.net/127.0.0.1
address=/anotherurl/ipaddress
Is this possible?
You can use the a(append) command in sed:
$ sed -i '/^#address=\/doubleclick.net\/127.0.0.1$/a\
> address=/anotherurl/ipaddress' file.txt
Or you can use s(substitute) command:
$ sed -i 's#^#address=/doubleclick.net/127.0.0.1$#&\naddress=/anotherurl/ipaddress#' file.txt
Note: $ and > are bash prompt.
This might work for you:
echo "#address=/doubleclick.net/127.0.0.1"|
sed '\|#address=/doubleclick.net/127.0.0.1|a\address=/anotherurl/ipaddress'
#address=/doubleclick.net/127.0.0.1
address=/anotherurl/ipaddress
You can use any delimiter you like in an address by prepending a \ i.e. \|...| for the substitute command the \ is not necessary.
If you want a blank line then some text following the match, use:
echo "#address=/doubleclick.net/127.0.0.1"|
sed '\|#address=/doubleclick.net/127.0.0.1|a\\naddress=/anotherurl/ipaddress'
#address=/doubleclick.net/127.0.0.1
address=/anotherurl/ipaddress

Resources