ellsusan ellsusan - 2 months ago 14
Linux Question

Using grep to get 12 letter alphabet only lines

Using grep

How many 12 letter - alphabet only lines are in testing.txt?

excerpt of testing.txt

tyler1
Tanktop_Paedo
xyz2@geocities.com
milt@uole.com
justincrump
cranges10
namer@uole.com
soulfunkbrotha
timetolearnz
hotbooby@geocities.com
Fire_Crazy
helloworldad
dingbat@geocities.com


from this excerpt, I want to get a result of 2. (helloworldad, and timetolearnz)

I want to check every line and grep only those that have 12 characters in each line. I can't think of a way to do this with grep though.

For the alphabet only, I think I can use

grep [A-Za-z] testing.txt


However, how do I make it so only the characters [A-Za-z] show up in those 12 characters?

Answer

You can do it with extended regex -E and by specifying that the match is exactly {12} characters from start ^ to finish $

grep -E "^[A-Za-z]{12}$" testing.txt
timetolearnz
helloworldad

Or if you want to get the count of lines you can use

grep -E "^[A-Za-z]{12}$" testing.txt | wc -l
2
Comments