replace a variable of another file [duplicate] - bash

This question already has an answer here:
sed fails with "unknown option to `s'" error [closed]
(1 answer)
Closed 1 year ago.
I have a file I want to edit.
at the beginning, I take another file, I add for each line "\0A", I delete the end-of-line characters, and I put it in an OV variable
OV=$(cat certificate_file | sed 's/$/\\OH/' | tr -d '\n') #Add \OH + delete end of
line caracter
Now I want to replace the contents of the config_var1= variable in the conf file, with what is in OV
I tried to use this command:
sed -i -r "s:config_var1=.*:config_var1=$OV:g" config_file
but it doesn’t work because I have a line like this in the OV vriable :
8d:c8:c7:r5:m8:e1:ub:f3:09:2s:c1:5a:7a:b7:c0:cb
so i got this error :
sed: -e expression #1, char 2239: unknown option to `s'

sed can use any character as a separator between the regex and the replacement. Choose some character that does not appear in $OV. For example:
sed -i -r "s#config_var1=.*#config_var1=$OV#g" config_file
If all of the characters are used and your script is running on Bash (non-POSIX), you can use substitution while expanding the variable and replacing the delimiter to escape it with the ${var/exp/subst} syntax. If your delimiter is #, you should do it like this: ${OV/#/\\#}. Note the double backslash to display a literal backslash.

Related

How to add double quotation marks ("") around pipe-separated fields [duplicate]

This question already has answers here:
How can I add quotation marks to fields in a CSV file?
(4 answers)
Closed 2 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Let's say I have a file with this structure:
1|2|3|4|
5|6|7|8|
9|10|11|12|
However, I want my file to look like this (expected output):
"1"|"2"|"3"|"4"|
"5"|"6"|"7"|"8"|
"9"|"10"|"11"|"12"|
I am trying to used sed command in the following way:
sed 's/^/"/g'
Unfortunately, it only adds quotation marks at the beginning of each line:
"1|2|3|4|
"5|6|7|8|
"9|10|11|12|
^ means "the beginning of a line". Use [^|] instead which means "anything but |". If your implementation of sed supports +, you can use
sed -E 's/[^|]+/"&"/g'
otherwise, you need to be more verbose
sed 's/[^|][^|]*/"&"/g'
& represents the matched part.
You can use
sed -E 's/[^|]+/"&"/g' file > newfile
The -E option enables the POSIX ERE syntax and [^|]+ thus matches one or more chars other than |, and "&" replaces each with its copy enclosed with " on both sides.
See the online sed demo:
s='1|2|3|4|
5|6|7|8|
9|10|11|12|'
sed -E 's/[^|]+/"&"/g' <<< "$s"
Output:
"1"|"2"|"3"|"4"|
"5"|"6"|"7"|"8"|
"9"|"10"|"11"|"12"|
Here is a gnu awk way of doing the same:
awk -v RS="[|\n]+" '{ORS=RT; print "\"" $0 "\""}' file
"1"|"2"|"3"|"4"|
"5"|"6"|"7"|"8"|
"9"|"10"|"11"|"12"|

Use sed with variable that contains dot [duplicate]

This question already has answers here:
Search and replace with sed when dots and underscores are present
(5 answers)
Closed 6 years ago.
I am trying to replace (for instance) 6.0 by 6.1 in a file, without 640 being replaced by 6.1
I have currently:
sed -i "s/$previousName/$newName/" 'myFile'
I think that the solution could be in here, but I don't find the right solution.
EDIT both string are inside a variable and the question this is supposed to be a duplicate of doesn't treat this case
Using an inner sed:
sed -i "s#$(echo $previousName | sed 's/\./\\./g')#$newName#g" myFile
Try this:
sed -i "s/6\.0/6.1/" 'myFile'
The key is to escape the . character in the pattern which has special meaning. By default it matches any character (including 0 in 640), whereas with a \ in front of it, it only matches a literal ..
Since you have the pattern in a variable, you could escape the . in it first like this:
previousNameE="$(sed -e 's/\./\\./' <<< "$previousName")"
sed -i "s/$previousNameE/$newName/" 'myFile'
if perl is acceptable:
perl -i -pe "s/\Q$previousName/$newName/" 'myFile'
From perldoc for \Q
Returns the value of EXPR with all the ASCII non-"word" characters
backslashed. (That is, all ASCII characters not matching
/[A-Za-z_0-9]/ will be preceded by a backslash in the returned string,
regardless of any locale settings.) This is the internal function
implementing the \Q escape in double-quoted strings
Another example:
$ echo '*.^[}' | perl -pe 's/\Q*.^[}/q($abc$)/e'
$abc$
Further reading: Perl flags -pe, -pi, -p, -w, -d, -i, -t?

