Pitr
Pitr

Reputation: 51

Grep only for lowercase and spaces

I need to grep files for lines containing only lowercase letters and spaces. Both conditions must be met at least once and no other characters are allowed.

I know how to grep only for lowercase or only for space but I don't know how to join those two conditions in one regexp/command.

I have only this right now:

egrep "[[:space:]]" $DIR/$file | egrep -vq "[[:upper:]]"

which of course will display lines with digits and/or special characters as well which is not what I want. Thanks.

Upvotes: 1

Views: 1883

Answers (3)

fedorqui
fedorqui

Reputation: 290035

awk may be better to express such conditions:

awk '/^[ a-z]+$/ && /[a-z]/ && / /' file

That is, it checks that a line:

  • consists in just spaces and lowercase letters.
  • it contains at least a lowercase.
  • it contains at least a space.

Test

$ cat a
hello this is something simple
but SUDDENLY not
wah
wa ah
$ awk '/^[ a-z]+$/ && /[a-z]/ && / /' a
hello this is something simple
wa ah

Upvotes: 2

Corubba
Corubba

Reputation: 2243

First grep all lines that only consist of lowercase characters and whitespace, and then all those that contain at least one whitespace.

egrep -x '[[:lower:][:space:]]+' "$DIR/$file" | egrep '[[:space:]]+'   

The [:space:] meta class also matches for tabs, and can be replaced with a plain space if desired.

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 60017

This is what you require

The -x matches whole lines

The first expression matches lines composed entirely of spaces and lower case letters.

The second expression matches lines that have both a space and a lower case letter.

egrep -x '[[:lower:] ]*' $DIR/$file | egrep '( [[:lower:]])|([[:lower:]] )'

Upvotes: 2

Related Questions