linux bash sed search and replace with sed characters - bash

I am trying to search and replace the text of a single line with sed characters inside that line. The line is a file path thus it has /*. instead of it. I have searched and found n\ and opposing \ but non of these seem to work. Whatever I am doing wrong it's simple, but I can't see the tree through the forest.
data to edit:
Include conf.d/*.conf
I am trying to edit the
conf.d/*.conf
conf.d/vhosts/*.conf
Include is else where in the file, but this is the only occurrence where it's the first word. this is what I have so far:
sed -i "/^Include/ s/\conf.d/*.conf\/ */\conf.d/vhosts/*.conf\/" /etc/httpd/conf/httpd.conf
I've alsow tried
sed -i "/^Include/ s/[conf.d/*.conf]/[conf.d/vhosts/*.conf]/g" /etc/httpd/conf/httpd.conf
This is the standard search error message:
Error Message: sed: -e expression #1, char 29: unknown option to `s'
I know it's something simple, but I can't seem to figure it out. I have been stuck on this for the past hour. If I can get past this, then I can edit the documentroot and other elements with sed like characters.
sed version GNU sed version 4.2.1

sed -i "s|conf.d/\*.conf|conf.d/vhosts/\*.conf|g" /path/to/file
Changed Separator to |
Escaped asterisk

Related

Using sed to replace the same text line in multiple files

In my current directory (projDir), I have about 41 folders as follows:
ProgOneDir
ProgTwoDir
ProgThreeDir
...
ProgFortyOneDir
...etc (I did not come up with the naming scheme and I am not a systems admin so please don't yell at me for them).
Inside each of the subfolders, there are README.md files I need to edit. Instead of going into each subfolder and editing the files there, I would like to use a sed command to do so. The files are consistently named README.md
I am running CentOS7
My current command is:
find . -name 'README.md' -exec sed -i -e 's/./makeprog $MAKE_FLAGS CFLAGS="-I/usr/local/include/libtool" OFLAGS="-L/usr/local/lib"/./makeprog/g' {} \;
Essentially, I need to switch:
./makeprog $MAKE_FLAGS CFLAGS="-I/usr/local/include/libtool" OFLAGS="-L/usr/local/lib
to
./makeprog
Would somebody be able to assist? The error I get is:
sed: -e expression #1, char 43: unknown option to `s'
I have already looked at:
Using sed to replace text between strings
How to replace a path with another path in sed?
Some other online resources
I believe my error is arising with the ./command. I have followed advice of switching all / to +, but that still didn't work, as I got the following error:
sed: -e expression #1, char 110: unterminated `s' command
Thank you.
Your problem is that you are using slash as the delimiter to the s/// command, but you are putting slashes into the pattern. You can pick a different delimiter that does not appear in the pattern.
Assuming your current directory is projDir, and there is only one call to makeprog:
sed -i -E 's#(./makeprog) .*#\1#' */README.md
or
perl -i -e 's{./makeprog\K.*}{}' */README.md
If you want to specifically match that exact line, then:
# are you missing a trailing double quote?
line='./makeprog $MAKE_FLAGS CFLAGS="-I/usr/local/include/libtool" OFLAGS="-L/usr/local/lib'
sed -i -E "s#$line#./makeprog#" */README.md

Changing a (full pathname) line using sed `s` operation

I'm trying to change #!/usr/bin/python to #!/usr/bin/python2.6
I've tried the following command line:
sed -i -e 's/.#!/usr/bin/python.*/#!/usr/bin/python2.6/' /usr/bin/yum
...which returns the following error:
sed: -e expression #1, char 11: unknown option to `s'
I can't find a good answer anywhere on Google.
Thanks in advance.
You're getting this error because sed interprets s/.#!/usr/b as your first search & replace command, and b isn't a valid flag for the command.
Indeed, the syntax of these commands is s<delimiter><search pattern><delimiter><replace><delimiter><flags>, where the most widely used <delimiter> is /.
You could escape the / in your search pattern and replace string so they aren't interpreted as the delimiter, however since you've got a lot of them I would suggest using another delimiter.
For example, using + as a delimiter your sed command would become sed -i -e 's+.#!/usr/bin/python.*+#!/usr/bin/python2.6+'.
As a side-note, the first . in your command is probably a mistake as the shebang should be written directly at the beginning of the files it appears in.
I assume that your line is first line from your file. With GNU sed:
sed '1s/python$/&2.6/' file
If you want to edit your file "in place" use sed's option -i.

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 gives "sed: 1: "tsunit.js": undefined label 'sunit.js'"

Short questing, why does this line
sed -i '1s/^/#!\/usr\/bin\/env node\n/' tsunit.js;\
Give me this error
sed: 1: "tsunit.js": undefined label 'sunit.js'
in a Makefile, if relevant.
I’m on a Mac.
According to the Apple man page for sed, the -i option takes a required argument specifying the file extension for the backup file. As a result, assuming that you are on a Mac or similar, sed believes that you intended '1s/^/#!\/usr\/bin\/env node\n/' to be the file extension of the backup. It then interprets tsunit.js as a sed command. the leading t tells sed to branch to the label sunit.js which, of course, doesn't exist. Hence the error message.
The solution is:
sed -i '.bak' '1s/^/#!\/usr\/bin\/env node\n/' tsunit.js
Or, if you really do not want a backup:
sed -i '' '1s/^/#!\/usr\/bin\/env node\n/' tsunit.js
Also, it looks like you're inserting a line. sed has more commands than s
sed -i "" '1i\
#!/usr/bin/env node' tsunit.js

Ignoring slashes in sed find and replace command

I'm trying to do a find and replace using sed in a terminal.
Within the apache config file, I'm trying to replace:
DocumentRoot /var/www
with
DocumentRoot /var/www/mysite.com/public_html
From the command line, without using an editor. I'd like to do this with sed, and I've tried various iterations of:
sudo sed -i 's/'DocumentRoot /var/www'/'DocumentRoot /var/www/mysite.com/public_html'/' /etc/apache2/sites-available/mysite.com
However when doing so I get this error:
sed: -e expression #1, char 14: unterminated s command
So it's erroring on the slashes in the paths. How do I get around this?
Thanks for your help
It isn't necessary to use / as the regex delimiter. You can use # or # when your regular expressions contain / that you would otherwise need to escape.
sed -i s#expr1#expr2#
So, it could be:
sudo sed -i 's#DocumentRoot /var/www#DocumentRoot /var/www/mysite.com/public_html#' /etc/apache2/sites-available/mysite.com
Also, as your substitution-argument itself contains spaces, you need to enclose them in single-quotes.
It is not compulsory to use / as separator in sed. sed 's/hi/hey/ is same as sed 's#hi#hey#'

Resources