sed not working correctly when using variables

source=<!--jta-data-source>jdbc/FCBDataSource</jta-data-source-->
destination=<jta-data-source>jdbc/FCBDataSource</jta-data-source>
sed -i "s/$source/$destination/g" /home/rohan/R2.5LZN/UIReleasedArea/obp.ui.domain/persistence.xml
I am getting error sed: -e expression #1, char 44: unknown option to s
Consider what happens when the substitution occurs. The command becomes:
sed -i 's/<!--jta-data-source>jdbc/FCBDataSource</jta-data-source-->/<jta-data-source>jdbc/FCBDataSource</jta-data-source>/g' some_filename
Then, sed sees s/<!--jta-data-source>jdbc/FCBDataSource</j… and thinks that you want to replace an occurrence of <!--jta-data-source>jdbc with the text FCBDataSource<, and the s command has an illegal j modifier (and other junk).
You need to pick a delimiter character that does not appear in either the pattern or the replacement text. A , will do.
destination='<!--jta-data-source>jdbc/FCBDataSource</jta-data-source-->'
source='<jta-data-source>jdbc/FCBDataSource</jta-data-source>'
sed -i "s,$source,$destination,g"
/home/rohan/R2.5LZN/UIReleasedArea/obp.ui.domain/persistence.xml
The problem was with the variable
Quotes were required

SED command gives unknown option to `s' error [duplicate]

This question already has an answer here:
sed fails with "unknown option to `s'" error [closed]
(1 answer)
Closed 7 years ago.
I have a file like this:
1 = zara
2 = gucci
I want to write a bash script which read two variables and decides to change the number in one of lines to a new number. I have this script for this purpose:
pattern="$num[[:space:]]=[[:space:]].*"
sed -ie 's,'"$pattern"','"$num"',\ =\ '"$newBrandName"',' $fileLocation
Variables num, newBrandName and fileLocation have the right value; I have checked this by echo command.
But when I run the script, I get this error:
sed: -e expression #1, char 52: unknown option to `s'
The output of echo on the SED command is this:
sed -ie s,1[[:space:]]=[[:space:]].*,1\ =\ sign, /root/info.info
Can anyone help me with the regex?
The form of s is s/regexp/replacement/. You can't have multiple expressions on it. Try this instead:
sed -i -e '/^${num}[[:space:]]*=[[:space:]]*/s|.*|$num = $newBrandName|" "$fileLocation"
It would find the line matching ^${num}[[:space:]]*=[[:space:]]* and replace everything on it with $num = $newBrandName.
You also should separate -i and -e so e would not be interpreted as the backup suffix for -i.
If think it's because you have an extraneous , left :
pattern="$num[[:space:]]=[[:space:]].*"
sed -ie 's,'"$pattern"','"$num"',\ =\ '"$newBrandName"',' $fileLocation
--------------------------------^
You might consider escaping it, or use another character like #.

sed replace command

I want to uncomment one of these two statements as per condition:
#export CATALINA_OPTS="-javaagent:$TOMCAT_INSTALL_LOCATION/$APACHE_TOMCAT_VERSION/lib/oi-mule-libs/usage/injector.jar"
#export CATALINA_OPTS="-javaagent:$TOMCAT_INSTALL_LOCATION/$APACHE_TOMCAT_VERSION/lib/agents/cl-proxy-agent.jar"
I used following sed command:
#sed -i 's/#export CATALINA_OPTS="-javaagent:$TOMCAT_INSTALL_LOCATION/$APACHE_TOMCAT_VERSION/lib/oi-mule-libs/usage/injector.jar"/export CATALINA_OPTS="-javaagent:$TOMCAT_INSTALL_LOCATION/$APACHE_TOMCAT_VERSION/lib/oi-mule-libs/usage/injector.jar"/' $TOMCAT_HOME/bin/init-script.sh
I got following error:
sed: -e expression #1, char 85: unknown option to `s'
Why did I get this error?
How could I achieve my purpose by modifying above command?
You should change your delimeters to the sed command. Your delimiter is '/' which also appers in your pattern and that gives an error. Try this:
sed 's:<pattern to be replaced>:<replacement pattern>:g'
Here g is for global, s for substitute. You can use any other delimiter not appearing in your pattern, I have used colon ':' in the example
You need to either escape slash characters with backslashes or use another character as a delimiter for the "s"ubstitute command (as pointed out by sleeping_dragon)
With that in mind:
sed -e s/^#(.*\/injector.jar.*)/\1/ -e s/^#(.*\/cl-proxy-agent.jar.*)/\1/ $my_file
making the regular expression in round brackets as unique as required to identify the exact string you need uncommenting.

Resources