Regex Reminder to Myself for a Common Grep Task

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.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • NewsVine
  • Reddit
  • Slashdot
  • TailRank
  • Technorati
  • YahooMyWeb
  • TwitThis