I have a following requirement
String 1 : http://gaa-dev.gk.gbcd:9701/analytics
String 2 : https://gaa-prod.gk.gbvd/analytics
There were around 40 different locations in various files(xml content) under /home/gaauser
I want String1 to be REPLACED BY String2
Tried SED, but no luck and I am struck now. Any inputs greatly appreciated.
thanks
Two fixes are needed:
Since you have / in your strings, it is recommended to use a different delimiter or you need to tell sed that it is not a delimiter by escaping them.
Since you are using variables, use double quotes to allow them to interpolate.
str1='http://gaa-dev.gk.gbcd:9701/analytics'
str2='https://gaa-prod.gk.gbvd/analytics'
echo 'http://gaa-dev.gk.gbcd:9701/analytics' | sed "s/$str1/$str2/"
sed: -e expression #1, char 11: unknown option to `s'
echo 'http://gaa-dev.gk.gbcd:9701/analytics' | sed "s|$str1|$str2|"
https://gaa-prod.gk.gbvd/analytics
Related
This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 1 year ago.
I want to ask 2 questions about sed.
For example, when I try to put a string to sed which contains special character like (.\n*.+) and sed cannot run properly.
set a = ".\n*.\+"
set input = ".\n*.\+adsdfasdf"
Then execute:
echo "$input" | sed 's/'$a'/hi/g' # It will give: sed: No match
but
echo "$input" | sed "s#${a}#hi#g" # It will run but not true
My questions are:
What is the difference between these commands: sed 's///' and sed
"s###"
How to treat input just as it is purely string?
1. What is the difference between these commands: sed 's///' and sed "s###"
--- In your case, / or # is a separator of the crucial "sections":
The s command (as in substitute) is probably the most important in sed
and has a lot of different options. The syntax of the s command is
‘s/regexp/replacement/flags’.
...
The / characters may be uniformly replaced by any other single character within any given s command.
2. How to treat input just as it is purely string?
--- To expand/interpolate a variables within sed command, those variables OR the whole expression should be enclosed with double quotes:
set a = ".\n*.\+"
echo "$input" | sed "s/$a/hi/g"
Well, you have a \n in the input string, which you substitute without quote delimiters in 's/'$a'/hi/g' and is parsed as a space, so you pass actually two parameters to sed(1) and you substitute as only one parameter (with the \n included as one character) in only one parameter in "s#$a#hi#g" (in which double quotes include the variable substitution). There is actually no difference in the character used as delimiter in sed(1), but you have called it differently in the two calls.
Finally, I found out that the separator MUST BE different with any characters in the string!
Or it will cause error!
Thus, now I will change the separator to #. It's like comment character.
This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 6 years ago.
I'm trying to replace text in a file with the output of another command. Unfortunately, the outputted text contains characters bash expands. For example, I'm running the following script to change the file (somestring references output that would break the sed command):
#!/bin/bash
somestring='$6$sPnfj/lnXwZVrec7$fCnL9uy1oWIMZduInKTHBAxhsQxGCsBpm2XfVFFqDPHKidrd93yfjbYvKgYexXHVcvkKdu9lbfy16Ek5GvKy/1'
sed '0,/^title/s/^title*/'"$somestring"'\n&/' $HOME/example.txt
sed fails with this error:
sed: -e expression #1, char 30: unknown option to `s'
I think bash is substuting the contents of $somestring when building the sed command, but is then trying to expand the resulting text. I can't put the entire sed script in single quotes, I need bash to expand it the first time, just not the second. Any suggestions? Thanks
here the forward slash / is the problem. If it's the only issue you can set sed to use a different delimiter.
for example
$ somestring="abc/def"; echo xxx | sed 's/xxx/'"$somestring"'/'
sed: -e expression #1, char 11: unknown option to `s'
$ somestring="abc/def"; echo xxx | sed 's_xxx_'"$somestring"'_'
abc/def
you also need to worry about & and \ chars and escape them if can appear in the replacement text.
If you can't control the the replacement string, either you have to sanitize with another sed script or, alternatively use r command to read it from a file. For example,
$ seq 5 | sed -e '/3/{r replace' -e 'd}'
1
2
3slashes///1ampersand&and2backslashes\\end
4
5
where
$ cat replace
3slashes///1ampersand&and2backslashes\\end
You have several errors here:
the string somestring has characters that are significative for sed command (the most important being '/' that you are using as a delimiter) You can escape it, by substituting it with a previous
somestring=$(echo "$somestring" | sed -e 's/\//\\\//g')
that will convert your / chars to \/ sequences.
you are using sed '0,/^title/s/^title*/'"$somestring"'\n&/' $HOME/example.txt which is looking to substitute the string titl followed by any number of e characters by that $somestring value, followed by a new line and the original one. Unfortunately, sed(1) doesn't allow you to use newline characters in the pattern substitution side of the s command, but you can afford the result by using the i command with a text consisting of you pattern (preceding any new line by a \ to interpret it as literal):
Finally the script leads to:
#!/bin/bash
somestring='$6$sPnfj/lnXwZVrec7$fCnL9uy1oWIMZduInKTHBAxhsQxGCsBpm2XfVFFqDPHKidrd93yfjbYvKgYexXHVcvkKdu9lbfy16Ek5GvKy/1'
somestring=$(echo "$somestring" | sed -e 's/\//\\\//g')
sed '/^title/i\
'"$somestring\\
" $HOME/example.txt
If your shell is Bash, you can use parameter substitution to replace the problematic /:
somestring="{somestring//\//\\/}"
That looks scary, but is easier to understand if you look at the version that replaces x with __:
somestring="${somestring//x/__}"
It might be easier to use (say) underscore as the delimiter for your sed s command, and then the substitution above would be
somestring="${somestring//_/\\_}"
If you already have backslashes, you'll need to first replace those:
somestring="${somestring//\\/\\\\}"
somestring="{somestring//\//\\/}"
If there were other characters that needed escaping (e.g. on the search side of s///), then you could extend the above appropriately.
This URL provides the cleanest answer:
Command to escape a string in bash
printf "%q" "$someVariable"
will escape any characters you need escaped for you.
I have a problem with sed. I want to replace the entire specific line number for multiple lines in multiples documents.
This the bash command for 1 specific line in 1 specific document:
BNAME=$(basename $FILE .pdb)
psfgen1="pdb ./sedpdb/${BNAME}.pdb/"
sed -i '8s/'.*'/'${psfgen1}'/' ./psfgen.inp
And I get this error :
sed: -e expression #1, char 60: unterminated `s' command
Is anyone know how to solve this issue? Thanks!
I can see two things wrong:
There are forward slashes in the string that you're attempting to use in the sed command. These will be interpreted as part of the command, so you should use a different delimiter.
The * is unquoted, so will be glob-expanded by the shell to the names of all the files in the directory.
Reliably using shell variables in string substitutions is non-trivial but can be done using one of the approaches shown in the answers to this question.
In your case, it looks like you can probably get away with using another character as the delimiter, such as #:
sed -i "8s#.*#${psfgen1}#" ./psfgen.inp
I am trying to substitute a word using sed with two words. For example I'm using:
sed s/TITLE/New Title/ old.txt > new.txt
However, when I run the command the following populates:
sed: -e expression #1, char 17: unterminated `s' command
Any help would be of great appreciation. I've searched everywhere without any clarity.
You are missing quotes. You need to wrap the substitution portion inside quotes like:
sed 's/TITLE/New Title/' old.txt > new.txt
If you are using variables as part of substitution, you'll need to use double quotes " instead of single quotes ' to allow variables to interpolate.
Take a look at sed man page and explore -i option which allows you to make in-place changes.
How can I fix this:
abc="a/b/c"; echo porc | sed -r "s/^/$abc/"
sed: -e expression #1, char 7: unknown option to `s'
The substitution of variable $abc is done correctly, but the problem is that $abc contains slashes, which confuse sed. Can I somehow escape these slashes?
Note that sed(1) allows you to use different characters for your s/// delimiters:
$ abc="a/b/c"
$ echo porc | sed -r "s|^|$abc|"
a/b/cporc
$
Of course, if you go this route, you need to make sure that the delimiters you choose aren't used elsewhere in your input.
The GNU manual for sed states that "The / characters may be uniformly replaced by any other single character within any given s command."
Therefore, just use another character instead of /, for example ::
abc="a/b/c"; echo porc | sed -r "s:^:$abc:"
Do not use a character that can be found in your input. We can use : above, since we know that the input (a/b/c/) doesn't contain :.
Be careful of character-escaping.
If using "", Bash will interpret some characters specially, e.g. ` (used for inline execution), ! (used for accessing Bash history), $ (used for accessing variables).
If using '', Bash will take all characters literally, even $.
The two approaches can be combined, depending on whether you need escaping or not, e.g.:
abc="a/b/c"; echo porc | sed 's!^!'"$abc"'!'
You don't have to use / as pattern and replace separator, as others already told you. I'd go with : as it is rather rarely used in paths (it's a separator in PATH environment variable). Stick to one and use shell built-in string replace features to make it bullet-proof, e.g. ${abc//:/\\:} (which means replace all : occurrences with \: in ${abc}) in case of : being the separator.
$ abc="a/b/c"; echo porc | sed -r "s:^:${abc//:/\\:}:"
a/b/cporc
backslash:
abc='a\/b\/c'
space filling....
As for the escaping part of the question I had the same issue and resolved with a double sed that can possibly be optimized.
escaped_abc=$(echo $abc | sed "s/\//\\\AAA\//g" | sed "s/AAA//g")
The triple A is used because otherwise the forward slash following its escaping backslash is never placed in the output, no matter how many backslashes you put in front of it.