Trouble with using sed command to update key value of config file - linux

Having following bash script to update key value of my config file
#!/bin/bash
ipaddr="192.168.0.1"
path="my/binary/file/path/version_op.bin"
sed -i "s/\("IP_ADDR" *= *\).*/\1$ipaddr/" config.txt
sed -i "s/\("PATH_N_FILENAME" *= *\).*/\1${path}/" config.txt
config file (config.txt) content
IP_ADDR=192.168.0.1
PATH_N_FILENAME=NO_PATH
Above scripts working fine for only update IP_ADDR but when i enable sed for PATH_N_FILENAME then it show me following error.
sed: -e expression #1, char 35: unknown option to `s'
might be this issue occurs because of path variable contain / in path and it put sed in confusion state.but what ever issue , still i could not find it.
Have any one idea how to resolve it?

use single-quote in sed command section :
sed -i 's/\("IP_ADDR" *= *\).*/\1$ipaddr/' config.txt

Related

Using Sed -i option in AIX servers. function cannot be parsed aix

I am trying to parse a file in AIX server. I could do the name in Ubuntu.
As the -i option did not work, i used a temperory file and renamed that to the original file. Please let me know where I am missing something.
I have to find a pattern in the file and add a line above or below the pattern.
sed '/ServiceNow (TAM):/i myline1' filename > temp_file && mv temp_file filename
Actual commands used:
sed '/ServiceNow (TAM)/i servicenow.test..ersngrouplist.0=Test' /itim/data/testfile > temp_properties && mv temp_properties /itim/data/testfile
Error:
sed: Function /ServiceNow (TAM)/i servicenow.test..ersngrouplist.0=Test cannot be parsed.
Please help how i can change this command to get
My Expectation:
myline1
ServiceNow (TAM): //pattern
myline2
The problem is that POSIX versions of sed don't accept the i command with additional text on the same line. You need to put a \ after i and to put the text for insertion on the next line.
sed '/ServiceNow (TAM)/i\
servicenow.test..ersngrouplist.0=Test'
The error message from GNU sed is far more helpful than AIX's!
$ sed --posix '/ServiceNow (TAM)/i servicenow.test..ersngrouplist.0=Test'
sed: -e expression #1, char 21: expected \ after `a', `c' or `i'

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 not working in usual way - Shell scripting

I have two sed command which includes in my cook.sh script. One command is
sed -E -i "s/^(\\\$mainDomain=\")[^\"]+(\";)$/\1$MainDomain\2/" /var/config.php
This is working fine.
But the below command which is almost same. But it is not working.
sed -E -i "s/^(\\\$authURI=\")[^\"]+(\";)$/\1$duo_auth\2/" /var/config.php
That give the below error message
sed: -e expression #1, char 36: unknown option to `s'
Any idea on this ?
The issue is likely due to your replacement variable $duo_auth having a un-escaped /, change the default sed separator from / to ~ as
sed -E -i "s~^(\\\$authURI=\")[^\"]+(\";)$~\1$duo_auth\2~" /var/config.php
Try it without -i for seeing if the replacement is as expected and put it back after confirmation.
Example:-
cat /var/config.php
<?php
$authURI="dev.digin.io";
now setting the variable
duo_auth="http://auth.uri.digin.io:3048/"
Now the replacement, without -i
sed -E "s~^(\\\$authURI=\")[^\"]+(\";)$~\1$duo_auth\2~" /var/config.php
<?php
$authURI="http://auth.uri.digin.io:3048/";
The problem is probably due to $duo_auth containing an unescaped /. This means that the sed editing script will have a syntax error.
You may pick any other character to use as a delimiter in the s/.../.../ command, for example #:
sed "s#....#....#"
Just make sure that you pick a character that is not ever going to turn up in either $duo_auth or $authURI.
While testing, I'd recommend that you avoid using in-place-editing (-i) with sed. Also, the -i switch is horribly non-portable between sed implementations (some requires an argument).
Instead, do the slightly more cumbersome
sed -e "s#...#...#" data.in >data.in.tmp && mv -f data.in.tmp data.in
While testing, check the data.in.tmp file before moving it.

how to use sed command with several slashes in bash

I am trying to use SED command with a variable that contains several / and i got the following error :
sed: -e expression 1, char 16: unknown option to s
this is my code and this is inside a script:
thispath=$(readlink -f $0)
sudo sed -i '13s/^/'"$thispath"'/g' /etc/rc.local
and the variable contains for example: /home/user/script.sh
i do not know very well the use of sed can somebody help me
The s command is sed allows you to use any character to delimit the regex and replacement parts. "/" is often a poor choice given how often you come across it in UNIX paths:
Try:
sudo sed -i '13s:^:'"$thispath"':g' /etc/rc.local
It is dangerous to do this directly on rc.local. So make a copy first.

Sed error : bad option in substitution expression

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

Resources