1

I have the following sample file and regular expression.

testing.txt

testing                  aa
a bc de
e                        aa
ba                        Z
testing                  bb
testing                  ac

my regular expression using egrep

egrep '[ ]{2,}' testing.txt

The above regular expression attempts to find contiguous white spaces in a line. However, the result returned is empty.

the regulartion expression below works for 1 or more spaces. However that is not what I want.

egrep '[ ]+' testing.txt
Mox
  • 2,355
  • 2
  • 26
  • 42
  • Which version of egrep are you using? `egrep --version` – l'L'l Aug 08 '16 at 06:13
  • Are there spaces at all? Try adding a tab to the bracket expression. – Wiktor Stribiżew Aug 08 '16 at 06:15
  • egrep --version gives me invalid syntax. – Mox Aug 08 '16 at 06:17
  • @WiktorStribiżew, nope that didnt work either(I suppose you mean adding \t into the [ ] brackets) – Mox Aug 08 '16 at 06:18
  • No, you can't add escape sequences in a bracket expression. A literal tab symbol. – Wiktor Stribiżew Aug 08 '16 at 06:21
  • @l'L'l, ok let me try that out. because on the system i can see that the OS is 10 years old ... SunOS 2006 ... jesus – Mox Aug 08 '16 at 06:22
  • @l'L'l, ok it doesn't work either – Mox Aug 08 '16 at 06:25
  • @WiktorStribiżew, i have tried both [ ]\t and [ \t]. – Mox Aug 08 '16 at 06:25
  • 1
    @Mox: Maybe try `[ ][ ]+` or `\s\s+` – l'L'l Aug 08 '16 at 06:26
  • 1
    A backslash inside a bracket expression is treated as a literal backslash. A `[\t]` matches a ``\`` or`t`. Use a literal tab inside the *bracket expression*, it is no character class. Another thing is that in BRE regex you need to escape the braces `\{n,m\}`, you do not need that Iin ERE. No idea what exact flavor egrep supports. – Wiktor Stribiżew Aug 08 '16 at 06:30
  • @l'L'l, wow that actually worked. so am i safe to assume that this OS doesnt suppose {M, N} syntax for regex? =/ – Mox Aug 08 '16 at 06:30
  • @Mox: Yeah, I think that `{n,n}` repetitions for character classes came later on in the egrep world ( or at least after the time of the version you're using ). – l'L'l Aug 08 '16 at 06:33
  • @ILl Limiting quantifier comes with BRE, it is rather old. – Wiktor Stribiżew Aug 08 '16 at 06:34
  • @WiktorStribiżew: Indeed — which is even more perplexing that the OP's version doesn't seem to have it. – l'L'l Aug 08 '16 at 06:40
  • 2
    I invariably end up using Perl on Sun systems - it is FAR more consistent. – Mark Setchell Aug 08 '16 at 07:46
  • `\s` is almost certain not to work; these escapes were introduced in Perl, and were unknown in `grep` variants at the time. – tripleee Aug 08 '16 at 08:56
  • I wonder if `grep '[ ]\{2,\}' testing.txt` works at all. I found some stange help: [*Traditional egrep did not support the `{` metacharacter, and some egrep implementations support `\{` instead, so portable scripts should avoid `{` in egrep patterns and should use `[{]` to match a literal `{.*`*](http://www.manpages.info/sunos/grep.1.html). – Wiktor Stribiżew Aug 08 '16 at 12:05
  • I think this is related: http://stackoverflow.com/questions/5918150/solaris-grep-with-or-functionality – Wiktor Stribiżew Aug 08 '16 at 14:27

1 Answers1

0

If your system is old, this help reference might be describing the issue:

Traditional egrep did not support the { metacharacter, and some egrep implementations support \{ instead, so portable scripts should avoid { in egrep patterns and should use [{] to match a literal {.

That means, that - if grep '[ ]\{2,\}' testing.txt does not work - you are better off using Perl or GNU grep to achieve what you want.

Also, egrep '[ ][ ]+' testing.txt seems to be a workaround only in the current situation, and will not scale, but it certainly will help you for the time being.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    ya unfortunately, it is the company's server haha so i have no power to upgrade ... – Mox Aug 08 '16 at 16:42