mechanize downloaded file, extract perl:
#!/usr/bin/perl
use strict;
use warnings;
use HTML::TokeParser;
# Define the filename containing the list of HTML files
my $filename = "file_names.txt";
my $output_file = "extract.txt";
# Open the file containing the list of HTML files
open my $file_list_fh, '<', $filename or die "Cannot open file $filename: $!";
# Open the output file for writing
open my $output_fh, '>', $output_file or die "Cannot open file $output_file: $!";
# Loop through each line of the file
while (my $html_file = <$file_list_fh>) {
chomp $html_file; # Remove newline character
next if $html_file =~ /^\s*$/; # Skip empty lines
# Read the entire HTML file into a string
open my $html_fh, '<', $html_file or die "Cannot open HTML file $html_file: $!";
my $html_content = do { local $/; <$html_fh> };
close $html_fh;
# Replace <br> tags with semicolons
$html_content =~ s/<br>/;/g;
# Now, process the modified HTML content using HTML::TokeParser and extract table values
my $parser = HTML::TokeParser->new(\$html_content) or die "Cannot create HTML::TokeParser object: $!";
my ($inside_td) = 0;
my ($extracted_content, $old_content) = ("", "");
while (my $token = $parser->get_token) {
if ($token->[0] eq 'S' && $token->[1] eq 'td' && defined $token->[2]{valign} && $token->[2]{valign} eq 'top') {
# Start of a table cell
$inside_td = 1;
} elsif ($token->[0] eq 'E' && $token->[1] eq 'td') {
# End of a table cell
$inside_td = 0;
if ($extracted_content || $old_content) {
print $output_fh "$html_file;$extracted_content;$old_content\n"; # Write the filename, extracted content, and old content to the output file if any exists
($extracted_content, $old_content) = ("", ""); # Reset the extracted content and old content for the next cell
}
} elsif ($token->[0] eq 'T' && $inside_td) {
# Text token inside a table cell
$old_content .= $token->[1]; # Accumulate old content if inside <td>
}
}
}
# Close the files
close $file_list_fh;
close $output_fh;
extract_TEL.pl (removes leading space of the line having TEL:)
#!/usr/bin/perl
use strict;
use warnings;
# Define the input and output filenames
my $input_filename = 'extract.txt';
my $output_filename = 'extract_TEL.txt';
# Open the input file for reading
open(my $fh_in, '<', $input_filename) or die "Could not open file '$input_filename' $!";
# Open the output file for writing
open(my $fh_out, '>', $output_filename) or die "Could not open file '$output_filename' for writing: $!";
# Loop through each line of the input file
while (my $line = <$fh_in>) {
chomp $line; # Remove newline character
# Remove leading spaces from the line if it contains "TEL:"
$line =~ s/^\s+// if $line =~ /^\s*TEL:/;
print $fh_out "$line\n"; # Print the modified line to the output file
}
# Close the file handles
close $fh_in;
close $fh_out;
print "Processing complete. Results saved in $output_filename\n";
extract_TEL_single_line.pl (move TEL: line to above line)
#!/usr/bin/perl
use strict;
use warnings;
# Define the input and output filenames
my $input_filename = 'extract_TEL.txt';
my $output_filename = 'extract_TEL_single_line.txt';
# Open the input file for reading
open(my $fh_in, '<', $input_filename) or die "Could not open file '$input_filename' $!";
# Open the output file for writing
open(my $fh_out, '>', $output_filename) or die "Could not open file '$output_filename' for writing: $!";
my $prev_line = ''; # Initialize a variable to store the previous line
# Loop through each line of the input file
while (my $line = <$fh_in>) {
chomp $line; # Remove newline character
if ($line =~ /^TEL:/) {
# If the line starts with "TEL:", concatenate it with the previous line
$prev_line =~ s/\s+$//; # Remove trailing whitespace from the previous line
$line =~ s/^\s+//; # Remove leading whitespace from the current line
print $fh_out "$prev_line $line\n";
} else {
# If the line does not start with "TEL:", print it as is
print $fh_out "$line\n";
$prev_line = $line; # Update the previous line
}
}
# Close the file handles
close $fh_in;
close $fh_out;
print "Processing complete. Results saved in $output_filename\n";
extract_TEL_single_line_only.pl (only lines having TEL: on it)
#!/usr/bin/perl
use strict;
use warnings;
# Define the input and output filenames
my $input_filename = 'extract_TEL_single_line.txt';
my $output_filename = 'extract_TEL_single_line_only.txt';
# Open the input file for reading
open(my $fh_in, '<', $input_filename) or die "Could not open file '$input_filename' $!";
# Open the output file for writing
open(my $fh_out, '>', $output_filename) or die "Could not open file '$output_filename' for writing: $!";
# Loop through each line of the input file
while (my $line = <$fh_in>) {
chomp $line; # Remove newline character
if ($line =~ /TEL:/) {
# If the line contains "TEL:", print it to the output file
print $fh_out "$line\n";
}
}
# Close the file handles
close $fh_in;
close $fh_out;
print "Processing complete. Lines containing 'TEL:' extracted to $output_filename\n";
extract_TEL_single_line_distinct.pl (distinct lines only)
#!/usr/bin/perl
use strict;
use warnings;
# Define the input and output filenames
my $input_filename = 'extract_TEL_single_line_only.txt';
my $output_filename = 'extract_TEL_single_line_distinct.txt';
# Open the input file for reading
open(my $fh_in, '<', $input_filename) or die "Could not open file '$input_filename' $!";
# Open the output file for writing
open(my $fh_out, '>', $output_filename) or die "Could not open file '$output_filename' for writing: $!";
# Define a hash to store unique lines
my %unique_lines;
# Loop through each line of the input file
while (my $line = <$fh_in>) {
chomp $line; # Remove newline character
if ($line =~ /TEL:/) {
# If the line contains "TEL:", add it to the hash
$unique_lines{$line} = 1;
}
}
# Write the unique lines to the output file
foreach my $line (keys %unique_lines) {
print $fh_out "$line\n";
}
# Close the file handles
close $fh_in;
close $fh_out;
print "Processing complete. Distinct lines containing 'TEL:' extracted to $output_filename\n";
extract_TEL_single_line_distinct_semicolon.pl (replace | with 😉
#!/usr/bin/perl
use strict;
use warnings;
# Define the input and output filenames
my $input_filename = 'extract_TEL_single_line_distinct.txt';
my $output_filename = 'extract_TEL_single_line_distinct_semicolon.txt';
# Open the input file for reading
open(my $fh_in, '<', $input_filename) or die "Could not open file '$input_filename' $!";
# Open the output file for writing
open(my $fh_out, '>', $output_filename) or die "Could not open file '$output_filename' for writing: $!";
# Loop through each line of the input file
while (my $line = <$fh_in>) {
chomp $line; # Remove newline character
# Replace '|' with ';'
$line =~ s/\|/;/g;
# Write the modified line to the output file
print $fh_out "$line\n";
}
# Close the file handles
close $fh_in;
close $fh_out;
print "Processing complete. '|' replaced with ';' in $output_filename\n";