Using sed to replace the same text line in multiple files - bash

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

Related

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.

linux bash sed search and replace with sed characters

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

sed replace command

I want to uncomment one of these two statements as per condition:
#export CATALINA_OPTS="-javaagent:$TOMCAT_INSTALL_LOCATION/$APACHE_TOMCAT_VERSION/lib/oi-mule-libs/usage/injector.jar"
#export CATALINA_OPTS="-javaagent:$TOMCAT_INSTALL_LOCATION/$APACHE_TOMCAT_VERSION/lib/agents/cl-proxy-agent.jar"
I used following sed command:
#sed -i 's/#export CATALINA_OPTS="-javaagent:$TOMCAT_INSTALL_LOCATION/$APACHE_TOMCAT_VERSION/lib/oi-mule-libs/usage/injector.jar"/export CATALINA_OPTS="-javaagent:$TOMCAT_INSTALL_LOCATION/$APACHE_TOMCAT_VERSION/lib/oi-mule-libs/usage/injector.jar"/' $TOMCAT_HOME/bin/init-script.sh
I got following error:
sed: -e expression #1, char 85: unknown option to `s'
Why did I get this error?
How could I achieve my purpose by modifying above command?
You should change your delimeters to the sed command. Your delimiter is '/' which also appers in your pattern and that gives an error. Try this:
sed 's:<pattern to be replaced>:<replacement pattern>:g'
Here g is for global, s for substitute. You can use any other delimiter not appearing in your pattern, I have used colon ':' in the example
You need to either escape slash characters with backslashes or use another character as a delimiter for the "s"ubstitute command (as pointed out by sleeping_dragon)
With that in mind:
sed -e s/^#(.*\/injector.jar.*)/\1/ -e s/^#(.*\/cl-proxy-agent.jar.*)/\1/ $my_file
making the regular expression in round brackets as unique as required to identify the exact string you need uncommenting.

Replace the string "//" with "/// " using the shell

I am trying to generate documentation of the Octave API using doxygen. However, doxygen recognizes only comments after /// to be documentation and the comment style in Octave is to use only //. I decided to run a shell command to change this and found this
sed -i 's/old-word/new-word/g' *.txt
from here. When I test the command using
sed -i 's/////// /g' *.txt
or
sed -i 's/"//"/"/// "/g' *.txt
I get the error:
sed: -e expression #1, char 5: unknown option to `s'
or
sed: -e expression #1, char 6: unknown option to `s'
respectively. How do I correctly use sed to replace // with ///?
Alternatively, is a simpler way, such as, for example, opening all the files simuntaniously in some IDE and refactoring.
Try:
sed -i 's+//+///+g' *.txt
It is unlucky to use / as a delimiter if you need to substitute /// for // xD.
Use strrep() (string replace).
I am assuming variable "s" contains your string containing "//". Add the following code in your script:
strrep (s,"//","///");
In this case / acts as escape sequence for / and // .

sed: unterminated s command from bash script

This exact code was working in another script. I copied the function over and now it keeps
giving the unterminated 's' command error.
sed "
s#<%BRANCH-NAME%>#${_loc_name}#g
s#<%BRANCH-ADDR%>#${_loc_strt}#g
s#<%BRANCH-CTY%>#${_loc_city}#g
s#<%CUST-NAME%>#${_pat_name}#g
s#<%CUST-ADDR%>#${_pat_addr}#
s#<%CUST-CTY%>#${_pat_city}#
s#<%BARCODE%>#${_barcode}#g
s#<%DATE%>#${_date}#
s#<%TITLE%>#${_title}#
s#<%AUTHOR%>#${_auth}#
s#<%PRICE%>#${_price}#" "$_template"
In response to requests for possible values:
These are huge files and this function is in a for loop. I use it to format forms for mailing. More info here: BASH: importing data from flat file into template
So this function worked for certain directories but I've hit a snag with this recent one.
I grepped for slash characters on the advice of kev below ...none found except '/'. Possibly
'silent' newlines.
find ./ -name "mail.TMP" -type f -exec grep -E '\n' {} \;
possible values would be things like:
${pat_name}
Hermann Hesse c/o His Mother
${loc_name}
Pandora SR home c/o Harmony City Hall
So ...maybe it's a quoting issue?
$ _loc_name=$'xxx\nyyy'
$ echo '<%BRANCH-NAME%>' | sed "s#<%BRANCH-NAME%>#${_loc_name}#g"
sed: -e expression #1, char 21: unterminated `s' command
Does any $var contain newline? Please echo them all to check.
$ echo "$_loc_name"
xxx
yyy

Resources