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

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

Related

How can I capitalize a single letter after a certain character? [duplicate]

This question already has answers here:
Using Sed to capitalize the first letter of each word
(4 answers)
Closed 1 year ago.
I have some strings
some-string
some-other-string
yet-another-string-to-handle
I want to convert those strings into
someString
someOtherString
yetAnotherStringToHandle
I'm trying to do the following
echo yet-another-string-to-handle | sed -r 's/\-(.*)/\U\1\E/g'
But that results in
yetANOTHER-STRING-TO-HANDLE
Needless to say, I'm a bit lost. Any suggestions on how I can achieve my goal?
With GNU sed:
sed -E 's/-(.)/\u\1/g' file
\u: Turn the next character to uppercase (GNU 'sed' extension).
Output:
someString
someOtherString
yetAnotherStringToHandle
See: info sed

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).

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

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 $!()

How to use sed to change references to directories? [duplicate]

This question already has answers here:
sed search and replace strings containing / [duplicate]
(2 answers)
Closed 8 years ago.
So I know how to use sed -i 's/string/new-string/g' *.* but I'm trying to change all references to a directory structure and I don't know the proper format for the command.
Example: I want to change 'myfolder/mysubfolder' to 'myfolder'.
So if there's a string in a file that says 'myfolder/mysubfolder/file.txt' I want it to say 'myfolder/file.txt'.
You are on right track, just need to escape the /
sed -i 's/myfolder\/mysubfolder/myfolder/g' *.*
sed -i 's#myfolder/mysubfolder#myfolder#g' *.*
I used a delimiter other than / to avoid having to escape the / in the string.

Resources