Replace <br /> using sed does not work [duplicate] - bash

This question already has answers here:
sed search and replace strings containing / [duplicate]
(2 answers)
Closed 4 years ago.
I want to remove <br /> from a file using sed -i ....
The / gives me trouble.
This works:
sed -i e 's/<br/''/g' File.txt
But this didn't work:
sed -i e 's/'<br />'/''/g' File.txt

Use any other char after the s
sed -i 's_/_+_'
Should work
the s subcommand from sed means substitution, and uses the next char as a delimiter, it could be almost anything. but it is a good pratice to not use any char that is related to your target text (HTML in this case), like <>/#.
Also it is advisable to avoid shell relevant ones $!()

Related

sed -i ‘’ -e '/javaagent.jar/ s/$/ proxyPort=8080/' this command should append string only if the line is uncommented [duplicate]

This question already has answers here:
sed - Commenting a line matching a specific string AND that is not already commented out
(10 answers)
sed in-place flag that works both on Mac (BSD) and Linux
(15 answers)
Ignore comments (#) using sed, but keep the lines untouched
(5 answers)
Closed 2 years ago.
Im using below to append proxyport=8080 to the end of the line where javaagent.jar string found and below is working fine, but this should ignore if there is a # (commented line) in the text file
sed -i '' -e '/javaagent.jar/ s/$/ proxyPort=8080/'
Example input:
#/opt/ver/inmind/javaagent.jar
/opt/ver/inmind/javaagent.jar
Example output should be:
#/opt/ver/inmind/javaagent.jar
/opt/ver/inmind/javaagent.jar proxyPort=8080
Assuming you have a FreeBSD sed version (as you have -i '' in your command), you can use
sed -i '' -e '/^#/!s/javaagent\.jar.*/& proxyPort=8080/' file
See the online demo.
The /^#/! part stops processing lines starting with # (add [[:space:]]* after ^ to account for indentation, any leading whitespace), and if there is no # at the start, s/javaagent\.jar.*/& proxyPort=8080/ finds javaagent.jar + the rest of the line and replaces this match with itself (with &) and adds a space and proxyPort=8080.

How do I remove a line that does not contain a specific character? [duplicate]

This question already has answers here:
In-place edits with sed on OS X
(8 answers)
Closed 2 years ago.
For example, I want do remove all lines in a textile that do not contain the character '#'
I have already tried to use sed like so
sed '/#/!d' data.txt
What am I missing? Shouldn't this work?
I prefer using ed over the non-standard sed -i, especially if it needs to be portable:
printf "%s\n" "v/#/d" w | ed -s filename
This deletes every line that doesn't contain a #, and saves the changed file back to disc.
sed -n '/#/p' [file]
-n suppress default printing
/#/ match on # anywhere on the line
p print if it matches
Add -i for in-place editing of the file (if supplied).

How to use sed to replace string that contains slash? [duplicate]

This question already has answers here:
How to insert strings containing slashes with sed? [duplicate]
(11 answers)
Closed 4 years ago.
I wanto replace # SigningTable refile:/etc/opendkim/SigningTable to SigningTable refile:/etc/opendkim/SigningTable.
Which means just remove #.
I use sed -i 's/# SigningTable refile:/etc/opendkim/SigningTable/ SigningTable refile:/etc/opendkim/SigningTable/g' /etc/opendkim.conf,but doesn't work.
I think it's because /,how to use sed to replace string with /?
You can change the delimiter:
sed -i 's!TEXT!REPLACE!' file
You can use this, if SigningTable appears once in the file:
sed '/SigningTable/{s/^# *//}' in
or be more specific:
sed '\#refile:/etc/opendkim/SigningTable#{s/^#//}' in

Find variable word and replace it in file using Bash [duplicate]

This question already has answers here:
Replace a string in shell script using a variable
(12 answers)
Closed 5 years ago.
I have a file and from this file I am trying to find a word and replace it with another word using Bash. I am using sed to do this and please note that the word that I am looking for is an output from a command. So I am trying to find a word, which is the output of a command, and replace it with another word and override the previous word.
This is my code:
File=file.txt
File2=file2.txt
min=$(cat $File2 | grep word);
sed -i 's/$min/max/g' $File
It's not producing any error, but I am unable to find the word in order to replace it. When I manually type the word rather than using the variable "$min" it works just fine. So when I do this, it works:
sed -i 's/min/max/g' $File
but when I do this, it doesn't:
sed -i 's/$min/max/g' $File
I am thinking maybe sed doesn't accept variables as a search string. Any idea how I can achieve this?
thank you.
Use double quotes for the sed expression, this should work:
sed -i "s/$min/max/g" $File

Print new line with sed using variable [duplicate]

This question already has an answer here:
SED command not being run from bash script
(1 answer)
Closed 8 years ago.
I'm trying to print a new line after 'string' using the variable THIS into file.
sed -i '' 'string/a\
${THIS}
' "${f}"
It prints "${THIS}" into file, literally. I've tried using double quotes for sed but that leads to error message:
"sed: 1: "...": command a expects \ followed by text"
I've also tried to Google this and have been browsing this excellent forum but couldn't find anything.
Any idea, please? Thank you!
It should look like this:
THIS="foo"
sed 's/string/string\n'"${THIS}"'/g' file.txt
Btw, if you are unsure I encourage you to be careful using the -i option. I would play with the sed command until I'm sure and only then use -i (which will overwrite the original file)
If you are replacing a literal string (no regex), the replace command might be better than sed here:
replace string "string"$'\n'"${THIS}" file.txt

Resources