我在文件中有以下数据
Muhammad,Ali House 12 Mazhar Colony Rawalpindi 0333-5438855
Bushra,Qadir Gulberg Society Lahore 0234-5321998
Muhammad,Imtiaz Sector E-11/4 Islamabad 0331-4320988
Ehtasham Ul,Haq Chinar House Islamabad 0432-2294712
Summaya,Mumtaz NUST Hostel-3 Islamabad 0321-7659832
Junaid,Riaz Sadar Road Rawalpindi 0331-5332222
我需要使用grep
搜索姓氏正好为5个字符且名字长度为3或4个字符的行。我尝试了很多不同的代码,但运气不利于我。关于这个的任何想法?
答案 0 :(得分:6)
你可以尝试以下方面:
grep -E '^\w{5},\w{3,4}\s'
文件内容:
Muhammad,Ali House 12 Mazhar Colony Rawalpindi 0333-5438855
Bushr,Qadi Gulberg Society Lahore 0234-5321998
Muham,Imt Sector E-11/4 Islamabad 0331-4320988
Ehtasham Ul,Haq Chinar House Islamabad 0432-2294712
Summaya,Mumtaz NUST Hostel-3 Islamabad 0321-7659832
Junaid,Riaz Sadar Road Rawalpindi 0331-5332222
输出:
Bushr,Qadi Gulberg Society Lahore 0234-5321998
Muham,Imt Sector E-11/4 Islamabad 0331-4320988
答案 1 :(得分:0)
运气与它无关。您正在搜索五个字母,后跟一个逗号,后跟三个或四个字母,后跟一个空格。我尝试了许多不同的代码,但运气不利于我。
^ is the beginning of a line
\w is "a letter"
{n} means "this many repetitions"
\s is "a whitespace" (space, tab, nonbreaking space etc.)
你只需将它们放在一起并获得正则表达式:
^\w{5},\w{3,4}\s