Reputation: 1472
I have two regular expressions:
$ grep -E '\-\- .*$' *.sql
$ sed -E '\-\- .*$' *.sql
(I am trying to grep lines in sql files that have comments and remove lines in sql files that have comments)
The grep command works using this regex; however, the sed returns the following error:
sed: -e expression #1, char 7: unterminated address regex
What am I doing incorrectly with sed?
(The space after the two hyphens is required for sql comments if you are unfamiliar with MySql comments of this type)
Upvotes: 5
Views: 715
Reputation: 2761
You are trying to use sed's \cregexpc
syntax where with \-<...>
you are telling sed the delimiter c
haracter you want use is a dash -
, but you didn't terminate it where it should be: \-<...>-
also add d
command to delete those lines.
sed '\-\-\-.*$-d' infile
see man sed
about that:
\cregexpc Match lines matching the regular expression regexp. The c may be any character.
if default /
was used this was not required so:
sed '/--.*$/d' infile
or simply:
sed '/^--/d' infile
and more accurately:
sed '/^[[:blank:]]*--/d' infile
Upvotes: 2
Reputation: 784938
You're trying to use:
sed -E '\-\- .*$' *.sql
Here sed
command is not correct because you're not really telling sed
to do something.
It should be:
sed -n '/-- /p' *.sql
and equivalent grep
would be:
grep -- '-- ' *.sql
or even better with a fixed string search:
grep -F -- '-- ' *.sql
Using --
to separate pattern and arguments in grep
command.
There is no need to escape -
in a regex if it is outside bracket expression (or character class) i.e. [...]
.
Based on comments below it seems OP's intent is to remove commented section in all *.sql
files that start with 2 hyphens.
You may use this sed
for that:
sed -i 's/-- .*//g' *.sql
Upvotes: 5
Reputation: 15206
The problem here is not the regex, the problem is that sed
requires a command. The equivalent of your grep
would be:
sed -n '/\-\- .*$/p'
You suppress output for non-matching lines -n
... you search (wrap your regex in slashes) and you print p
(after the last slash).
P.S.: As Anub pointed out, escaping the hyphens -
inside the regex is unnecessary.
Upvotes: 3