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

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 /

Related

Sed: unescaped newline inside substitute pattern

I want to replace '-Djava.library.path=""' with '-Djava.library.path="path"' in file named "file" using sed
sed -i 's/-Djava.library.path=""/-Djava.library.path="path"/g' -f file
returns
sed: 2: file: unescaped newline inside substitute pattern
What's wrong? Can someone help?
Changed to
sed -i 's/Djava.library.path=""/Djava.library.path="path"/' file
Now it works.

remove everything between two characters with 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).

sed: remove matching line and everything after

I'm trying to remove all lines after the first blank line in a file with a git filter using sed.
This seems to remove everything after the blank line
sed -i '/^$/q' test.rpt
How do I also include the blank line itself to be deleted?
If this is GNU sed, just use Q instead of q.
sed -i '/^$/Q' test.rpt
For BSD sed, use -n switch to suppress automatic printing, and print lines manually. E.g:
sed -n -i '/^$/q;p' test.rpt
PS: You might want to change the regex to ^[[:blank:]]*$ to regard lines of all blank characters as blank lines as well.
Try this:-
sed -i '/^$/,$ d' inputfile

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'

remove trailing spaces in a file only from non empty lines

I know how to remove all trailing spaces from a file, e.g :
sed -i 's/ *$//' file
Is there a way to do it, but not in lines containing only spaces?
Something in the spirit of :
sed -i 's/[a-zA-Z0-9;}{] *$/[a-zA-Z0-9;}{]/' file
^ keep the original characters
Preferably, but not necessariliy, with sed.
Any linux supported solution will do.
Thanks
Just make sure some other character appears before:
sed -r 's/([^\s])\s+$/\1/' file
This checks if a non-space character (\s) appears followed by any amount of spaces. If so, just print this non-space character back, so that the trailing spaces are removed.
Test
Using cat -vet to see the markers:
$ cat -vet a
hello $
$
bye $
$ sed -r 's/([^\s])\s+$/\1/' a | cat -vet -
hello$
$
bye$