I want to delete all the text between a pair of "};" which contains a particular keyword. What i want is
input:
}; text text KEYWORD text text };
Output:
}; };
Suggest me a simple regular expression. I know 'sed' is to be used.
I want to delete all the text between a pair of "};" which contains a particular keyword. What i want is
input:
}; text text KEYWORD text text };
Output:
}; };
Suggest me a simple regular expression. I know 'sed' is to be used.
\};[^}]*KEYWORD[^}]*\};
will work if there are no }
between the two delimiters.
So:
sed 's/\};[^}]*KEYWORD[^}]*\};/}; };/g' file.in > file.out
Below regex will match the thing that you want to delete -
(?<=\};).*?KEYWORD.*?(?=\};)
Edit: this wont work with sed as pointed out by @Tim as sed does not support lookarounds.
This should work under most conditions:
sed '/};[^}]*};/{s/};[^}]*};/}; };/;b};/};/!b;:a;N;/\n[^}]*};/!ba;s/[^;]*\n.*\n[^}]*/ /' inputfile
There will probably be some corner cases where this fails. Change the space near the end to \n
if you want the result to be on two lines.
Examples:
}; test ;}
becomes }; };
};
becomes
test
};}; };
abc };
becomes
test
}; defabc }; }; def
abc }; 111
becomes
test1
test2
222 }; defabc }; }; def
The simplest approach possible:
cat file.in | sed "/KEYWORD/s/};[^}]*};/}; };/g" > file.out