Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this regex work in grep but not sed?

Tags:

regex

grep

sed

I have two regular expressions:

  1. $ grep -E '\-\- .*$' *.sql
  2. $ 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)

like image 278
Daniel Avatar asked Feb 26 '21 19:02

Daniel


People also ask

Can you use regex with grep?

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.

Can I use grep and sed together?

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.

What flavor of regex does grep use?

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.

Which option would you choose to force grep to use a basic regular expression Bre?

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.

What are the regular expressions in grep?

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 #. ...

Can I use regular expressions in Linux?

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.

How to grep only the matching string after prefix?

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

What does grep look for in a string?

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”.


Video Answer


3 Answers

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
like image 81
anubhava Avatar answered Oct 17 '22 21:10

anubhava


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.

like image 30
tink Avatar answered Oct 17 '22 21:10

tink


You are trying to use sed's \cregexpc syntax where with \-<...> you are telling sed the delimiter character 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
like image 2
αғsнιη Avatar answered Oct 17 '22 22:10

αғsнιη