正则表达式,适用于egrep

时间:2016-09-12 21:42:05

标签: regex shell git-bash

需要打印只包含一次笑声的行,其中笑声被定义为Hahahahahahahahahahahahahahaha形式的字符串!,任意多ha。

我拥有的是

egrep "^Ha.*[ha*][!$]" myfile.txt

并打印

Hahahahahahahahahahahahahaha!
Hahahahahahahahahahahahahaha! nklddln

和myfile.txt包含

kaka
linux.student.cs.uwaterloo.ca
So How is going on Man. 
I am now just trying to test the stuff with Ubuntu
Digit in the line 1
Regular<title> expression</title> stuff must be working
alright just testing linux.studEnt.cs.Uwaterloo.ca
liNUx.student.cs.uwaterlOO.Ca so the things
We need to</title> have more thn ten<title> lines that have more tha twenty characters
So, the assignment needs to be done very quickly
This cs247 line contains the course code cs246
The course code cs246 is in the cs247 line
All these lines in this text file are for testing only
We need to<title></title> thouroughly check the cs246 assignment
heheheheehe man this work will take some time
you have to be quick as we need to get it done before deadline
Okey kaka g whats the situation
Man How are you?
course code is CS246
course CS246
Hahahahahahahahahahahahahaha!
ghjf akf Hahahahahahahahahahahahahaha!
dhgD Hahahahahahahahahahahahahaha! jwef
Hahahahahahahahahahahahahaha! nklddln
hufwf Hahahahahahahahahahahahahaha!
course cs246
12345678901234567890
1234567890123456789

我不希望打印第二行,因为它包含一个额外的单词。必须使用egrep。

1 个答案:

答案 0 :(得分:2)

您需要在$之后使用锚点!进行精确匹配:

egrep '^Ha(ha)+!$' file

Hahahahahahahahahahahahahaha!
相关问题