Command line replace with perl

I often run into issues on the command line where I’d like to perform a non-greedy search and replace. This is not possible with sed, grep, or egrep, AFAIK, so I must resort to perl. Here is how it’s done:

perl -pe 's|"http://(.*?)/.*$|$1|g' 

The above example will take a list of requested URIs or search domains from an apache log and print out only the domain. I’m using pipes instead of slashes in the regex to eliminate the need to escape the slashes.

Note the question mark used to make a non-greedy match.

Leave a Reply

Your email address will not be published. Required fields are marked *