I 've got a file named file.conf containing:
this is the configuration text and this is the WORD to change.
Running:
sed -i 's/WORD/"ONE TWO"/g' file.conf
I will have file.conf modified:
this is the configuration text and this is the "ONE TWO" to change.
now if I make a script, using read:
read -p 'word to change' TEXT -> "ONE TWO"
echo $TEXT -> "ONE TWO"
sed -i 's/WORD/'$TEXT'/g' file.conf
it does not work with error message:
sed: -e expression #1, char 11: unterminated `s' command
file.conf is not modified in this case.
but it works if I read $TEXT with only one word without spaces: "ONE" for instance.
Thanx folks.
Double quote variable like this:
sed -i 's/WORD/'"$TEXT"'/g' file.conf
Even safer:
sed -i 's/WORD/'"${TEXT}"'/g' file.conf
Related
I would like to extract substring with sed as below:
#!/bin/bash
txt="[audio.sys.offload.pstimeout.secs]: [3]"
echo $txt|sed -r -e 's/\[[a-zA-Z0-9_.]+\].*/\1/'
expected output is:
audio.sys.offload.pstimeout.secs
Error message:
sed: -e expression #1, char 26: invalid reference \1 on `s' command's RHS
#!/bin/bash
txt="[audio.sys.offload.pstimeout.secs]: [3]"
echo $txt | sed -r -e 's/^\[(.*)\]:.*/\1/'
we're grabbing all the characters from the 1st [ until the last ]: and putting them in a capture group.
Would you like the regex to remain mostly like yours?
by the way - with lazy matching (which isn't supported by sed),
the regex could be cleaner, simply ^\[(.*?\])
New to SED and trying to use it to find a custom string tag and replace with an html hyperlink, but can't get the following SED format to work correctly. Thanks for your help.
Summary:
Find DEV-XXXX in string and replace w/ an html hyperlink, the DEV- string tag will always remain the same but the XXXX digit reference can vary for different strings.
"This is a test of DEV-1212"
"This is a test of DEV-1213 more text"
Expected results:
"This is a test of DEV-1212"
"This is a test of DEV-1213 more text"
This is the SED syntax I've been working with, but haven't been able to make it work correctly.
$ echo "This is a test DEV-1212" | sed -r 's/DEV-^[^0-9]*([0-9]+).*/&/'
**Produces the following error. **
sed: -e expression #1, char 43: unknown option to `s'
Apart from not escaping the \/ when using / as the delimiter, your pattern does not match because the caret ^ asserts the start of the string which will not match.
DEV-^[^0-9]*([0-9]+).*
----^
If there can only be digits after DEV- you could write the pattern as:
echo "This is a test of DEV-1212" | sed -r 's~DEV-[0-9]+~&~'
Or else keep matching non digits and the rest of the line:
echo "This is a test of DEV-1212" | sed -r 's~DEV-[^0-9]*[0-9].*~&~'
Output
This is a test of DEV-1212
Edit
If you want to match only digits:
echo "This is a test of DEV-1212 with more data" | sed -r 's~DEV-[^0-9]*[0-9]+~&~'
Output
This is a test of DEV-1212 with more data
You did not escape /. Why escape ".
echo "This is a test DEV-1212" | sed -r 's/DEV-^[^0-9]*([0-9]+).*/<a href="https:\/\/devtest.net\/&">&<\/a>/'
but use a different delimieter:
echo "This is a test DEV-1212" | sed -r 's|DEV-^[^0-9]*([0-9]+).*|&|'
I have a file containing lines like
echo ((hello world))
I want the output to be
echo (hello world)
Any idea how I can accomplish this with sed?
sed 's/echo \(\(/echo \(\/' myfile
sed: -e expression #1, char 21: unterminated `s' command
You accidentally escaped the delimiter:
sed 's/echo \(\(/echo \(\/' myfile
^
I see you're not using sed -E, so it's better thet you drop all slashes as in BRE, brackets and braces shouldn't be escaped, as opposed to ERE. So the correct command would be (either of the two):
sed 's/echo ((/echo (/' myfile
sed -E 's/echo \(\(/echo \(/' myfile
I want to replace one string with another but I can't. The code is:
updatedb
MCRYPTINI=$(locate mcrypt.ini | grep 'apache2')
MCRYPTSO=$(locate mcrypt.so | grep "/mcrypt.so")
OLD="extension=mcrypt.so"
NEW="extension=$MCRYPTSO"
echo $MCRYPTINI
echo $MCRYPTSO
echo $OLD
echo $NEW
echo "'s/$OLD/$NEW' $MCRYPTINI"
sed -i 's/$OLD/$NEW' $MCRYPTINI
And the result is:
sudo sh testScript.sh
/etc/php5/apache2/conf.d/20-mcrypt.ini
/usr/lib/php5/20121212/mcrypt.so
extension=mcrypt.so
extension=/usr/lib/php5/20121212/mcrypt.so
's/extension=mcrypt.so/extension=/usr/lib/php5/20121212/mcrypt.so' /etc/php5/apache2/conf.d/20-mcrypt.ini
sed: -e expression #1, char 11: unterminated `s' command
For the response I don't need to use 'sed', but it's looks easy and good.
I use sh not bash because I want the code can use in all the systems, so I prefer answers that follow that principle
UPDATE
sed -i "s/$OLD/$NEW/" $MCRYPTINI
error:
sed: -e expression #1, char 14: unknown option to `s'
Add a slash and double quotes:
sed -i "s/$OLD/$NEW/" file
The solution could be:
sed -i "s/$OLD/$NEW/" $MCRYPTINI
but $NEW is a path, so I need to change "/" by other character, for example "+"
sed -i "s+$OLD+$NEW+" $MCRYPTINI
I'd like to replace all the \r\n with < br/ >in a document, and I'm trying this see script below
# sed -i 's/\r\n/<br/>' ~/xxd/*
however i got this error back
sed: -e expression #1, char 12: unknown option to `s'
How do i solve this problem?
Thanks!
Your problem is that you have the / separator in your replacement string so sed is assuming that's the end of your replacement, and that the > following it is a flag.
If your sed is modern enough, just use a different separator character, one that's not in the replacement string:
pax$ echo hello | sed -e 's/e/<br />/'
sed: -e expression #1, char 9: unknown option to `s'
pax$ echo hello | sed -e 's?e?<br />?'
h<br />llo
Alternatively, you can escape the offending character but I try to avoid that since it tends to lead to overly sawtooth sed commands like /\/\/\/\/\/\.
The other thing you may want to watch out for is trying to use \n in your regex since sed operates on lines anyway. If your intent is to just strip carriage returns and insert HTML line breaks, then the following sed command may be better:
s?\r$?<br />?