Example:
This (word1) is a test (word2) file.
What I want:
This is a test file.
The problem is that the brackets occur more than once, so if I use:
sed 's/<.*>//g'
I get This file
which it's wrong.
How about if I want to replace the string between two same patterns?
Like:
WORD1 %WORD2% WORD3 => WORD1 WORD3
All you need is a negated character class
[^<>]*
that will match any character but a<
or>
:Or, if you have round brackets you can use
[^()]*
(note that in BRE syntax, to match a literal(
or)
escaping\
is not necessary):See IDEONE demo
As for the update, you can remove everything from
WORD1
tillWORD3
using.*
, but only if there is only one set ofWORD1
andWORD3
(demo):With sed, it is not possible to use lookarounds (lookaheads here), nor lazy quantifiers to restrict the match to the leftmost
WORD3
occurrences. And if you know for sure there is no%
symbol in between, you can still use the negated character class approach (demo):A generic solution is to do it in several steps:
<UC>
) (I am using Russian letters, but it should be some control character)<UC1>[^<UC1><UC2>]*<UC2>
to replace with the necessary replacement stringHere is an example:
I am hardcoding a space, but it can be adjusted whenever necessary.