3

I'm on Ubuntu 16.04

Trying: grep '.*' file1

Output: file nu-mber o-ne second string

Trying: grep '.+' file1

Output is absent

Why plus is not working?

heroys6
  • 133

3 Answers3

7

You need to tell grep you're using an extended regular expression:

grep -E '.+' file1

The standard Basic Regular Expression (as used by grep without -E) equivalent of the Extended Regular Expression + operator is \{1,\} though some implementations (like GNU's) also recognise \+ for that as an extension (and you can always use ..*).

(Note that in this particular case grep -E .+ is equivalent to grep -E . as you're looking for substrings matching the regex when not using the -x option. On many systems egrep is provided as an equivalent command to grep -E, but as Graeme points out this is obsolete.)

Stephen Kitt
  • 434,908
  • Thx a lot. After 12 minutes your answer will be the best – heroys6 Oct 14 '16 at 10:14
  • 3
    Note that egrep is considered to be obsolete, so grep -E is preferred - http://pubs.opengroup.org/onlinepubs/7908799/xcu/egrep.html. Not that there is any sign of it disappearing. – Graeme Oct 14 '16 at 11:13
2

With GNU grep (default on Ubuntu) you can also enable extended behavior with a backslash. Eg:

grep '.\+' file1
Graeme
  • 34,027
1

I believe + is an extended regular expression metacharacter. Try using egrep.

user628544
  • 1,565