0

I’m trying to use grep to find lines that start with duplicate alpha characters (a-zA-z). What would be the proper grep command to accomplish this?

This is what I have so far:

grep -E '[:alpha:]' $lines1

1 Answer 1

4

This seems to be the expression you seek:

^([A-Za-z])\1

As shown here:

$ grep -E '^([A-Za-z])\1' input
aardvark
$ grep -E '^([[:alpha:]])\1' input
aardvark
$ cat input
apple
aardvark
bookkeeper

To expand on the expression:

 NODE                     EXPLANATION
 ^                        the beginning of the string
 (                        group and capture to \1:
   [A-Za-z]                 any character of: 'A' to 'Z', 'a' to 'z'
 )                        end of \1
 \1                       what was matched by capture \1
1
  • 1
    The version with [:alpha:] works with accented characters (according to the current locale), the one with [A-Za-z] doesn't. FWIW. Nov 2, 2017 at 19:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .