• Decrease font size
  • Reset font size to default
  • Increase font size

Who's Online

We have 19 guests online

PayPal Donation

Perl One liners

Perl one liners that can come in handy

Perl one liners are super handy for text file manipulation, searching, and tabulating. To get started you must first have perl installed on your system. The perl one liners I have listed below will scan and/or modify a file named "file" by performing a search and replace, which is one of the most common regular expression (regex) statements "s///".

The search and replace regex breaks down as "substitute/what I'm looking for/with this/" a g can be added to the end of the regex statement to search multiple times within the same line of text "s///g". All my statements use -i.bak which will backup the original file to "file.bak", it is very important to always have a backup of the files your using because quite often you might not get the results you want when experimenting with Perl one liners.

Perl one liners are formatted differently for different operating system you might be using on your computer. Windows requires the code to be contained within double quotes " for example: perl -i.bak -pe "s/this/that/g" file. Since Windows uses double quotes " to contain the code if you ever find yourself needing to define text string that are using escape characters or variables they will need to be defined using escape character backslash \ followed by a double quote " for example: perl -e "print \"Hello World\nGoodbye\"". With Linux/Unix systems it is much simpler since they use the single quotes ' for defined the code, so for the same example it would be defined as perl -e 'print "Hello World\nGoodbye"'.

Perl one liners are commonly referred to as "perl pie" because most often the command line arguments -p -i -e are used. So if you are not finding what you need here go and google "perl pie" and you will find a vast amount of examples and resources on Perl one liners.

For those Microsoft Windows users who are a little weary about trying to install Perl and using the command line there is a very powerful notepad tool which I find my self using quite often called Notepad++. It is an amazing piece of open source software which has a very powerful regex search and search/replace feature. Notepad++ can also be extended with Plugins one very handy plugin I find myself using for modifying binary files is called Hex-Editor.

Note: I've found there appears to be issues running Perl pie within Windows PowerShell, so I would recommend using it under Windows CMD prompt instead.

Text File Search and Replace:

# Find all comma's "," and replace them with nothing "".
perl -i.bak -pe "s/,//g" file

# Find a word and change it from lowercase to uppercase
# For example: "1: this_is_my_word = 2333"
perl -i.bak -pe "s/ ([\w+_+]+?) =/ \U\1\E =/" file

# Find a word and change it from uppercase to lowercase
# For example: "1: THIS_IS_MY_WORD = 2333"
perl -i.bak -pe "s/ ([\w+_+]+?) =/ \L\1\E =/" file

# Using Linux change format of file from DOS to Linux.
perl -i.bak -pe 's/\r\n$/\n/g' file

# Using Linux change the format of a file from Linux to DOS.
perl -i.bak -pe 's/\n$/\r\n/g' file

# Using Windows change the format of file from DOS to Linux.
perl -i.bak -pe "binmode ARGVOUT if $.==1; s/\r\n$/\n/;" file

# Using Windows change the format of file from Linux to DOS.
perl -i.bak -pe "binmode ARGVOUT if $.==1; s/\n$/\r\n/;" file

# Change Tabs to 4 Spaces.
perl -i.bak -pe "s/\t/ /g" file

# Change 4 Spaces to Tab.
perl -i.bak -pe "s/ {4}/\t/g" file

# Split up a single line into multiple lines. This example
# takes "FFB14FFD0EFFC16FFB3E" and adds a newline after
# ever two characters.
perl -i.bak -pe "s/(.{2})/\1\n/g" file

Text File Information and Cleanup Operations:

# Count the number of lines in a text file.
perl -pe "}{$_=$." file
# -or- (a more readable version)
perl -ne "END{print $.}" file

# Count occurrences in a string. This example would count
# How many '-' are in each line of a text file
perl -ne "print'Line '.$..':'.$s.\"\n\"if$s=s/-//g" file
# Insert Line number at the beginning of every line.
perl -i.bak -pe "s/^/$.: /" file

# Insert line numbers with a constant width example: "0001: ".
perl -i.bak -pe "$l=sprintf('%04d: ',$.);s/^/$l/" file

# Remove Lines containing ONLY white space and empty lines.
perl -i.bak -ne "print unless m/^\s*$/;" file

# Swap words,numbers, or, characters around in a file.
# This changes a date ex: "06-20-2012" to "2012-06-20".
perl -i.bak -pe "s/(\d\d)-(\d\d)-(\d\d\d\d)/\3-\1-\2/g" file

# Take an average of a recurring time measurement throughout
# a text file. For example: Time=50.366 seconds
perl -ne "END{print$s/$i}if(m/Time=(.+) /){$i++;$s+=$1}" file

using Perl pie on multiple files at a time. Note performing operations on multiple files Perl pie does not explicitly close the file handles which causes $. to never reset back to 0, for example if your first file had 10 lines the next file $. would start at 11:

# Adding text at the beginning of every file in the folder
perl -i.bak -pe 'print "text" if $.==1;close ARGV if eof;' *
# Adding text at the begining of every file in the folder
# and all sub-folders within. (Linux Only) perl -i.bak -pe 'print "text" if $.==1;close ARGV if eof;' `find | grep \.txt$`
# Replace "localhost" with "127.0.0.1" in all files that # contain "localhost" in current folder and all sub-folders. # (Linux Only) perl -p -i -e 's/"localhost"/"127.0.0.1"/g' `grep -ril "\"localhost\"" *`

References:

1) perldoc.perl.org: Home->Language reference->perlreref

 
Copyright © 2000 to 2012 Linkapps.com All rights reserved.