Regex Reminder to Myself for a Common Grep Task

by Chris Abraham on 02/02/2006 ·

If you need to grep from a search string to the end of line, you can do that with “.*$”


So, lets say you have this text:

I love dogs
Eat your vegetables
I love cats
Eat small portions
I love canaries
Eat chicken thoroughly cooked
I love frogs

If you would like to use TextPad, for example, to delete all the lines that say “I love…” you could check “Regular expression” and then put “I love.*$” into Find What and nothing in the Replace with. Of course, that would leave the line blank. If you wanted to be even more precise, you could write “^I love.*$”

The “^” means “beginning of line” to regex, the $ means “end of line” to regex, and “.” refers to one character and “*” is called a wildcard and means everything from that character until the end of line, $.

^I love.*$

If you want to kill the line you could put “I love.*$” in there and that would kill the carriage return instead of leaving you a blank line. “” is regular expression for “newline” — a carriage return has a character, “,” as does a tab, “\t,” for example.

^I love.*$

If you wanted to delete all the lines that began with “Eat” then you would use:

^Eat.*$

That is, of course, only if you have the freedom to do search and replace using regular expressions, that is. If you want it, I recommend TextPad.

So, there are loads of times when I need to redact a whole lot of code, be it XML or HTML or just garbaged-up plaintext.

Share on Tumblr

Facebook comments:

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution.

Previous post:

Next post: