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)
GNU grep supports three regular expression syntaxes, Basic, Extended, and Perl-compatible. In its simplest form, when no regular expression type is given, grep interpret search patterns as basic regular expressions. To interpret the pattern as an extended regular expression, use the -E ( or --extended-regexp ) option.
This collection of sed and grep use cases might help you better understand how these commands can be used in Linux. Tools like sed (stream editor) and grep (global regular expression print) are powerful ways to save time and make your work faster.
Grep is an implementation of POSIX regular expressions. There are two types of posix regular expressions -- basic regular expressions and extended regular expressions. In grep, generally you use the -E option to allow extended regular expressions.
Basic and extended regular expressions are two variations on the syntax of the specified pattern. Basic Regular Expression (BRE) syntax is the default in sed (and similarly in grep ). Use the POSIX-specified -E option ( -r , --regexp-extended ) to enable Extended Regular Expression (ERE) syntax.
Regular Expressions in Grep (Regex) 1 Grep Regular Expression #. A regular expression or regex is a pattern that matches a set of strings. ... 2 Literal Matches #. The most basic usage of the grep command is to search for a literal character or series of characters in a file. 3 Anchoring #. ... 4 Bracket Expressions #. ...
Regular expressions (Regex) are widely used in the Linux command line. Many common commands support Regex, such as grep, sed, and awk. Some of us may have encountered a case where a particular Regex doesn’t work with Linux commands – for instance, a pattern containing \d – however, the same Regex works well with Java or Python.
What you have (look-aheads) are available only in the PCRE regex flavor which is supported only in GNU grep with its -P flag. Assuming you need to extract only the matching string after prefix you need to add an extra flag -o to let know grep that print only the matching portion as
It is important to note that grep looks for the search pattern as a string, not a word. So if you were searching for “gnu”, grep will also print the lines where “gnu” is embedded in larger words, such as “cygnus” or “magnum”.
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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With