Say my search term is CAT, replace term is DOG I only want to match things that begin with whitespace before CAT, regardless of how much whitespace. Also, is there any way I can preserve the correct amount of whitespace?
so
else CAT {
would stay
else CAT {
but
CAT {
would become
DOG {
and
CAT {
would become
DOG {
I am trying sed -i ' ' -E "s/^[^a-z]*CAT/DOG/g" $file
so that it doesn't match anything that has letters anywhere between the beginning of the line, and the search term
And other variations, but haven't found anything that works correctly. I can't do sed -i ' ' -E "s/^[ \t]*CAT/DOG/g" $file
because \t
doesn't work on OSX
---- my file:
method()
{
if ((self = [super init])) {
hello1
}
else if ((self = [super init])) {
hello2
}
}
---- using sed -i '' "s/^\([[:blank:]]*\)$s/\1$r/" $file
Turns it into:
method()
{
self = [super init];
if (self) {
hello1
}
if () {
hello2
}
}
which is bad, because I don't want the second else if to be affected at all. The first one is good though.