UTC 15:41:29

April 2018
Calendar Tool


One Line of Program at Command Prompt

To execute one line of code on a file and print the results to standard output:
perl -pe ' your single line of code here ' filename.txt
-e tells perl to execute code between quotes.
-p prints the results to your screen

perl -pe 's/search/replace/g' filename.txt

To execute one line of code on a file, write results to that file and keep a backup copy of the original:
perl -p -i~ -e '...your code...' file.txt
The -i tells perl to keep a backup of the original. The ~ is what's appended to the backup file name.

To extract what's inside single quotes using regex - where your regex is enclosed in single quotes:
In a text file you have:
   197  '' - passed

perl -e 's/.*'\''(.*)'\''.*/$1/' -p file1.txt
So you get the first single quote by actually ending the single quoted string, then escape the single quote you wish to insert, then start single quoted string again and continue.
A simpler example:
To print: My O'Reilly Books
In perl it's: print "My O'Reilly Books\n";
In bash shell:
perl -e 'print "My O'\''Reilly Books\n";'
In this example, right after the O, you actually end the single quoted string, then insert your escaped single quote, then start the single quoted string again. Note that those are two single quotes between the backslash and Reilly.

debugging CGI Scripts and passing parameters on your console:
In your browser you have:

on your console you would:
perl -d myScript.cgi
then paste your parameters starting with the question mark:
press enter, then press [ctrl] + d to exit and continue with script

pipe output to perl
echo "hello" | perl -e 's/\w+/goodbye/' -p