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

This seems to be the expression you seek:


As shown here:

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

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
| improve this answer | |
  • 1
    The version with [:alpha:] works with accented characters (according to the current locale), the one with [A-Za-z] doesn't. FWIW. – Satō Katsura Nov 2 '17 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.