Suppose I have the following text file called inputFile:

   Hello World
abc 123
1 3 5 a b

and I want to print out the lines containing only one word(any sequence with uppercase/lower case letters), regardless if there are spaces before or after the character, using egrep and regex.

so the output should look like this:


The best regex I came up was

egrep ' *[a-zA-Z] *$' inputFile

but it does't seem to work. Any suggestions?

  • How does it "not seem to work"? You can edit your post with more details. Dec 6, 2015 at 7:44

2 Answers 2


You need a + after the [A-Za-z] so that the regex matches 1-or-more, instead of just a single letter. You also need to anchor the start of the regex with a ^ (which matches the start of the line, opposite to the $ matching the end of the line)

egrep '^ *[a-zA-Z]+ *$' inputFile

You might also want to use [[:space:]]* rather than just *, so that tabs are matched as well as just spaces. And [[:alpha:]] matches all alpha characters from your locale, not just A-Z.

egrep '^[[:space:]]*[[:alpha:]]+[[:space:]]*$' inputFile

You're off to a good start.  You're looking for a sequence of one or more letters, with no other non-blank characters on the same line (before or after).  Using the + notation, you can represent a string of one or more letters, as cas says, with [a-zA-Z]+.

You get the rest of the requirement — no other non-blank characters on the same line (before or after) — by doing an anchored search (anchored to the beginning and end of the line, so it examines the entire line) for a sequence (possibly null) of blanks, followed by a non-null sequence of letters, followed by another sequence (possibly null) of blanks:

egrep '^ *[a-zA-Z]+ *$'

If you want to include letters from outside the standard Latin alphabet (e.g., à, é, ï, and ô), and allow whitespace characters other than space (e.g., Tab), use the character class codes:

egrep '^[[:space:]]*[[:alpha:]]+[[:space:]]*$'

You must log in to answer this question.

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