Reputation: 3910
I need to match string update sims
in .sql
files to prevent any update to table sims
. I have cat test.sql | grep -iE '^\s*UPDATE\s+sims,*\s+'
, which will not wrongly match, say update simsfdafsa
. It will match update sims set
and update sims, table1, table2...
.
But I cannot match any line break char. If someone wrote:
update sims
set dfa=fdag
The grep fails. There's a line break after the sims
above. I tried \n
and \s
, but neither worked. I wonder how to match line break in egrep?
Upvotes: 0
Views: 423
Reputation: 2156
using this as a test file:
cat ~/tmp/test
update sims
set dfa=fdag
update simsafing
update sims, test1
update simss
this regex parses it as you require:
egrep 'update sims$|sims[, ]' ~/tmp/test
update sims
update sims, test1
this page https://www.gnu.org/software/sed/manual/html_node/Regular-Expressions.html explains all the regex patterns that sed knows about (it is basic regex). Because sed is 'greedy' (once you've read that page you'll know what that means) it attempts to match as much of the pattern as possible, so [, ] will match as many spaces or commas in a run as possible.
Upvotes: 1