sed does not work as expected with variables in script [duplicate] - linux

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed last year.
I have a small script that does some global string replacements in various files. This should be done with sedso that only the affected lines are changed.
Preparation:
#!/bin/sh
find="Mücke Motorsport"
replace="Mücke Motorsport Racing Team"
file="/Users/meyer/Dropbox/Dev/App Framework iOS/dev/myfile.txt"
This is my problematic line in the script:
sed -i s/"$find"/"$replace"/g "$file"
Does not work (sed: 1: "/Users/meyer/Dropb ...": invalid command code o). Next try:
sed -i 's/'"$find"'/'"$replace"'/g' "$file"
Does not work (sed: 1: "/Users/meyer/Dropb ...": invalid command code o). Next try:
sed -i "s/$find/$replace/g" "$file"
Does not work (sed: 1: "/Users/meyer/Dropb ...": invalid command code o). Next try:
sed -i s/$find/$replace/g "$file"
Does not work (sed: 1: "Motorsport/Mücke": invalid command code M). Because of this error message I tried to escape my variables:
escaped_ find =$(printf '%s\n' "$find" | sed 's:[][\/.^$*]:\\&:g')
escaped_ replace =$(printf '%s\n' "$replace" | sed 's:[\/&]:\\&:g;$!s/$/\\/')
But usage of the replace variables does´t work ... I don´t know how to get this working :(

On the OSX version of sed, the -i option expects an extension argument so your command is actually parsed as the extension argument and the file path is interpreted as the command code.
Try adding the -e argument explicitly
sed -i'' -e 's/old/new/g' file

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'

How to use sed 's/$var/$var2' file [duplicate]

This question already has an answer here:
sed fails with "unknown option to `s'" error [closed]
(1 answer)
Closed 5 years ago.
I'm trying to replace a value shown as a variable with another value shown as a second variable, using the following line:
sed -i "s/$header/$new/" file.f
Where "$header" is the old variable I want replaced with the new one ($new).
I'm getting this error:
sed: -e expression #1, char 20: unknown option to `s'
I've tried
sed -i 's/$header/$new/' file.f
sed -i "s/$header/$new/" file.f
sed -i 's/"$header"/"$new"/' file.f
None of it seem to work.
How should I write this line so I can get the right output (replacing '$header' with '$new' on the file)?
Thanks in advance
sed -i "s/$old/$new/" file
works fine. You can change the separtor if your data has the character /.
sed -i "s#$old#$new#" file
If you are not sure of the content of the variables and to reduce clashing the separator you can use
sed "s^A$old^A$new^A"
to enter the CTRL-A press CTRL-V + CTRL-A (or any other value not expected in vars)

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.

Call to sed works from command line but fails in script

I've been using the following call to sed to remove lines files.
sed "/% Program:/,/mode: SPIKE/d" file.tt > file.tt.nh
I tried writing a bash script to allow me to run the script on many files at once. However sed fails.
sed "/% Program:/,/mode: SPIKE/d" file.tt > file.tt.nh
sed: -e expression #1, char 1: unknown command: `"'
This is the script I've written that isn't working
#!/bin/bash
SEDARG='"/% Program:/,/mode: SPIKE/d"';
for F in *.tt
do
OUT=$F'.nh';
echo 'sed '$SEDARG' '$F' > '$OUT;
sed $SEDARG $F > $OUT
done
What perplexes me is if I copy the echo'd string and paste it in the command line it works fine.
Does anyone know why this script is failing but the command line call works?
Quoting is tricky. Basically, move the double quotes from where you assign to $SEDARG to where you use $SEDARG.
SEDARG='/% Program:/,/mode: SPIKE/d'
for F in *.tt
do
OUT="$F.nh"
echo "sed $SEDARG $F > $OUT"
sed "$SEDARG" "$F" > "$OUT"
done

Resources