| Perl One liners |
Perl one liners that can come in handyPerl 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. Text File Search and Replace: # Find all comma's "," and replace them with nothing "". Text File Information and Cleanup Operations: # Count the number of lines in a text file. References: |