I’m trying to use sed to get some characters between an opening and a closing character. These opening and closing characters could be braces e.g. { and }, etc. In my specific case, the opening character is = and the closing character is the end of the line.
In PHP, I had a regex that usually worked for me: ([^\^]*?). However, being that sed uses the POSIX-BRE flavour, I do not believe this will work.
The string I’m using the regex on could be something like this: password=mailPASSWORD. mailPASSWORD could contain regular alphabet characters as well as special characters.
So I want to replace mailPASSWORD and put my own password.
sed -i -r "s/password[ ]*=[ ]*([pattern_goes_here])/password=mypassword/" /myfile
I’d appreciate some assistance.
Thanks in advance
EDIT
After looking at other alternatives to sed, I found out that perl is actually a much better tool for this sort of thing. And being that my Regular Expression knowledge is pretty much perl-based it looks like the better way to go.
Here’s how I would solve the same problem:
perl -p -i -e "s/password *= *[^\n]*/password=mypassword/" /myfile
Since I’m doing stuff line by line it just make things a lot easier to script.
For your specific case:
For the more general case, if the end delimiter is a character, let’s say
x, you simulate this:with this:
For multi-character end-delimiters it gets more complicated, but that’s the basic idea: instead of non-greedy matching, create a pattern that matches everything but the end-delimiter.