# This program ask its user to input a sequence, searches a file for #that sequence 
# and writes all lines with that sequence into a separate file

$b = 1;
# The variable $b ($ is used to refer to a variable) is set to one

print "What is your input file name:\n"; 
# Prints a text on the screen and adds a new line (\n)

chomp($infile=<STDIN>);
# Waits for the name of the input file and pics it up from the keyboard input

open IN, $infile or die "No file, no fun!";
# Opens the file to read from and associates it with the name given in the previous line

print "What is your output file name:\n";
# Prints a text on the screen and adds a new line (\n)

chomp($outfile=<STDIN>);
# Waits for the name of the output file and pics it up from the keyboard

print "What are you looking for:\n";
# Prints a text on the screen and adds a new line (\n)

chomp($trag=<STDIN>);
# Waits for the name of the output file and pics it up from the keyboard

open OUT, ">$outfile" or die "No file, no fun!";
# Opens the file to rewrite to (>, >> would be appending to it) and associates it 
# with the name previously supplied by the user

while (<IN>) {
# as long as there is anything in the input file (while <IN>) 
# start doing the following ({)

    if (/$trag/) { 
# if you find the sequence in any line start doing the following ({)

    print OUT "$infile $b: $_"; 
# print to the output file as follows: 
# filename, line number, line with the sequence seached

    } 
# finish the job triggered by the sequence found

    $b++; 
# Increase the line number any time you read a new line (linwise 
# reading of files is a default in the while loop

 } 
# finish the while loop

close (IN) or die "D'oh!";
# Close the input file or close the program if there is no file

close (OUT) or die "Do'h!";
# Close the output file or close the program if there is no file