I am trying to make a simple replacement for an IPv4 address script.
Here is my code.
#!/bin/sh
sed 's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/192.100.100.100/g/'
What is happening is every time I call:
example.sed example1 > example.output
I get:
sed: -e expression #1, char 75: unknown option to `s'
where the 75th char is the 1 from 192.100.100.100.
Why?
Drop the trailing slash!
sed -e \
's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/192.100.100.100/g'
(The split over two lines attempts to ensure visibility of the end of the string.)
Note that sed starts counting at the s at the start of the s/// string, not at the start of the sed word. The trailing / was at character 75 in that string.
The full script should probably add "all the command line arguments" (aka "$#") too:
sed -e \
's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/192.100.100.100/g' \
"$#"
This will read files if they're specified (and would also accept extra editing commands such as -e 's/.*//'!) or standard input if no arguments are specified. You can put it all on one line in your script; the breaks are for improved clarity on Stack Overflow.
Related
I have a variable called varname which have characters such as /,. etc. Requirement is to use it with sed command. This is what I've done.
echo Hello admin, please add , after you enter the image name
read -p varname
sed -i "s/my-images=/&$varname/" /home/myconfig
echo Image $varname has been added to the configuration. Thanks!!
/home/myconfig has
id=1
max-mb=1000
my-images=customimage
And required output is
id=1
max-mb=1000
my-images=mynewtext/version1,customimage
I am getting error while running this code and the error is : sed: -e expression #1, char 28: unknown option to `s'
Any help would be appreciated.
You could escape all /'s in your variable with a backslash \ (a literal \ must be escaped as \\):
sed -i "s/my-images=/&${varname//\//\\/}/" /home/myconfig
if you have entered text with slash (as I can see you have mynewtext/version...), it will recognized as finish s command.
You can try to use another character as separator in sed, like:
sed -i "s#my-images=#&$varname#" /home/myconfig
I'm trying replace a word in a file using sed.
In the same bash script I use the command :
sed -i "s/${list[$index]}/${phone}/g" $1
And it's working flawlessly on the first function, but the second function I wrote:
sed -i "s/${list[$index]}/${zipcode}/g" $1
Outputs this error:
sed: -e expression #1, char 0: no previous regular expression
I'm really desperate, I'm pretty sure that it's a dumb mistake I'm doing but I can't sort it out
When the first half of a sed substitute command is empty:
sed 's//foo/' <<< bar
It returns this error:
sed: -e expression #1, char 0: no previous regular expression
Therefore, as William Pursell commented, there's a value of the ${list[#]} array that's empty, or maybe $index is out of the array's range.
I am using shell script. My requirement is to find and replace the string. The string contains "/" char as well. I am getting error sed: -e expression #1, char 18: unterminated `s' command. Can someone tell how should i replace the string which has "/"?
#!/bin/bash
...
search_string="../conf/TestSystem/Inst1.xml"
rep="Inst1/Instrument.xml"
sed -i 's|${line}|${rep}/g' MasterConfiguration.xml
I tried using another sed command but that one also gave error sed: -e expression #1, char 13: unknown option to `s'
sed -e "s/${line}/${rep}/g" MasterConfiguration.xml > tempfile
Whenever you deal with shell-variables you have to get them out of the "sed-string":
For example:
sed -e "s/"${line}"/"${rep}"/g" MasterConfiguration.xml > tempfile
Otherwise sed will treat the chars as-is and search for ${line} literally:
As you see, nothing happens here.
Furthermore, if your variables contain / you need to use another delimiter for sed. I tend to use ~ in such a case, but you're free to use other chars - just be consequent and don't mix them like in your first example-sed-command:
sed 's~'${line}'~'${rep}'/g' //WRONG
sed 's~'${line}'~'${rep}'~g' //RIGHT
Combine both and it will work:
You can try this sed,
sed -i "s#${line}#${rep}#g" MasterConfiguration.xml
Problem:
Instead you have,
sed -i "s|${line}|${rep}/g" MasterConfiguration.xml
It should be,
sed -i "s|${line}|${rep}|g" MasterConfiguration.xml
Syntax:
sed "s|pattern|replacement|g"
I want to delete in a file each line beginning by '#'. I ran that (I am using osx)
sed -i '' -e 's/#.*/d' file
but I get this error message :
sed: 1: "s/#.*/d
": unescaped newline inside substitute pattern
The s command in sed means "substitute" and it takes two arguments:
s/pattern/replacement/
What you want to do is just to match lines starting with # and delete them, so you need the sed program:
/^#/d
Note that the pattern needs to start with ^ (meaning "start of line") otherwise it will match a # anywhere in the line.
As stated by Gareth Rees above, the correct command is:
sed '/^#/ d' file
This good sed tutorial contains your question as an example:
http://www.grymoire.com/Unix/Sed.html#toc-uh-30
First of all i apologise in case this has been answered before but i couldn't solve my problem.
I need to search a pattern and then replace it with a line of text comprising of both text and variable.Btw i am using bash..
say
$var = "stacko.ver/rulz=" **Note: $var contain double quotes & = & a dot and /**
i want to so the follow
1.Search for ;te.xt = Note: The value to be search contain ; & = and a dot
2.Replace it with
textnum=$var
Of course $var should be replaced with its actual value
My attempts
sed -i "s/;te.xt =/textnum=$var/" file
sed -i "s/;te.xt =/textnum="$var"/" file
sed -i "s/";te.xt ="/"textnum=$var"/" file
None of these actually worked , either sed giving me an error or the value of $var not shown in file
Thanks for the help
Regards
Quoting doesn't help since this is a sed issue, not a bash issue. Just pick a sed s-expression delimiter that doesn't appear in your text:
sed -i "s|;te.xt =|textnum=$var|" file
You can pick any delimiter for s that doesn't appear in your input. sed -e 'streetlight' is a perfectly valid sed command.
I can see the error:
$ var="stacko.ver/rulz="
$ data="foo ;te.xt = bar"
$ sed "s/;te.xt =/textnum=$var/" <<< "$data"
sed: -e expression #1, char 31: unknown option to `s'
The problem is that $var contains a slash, so sed's s/// command is breaking. You need to pick a character that does not appear in $var
$ sed "s#;te.xt =#textnum=$var#" <<< "$data"
foo textnum=stacko.ver/rulz= bar
This can be hard -- what if slash and hash are in $var? Using bash, you can use ANSI-C quoting to use a control character that is unlikely to appear in your data, e.g.
$ sed $'s\037;te.xt =\037textnum=$var\037' <<< "$data"
foo textnum=stacko.ver/rulz= bar