Thursday, March 24, 2011

Perl: search for whatever damn keywords in a file with four easy lines.

while (<>) {
chomp;
if ($_ eq "then") {
print "\n$_\n";
}}


To use it just open the folder holding the files you want to search through, and place the perl program in the same damn folder. Now open a konsole pointing at the same folder and type:

 ./myperlprogram name-of-file-you-want-to-use-it-on

So for me I had to type:

./f.pl names

f.pl is the name of the perl program. names is the file.








Lets look at the code again:

while (<>) {
chomp;
if ($_ eq "then") {
print "\n$_\n";
}}


See the if statement?

if ($_ eq "then") { 

This is the part you want to change to make the program search for whatever damn keyword you want.  I was searching the string "then".

(Don't ask me why I'm searching for the word "then". I wouldn't fucking tell you anyway.)

Ok, say you had a file with numbers in it and you wanted the program to return all the numbers greater than 5. You would change the if statement to this:

new statement: if ($_ > 5) {

old statement:  if ($_ eq "then") {


See the difference?

That's all. See what kind of crazy hacks you can do by just changing the if statement.

No comments:

Post a Comment