{"id":3595,"date":"2024-02-10T19:49:31","date_gmt":"2024-02-11T04:49:31","guid":{"rendered":"https:\/\/crm270s.abramjmeister.com\/?p=3595"},"modified":"2024-02-10T19:49:31","modified_gmt":"2024-02-11T04:49:31","slug":"extract-pl","status":"publish","type":"post","link":"https:\/\/crm270s.mycpamytax.com\/?p=3595","title":{"rendered":"extract.pl"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">mechanize downloaded file, extract perl:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/perl\nuse strict;\nuse warnings;\nuse HTML::TokeParser;\n\n# Define the filename containing the list of HTML files\nmy $filename = \"file_names.txt\";\nmy $output_file = \"extract.txt\";\n\n# Open the file containing the list of HTML files\nopen my $file_list_fh, '&lt;', $filename or die \"Cannot open file $filename: $!\";\n# Open the output file for writing\nopen my $output_fh, '&gt;', $output_file or die \"Cannot open file $output_file: $!\";\n\n# Loop through each line of the file\nwhile (my $html_file = &lt;$file_list_fh&gt;) {\n    chomp $html_file; # Remove newline character\n    next if $html_file =~ \/^\\s*$\/; # Skip empty lines\n\n    # Read the entire HTML file into a string\n    open my $html_fh, '&lt;', $html_file or die \"Cannot open HTML file $html_file: $!\";\n    my $html_content = do { local $\/; &lt;$html_fh&gt; };\n    close $html_fh;\n\n    # Replace &lt;br&gt; tags with semicolons\n    $html_content =~ s\/&lt;br&gt;\/;\/g;\n\n    # Now, process the modified HTML content using HTML::TokeParser and extract table values\n    my $parser = HTML::TokeParser-&gt;new(\\$html_content) or die \"Cannot create HTML::TokeParser object: $!\";\n    my ($inside_td) = 0;\n    my ($extracted_content, $old_content) = (\"\", \"\");\n\n    while (my $token = $parser-&gt;get_token) {\n        if ($token-&gt;&#91;0] eq 'S' &amp;&amp; $token-&gt;&#91;1] eq 'td' &amp;&amp; defined $token-&gt;&#91;2]{valign} &amp;&amp; $token-&gt;&#91;2]{valign} eq 'top') {\n            # Start of a table cell\n            $inside_td = 1;\n        } elsif ($token-&gt;&#91;0] eq 'E' &amp;&amp; $token-&gt;&#91;1] eq 'td') {\n            # End of a table cell\n            $inside_td = 0;\n            if ($extracted_content || $old_content) {\n                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\n                ($extracted_content, $old_content) = (\"\", \"\"); # Reset the extracted content and old content for the next cell\n            }\n        } elsif ($token-&gt;&#91;0] eq 'T' &amp;&amp; $inside_td) {\n            # Text token inside a table cell\n            $old_content .= $token-&gt;&#91;1]; # Accumulate old content if inside &lt;td&gt;\n        }\n    }\n}\n\n# Close the files\nclose $file_list_fh;\nclose $output_fh;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>extract_TEL.pl (removes leading space of the line having TEL:)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/perl\nuse strict;\nuse warnings;\n\n# Define the input and output filenames\nmy $input_filename = 'extract.txt';\nmy $output_filename = 'extract_TEL.txt';\n\n# Open the input file for reading\nopen(my $fh_in, '&lt;', $input_filename) or die \"Could not open file '$input_filename' $!\";\n# Open the output file for writing\nopen(my $fh_out, '&gt;', $output_filename) or die \"Could not open file '$output_filename' for writing: $!\";\n\n# Loop through each line of the input file\nwhile (my $line = &lt;$fh_in&gt;) {\n    chomp $line;  # Remove newline character\n\n    # Remove leading spaces from the line if it contains \"TEL:\"\n    $line =~ s\/^\\s+\/\/ if $line =~ \/^\\s*TEL:\/;\n\n    print $fh_out \"$line\\n\";  # Print the modified line to the output file\n}\n\n# Close the file handles\nclose $fh_in;\nclose $fh_out;\n\nprint \"Processing complete. Results saved in $output_filename\\n\";\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>extract_TEL_single_line.pl (move TEL: line to above line)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/perl\nuse strict;\nuse warnings;\n\n# Define the input and output filenames\nmy $input_filename = 'extract_TEL.txt';\nmy $output_filename = 'extract_TEL_single_line.txt';\n\n# Open the input file for reading\nopen(my $fh_in, '&lt;', $input_filename) or die \"Could not open file '$input_filename' $!\";\n# Open the output file for writing\nopen(my $fh_out, '&gt;', $output_filename) or die \"Could not open file '$output_filename' for writing: $!\";\n\nmy $prev_line = '';  # Initialize a variable to store the previous line\n\n# Loop through each line of the input file\nwhile (my $line = &lt;$fh_in&gt;) {\n    chomp $line;  # Remove newline character\n\n    if ($line =~ \/^TEL:\/) {\n        # If the line starts with \"TEL:\", concatenate it with the previous line\n        $prev_line =~ s\/\\s+$\/\/;  # Remove trailing whitespace from the previous line\n        $line =~ s\/^\\s+\/\/;  # Remove leading whitespace from the current line\n        print $fh_out \"$prev_line $line\\n\";\n    } else {\n        # If the line does not start with \"TEL:\", print it as is\n        print $fh_out \"$line\\n\";\n        $prev_line = $line;  # Update the previous line\n    }\n}\n\n# Close the file handles\nclose $fh_in;\nclose $fh_out;\n\nprint \"Processing complete. Results saved in $output_filename\\n\";\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>extract_TEL_single_line_only.pl (only lines having TEL: on it)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/perl\nuse strict;\nuse warnings;\n\n# Define the input and output filenames\nmy $input_filename = 'extract_TEL_single_line.txt';\nmy $output_filename = 'extract_TEL_single_line_only.txt';\n\n# Open the input file for reading\nopen(my $fh_in, '&lt;', $input_filename) or die \"Could not open file '$input_filename' $!\";\n# Open the output file for writing\nopen(my $fh_out, '>', $output_filename) or die \"Could not open file '$output_filename' for writing: $!\";\n\n# Loop through each line of the input file\nwhile (my $line = &lt;$fh_in>) {\n    chomp $line;  # Remove newline character\n\n    if ($line =~ \/TEL:\/) {\n        # If the line contains \"TEL:\", print it to the output file\n        print $fh_out \"$line\\n\";\n    }\n}\n\n# Close the file handles\nclose $fh_in;\nclose $fh_out;\n\nprint \"Processing complete. Lines containing 'TEL:' extracted to $output_filename\\n\";\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>extract_TEL_single_line_distinct.pl (distinct lines only)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/perl\nuse strict;\nuse warnings;\n\n# Define the input and output filenames\nmy $input_filename = 'extract_TEL_single_line_only.txt';\nmy $output_filename = 'extract_TEL_single_line_distinct.txt';\n\n# Open the input file for reading\nopen(my $fh_in, '&lt;', $input_filename) or die \"Could not open file '$input_filename' $!\";\n# Open the output file for writing\nopen(my $fh_out, '>', $output_filename) or die \"Could not open file '$output_filename' for writing: $!\";\n\n# Define a hash to store unique lines\nmy %unique_lines;\n\n# Loop through each line of the input file\nwhile (my $line = &lt;$fh_in>) {\n    chomp $line;  # Remove newline character\n\n    if ($line =~ \/TEL:\/) {\n        # If the line contains \"TEL:\", add it to the hash\n        $unique_lines{$line} = 1;\n    }\n}\n\n# Write the unique lines to the output file\nforeach my $line (keys %unique_lines) {\n    print $fh_out \"$line\\n\";\n}\n\n# Close the file handles\nclose $fh_in;\nclose $fh_out;\n\nprint \"Processing complete. Distinct lines containing 'TEL:' extracted to $output_filename\\n\";\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>extract_TEL_single_line_distinct_semicolon.pl (replace | with \ud83d\ude09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/perl\nuse strict;\nuse warnings;\n\n# Define the input and output filenames\nmy $input_filename = 'extract_TEL_single_line_distinct.txt';\nmy $output_filename = 'extract_TEL_single_line_distinct_semicolon.txt';\n\n# Open the input file for reading\nopen(my $fh_in, '&lt;', $input_filename) or die \"Could not open file '$input_filename' $!\";\n# Open the output file for writing\nopen(my $fh_out, '>', $output_filename) or die \"Could not open file '$output_filename' for writing: $!\";\n\n# Loop through each line of the input file\nwhile (my $line = &lt;$fh_in>) {\n    chomp $line;  # Remove newline character\n\n    # Replace '|' with ';'\n    $line =~ s\/\\|\/;\/g;\n\n    # Write the modified line to the output file\n    print $fh_out \"$line\\n\";\n}\n\n# Close the file handles\nclose $fh_in;\nclose $fh_out;\n\nprint \"Processing complete. '|' replaced with ';' in $output_filename\\n\";\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>mechanize downloaded file, extract perl: extract_TEL.pl (removes leading space of the line having TEL:) extract_TEL_single_line.pl (move TEL: line to above line) extract_TEL_single_line_only.pl (only lines having TEL: on it) extract_TEL_single_line_distinct.pl (distinct lines only) extract_TEL_single_line_distinct_semicolon.pl (replace | with \ud83d\ude09<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[26],"tags":[],"class_list":["post-3595","post","type-post","status-publish","format-standard","hentry","category-codes"],"_links":{"self":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/posts\/3595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3595"}],"version-history":[{"count":0,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/posts\/3595\/revisions"}],"wp:attachment":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}