使用grep只能获得12个字母的字母

时间:2016-09-08 20:24:35

标签: linux unix grep

使用grep

在testing.txt中有多少12个字母 - 仅字母表的行?

test.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

从这段摘录中,我想得到2的结果。(helloworldad,timetolearnz)

我想检查每一行,并且只查看每行中有12个字符的那些行。我想不出用grep做这个的方法。

仅限字母表,我想我可以使用

grep [A-Za-z] testing.txt

但是,如何才能使这些字符[A-Za-z]出现在这12个字符中?

2 个答案:

答案 0 :(得分:4)

您可以使用扩展正则表达式 -E 并指定匹配完全是 {12} 字符从开始 ^ 完成的 $

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

或者,如果您想获得可以使用的行数 -c

$ grep -cE "^[A-Za-z]{12}$" testing.txt
2

答案 1 :(得分:3)

grep支持整行匹配和计数,例如:

grep -xc '[[:alpha:]]\{12\}' testing.txt

输出:

2

[:alpha:]字符类是说[A-Za-z]的另一种方式。有关此主题的更多信息,请参阅信息页面的第3.2节:info grep 'Regular Expressions' 'Character Classes and Bracket Expressions'。或者在the pdf manual online中查找。