remove everything between two characters with sed - sed

I'd like to remove any characters between including them also
<img src=\"/wp-content/uploads/9e580e68ed249dec8fc0e668da78d170.jpg\" / hspace=\"5\" vspace=\"0\" align=\"left\">
I was trying
sed -i -e 's/<img src.*align=\\"left\\">//g' file

You do not say what version of sed you are using, or what shell.
With GNU sed and bash, your attempt was almost there. Try:
sed -i 's/<img src[^>]*align=\\"left\\">//g' file
Explanation:
s/<img src[^>]*align=\\"left\\">/ search for <img src_STUFF_align=\"left\">, where _STUFF_ cannot contain any >
// and replace it with nothing
/g and continue
-i and modify the file
I believe this should work with most version of sed (except for the -i).

Related

remove special character (^#) with sed

I want to remove "^#^#^#^#^#^#^#^#^#" from my textfile. I tried the following, but it did not work:
sed -i 's/\\^#//g' myfile.txt
sed is not generally robust against null characters. But Perl is, and tr:
tr -d '\000' <myfile.txt >newfile.txt
Some sed variants will be able to handle null bytes with the notation which works in Perl:
perl -i -pe 's/\x00//g' myfile.txt
The -i option says to replace the original file, like some sed variants also allow you to.
^# is one of the ways how to display the null byte. sed (at least the GNU one) represents it as \x00:
sed 's/\x00//g'

SED inplace file change inside make - How?

sed inplace change on a file is not working inside Make object.
I want to replace a line in a file with sed called in a make object. But it does not seem to be working. How can I fix this?
change_generics:
ifeq ($(run_TESTNAME), diagnostics)
ifeq ($(run_TESTCASE), 1)
sed -i -e "s/SIM_MULTI\==[a-z,A-Z]*/SIM_MULTI=TRUE/" ./generics.f
else ifeq ($(TESTCASE), 2)
sed -i -e "s/SIM_MISSED\==[a-z,A-Z]*/SIM_MISSED=TRUE/" ./generics.f
endif
endif
I would like the generics.f file changed with that one line change. But it remains the same as the original. The sed command works outside make.
I can't reproduce this using GNU sed 4.2.2 and GNU make 3.82, or at least, I can't reproduce any scenario where the same sed command works from the command line but not in a Makefile.
Simpler Makefile:
all:
# Contrived just so I can test your 2 sed commands.
sed -i -e "s/SIM_MULTI\==[a-z,A-Z]*/SIM_MULTI=TRUE/" ./generics.f
sed -i -e "s/SIM_MISSED\==[a-z,A-Z]*/SIM_MISSED=TRUE/" ./generics.f
Sample file content in generics.f:
SIM_MULTI=foo
SIM_MISSED=bar
Testing:
$ make all
sed -i -e "s/SIM_MULTI\==[a-z,A-Z]*/SIM_MULTI=TRUE/" ./generics.f
sed -i -e "s/SIM_MISSED\==[a-z,A-Z]*/SIM_MISSED=TRUE/" ./generics.f
Confirmed that both sed commands fail to edit a file with this content.
To fix:
Probably, you need to simply remove the \= from your regular expression. The backslash there has no effect, and causes your regex to simply match two equals signs ==. Thus this works:
all:
sed -i 's/SIM_MULTI=[a-zA-Z]*/SIM_MULTI=TRUE/' ./generics.f
sed -i 's/SIM_MISSED=[a-zA-Z]*/SIM_MISSED=TRUE/' ./generics.f
Testing:
$ make all
sed -i 's/SIM_MULTI=[a-zA-Z]*/SIM_MULTI=TRUE/' ./generics.f
sed -i 's/SIM_MISSED=[a-zA-Z]*/SIM_MISSED=TRUE/' ./generics.f
$ cat generics.f
SIM_MULTI=TRUE
SIM_MISSED=TRUE
Further explanation:
There is no need to specify -e there.
There is no need to enclose the script in double quotes, which is riskier because it allows the contents to be modified by the shell.
The bug appears to be \= and I deleted those characters, as mentioned above.
Note that I removed the comma , as well in [a-z,A-Z]. I think that probably isn't what you meant, and it would cause a class of characters including a-z, A-Z and a comma , to be matched by the regex. (And if it is what you mean, you might consider writing it as [a-zA-Z,] as that would be less confusing.)
If this has not resolved your issue, I would need to know things like:
What is the version of your sed.
What is the contents in generics.f.
POSIX/GNU sed have c for "change":
sed -i '/SIM_MULTI=/c\SIM_MULTI=TRUE'
sed -i '/SIM_MISSED=/c\SIM_MISSED=TRUE'

how to replace softtabs with hardtabs in sed

I am trying to replace softtabs with hardtabs in sed. I have tried the following but to no avail:
sed -i 's/ /\t/g' path/to/file
What am I doing wrong?
It appears what I was looking for was the unexpand command.
unexpand -a -t4 file > newfile
If you have a file like this where all space is spaces and not tabs:
cat file
test more data
here are more
You can use
sed 's/ */\t/g'
or
sed -r 's/ +/\t/g'
and get
test more data
here are more
Where it now have changed multiple spaces to tab

sed: can't find label for jump to `omcat-users>/d'

remove everything in a file from to in /etc/tomcat7/tomcat-users.xml
sudo sed '/<tomcat-users>/,/</tomcat-users>/d' /etc/tomcat7/tomcat-users.xml
error:
sed: can't find label for jump to omcat-users>/d
What is wrong with the syntax?
Try
sed '/<tomcat-users>/,/<\/tomcat-users>/d' /etc/tomcat7/tomcat-users.xml
---^
The / needs to be escaped
To remove that section from the input use the -i flag
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
i.e.
sed -i '/<tomcat-users>/,/<\/tomcat-users>/d' /etc/tomcat7/tomcat-users.xml
Why not just change the separator in sed
sed ':<tomcat-users>:,:</tomcat-users>:d' /etc/tomcat7/tomcat-users.xml
Then you do not need to escape the /

using sed for substitution in next line

I am working on sed command to translate some text into another text.
cat text
<strong>ABC
</strong>
Commnad:
sed -e 's|<strong>(.*?)</strong>|//textbf{1}|g'
Expected Outcome: \textbf{ABC}
but using above script i cannot convert it into expected output since there is new line between the tags. How to handle such cases?
This might work for you (GNU sed):
sed -r '$!N;s|(<)(strong>)([^\n]*)\n\s*\1/\2|//textbf{\3}|;P;D' file
or
sed '$!N;s|\(<\)\(strong>\)\([^\n]*\)\n\s*\1/\2|//textbf{\3}|;P;D' file
sed -e 'N;s|<strong>\(.*\?\)\n</strong>|\/textbf{\1}|g'
as said by CodeGnome and David Ravetti, the N flag allows for multi-line patterns.