Quick Reference- Perl one liners

#search for a pattern in a file just like Linux grep command
$perl -ne ‘print if /findword/;’

#search and replace a pattern in a file and print the modified contents on the screen
$perl -pe ‘s/findword/replaceword/g;’

#search and replace a pattern in a file. Original files are backed up in .bak extension. This is one of the most powerful command if you want to do operations on large set of files in a directory.
$perl -pi.bak -e ‘s/findword/replaceword/’

#see the encode and decode characters of URLs
$perl -MURI::Escape -le ‘$var=qq(userid,;); print uri_escape($var);’
$perl -MURI::Escape -le ‘$var=qq(userid%2C%3B); print uri_unescape($var);’

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s