Mani G Mani G - 2 months ago 6
Bash Question

Regular expression which is suitable for use with egrep

Need to print Lines that contain nothing but a single occurrence of laughter, where laughter is de fined as a string of the form Hahahahahahahahahahahahahaha!, with arbitrarily many ha's.

What I have is

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


and it prints

Hahahahahahahahahahahahahaha!
Hahahahahahahahahahahahahaha! nklddln


and myfile.txt contains

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


I do not want the second line to be printed as it contains an extra word. Use of egrep is must.

Answer

You need to use anchor $ after ! for precise matching:

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

Hahahahahahahahahahahahahaha!
Comments