0

I am using Perl to achieve this

while(<INFILE>){
        chomp;
         if(/\<td/){
          system("perl -i -e 's/<td/<td bgcolor="blue"/g' $_");
          }
}

When I run the command I get

./HtmlTest.pl file.html
Bareword found where operator expected at ./HtmlTest.pl line 13, near ""perl -i -e 's/<td/<td bgcolor="grey"
        (Missing operator before grey?)
String found where operator expected at ./HtmlTest.pl line 13, near "grey"/g' $_""
syntax error at ./HtmlTest.pl line 13, near ""perl -i -e 's/<td/<td bgcolor="grey"
Execution of ./HtmlTest.pl aborted due to compilation errors.

I am not able to figure out why

Even if i run as

perl HtmlTest.pl file.html

I get the same errors.

Sample html table

 <td>ABC</td>
 <td>DEF</td>
 <td>20:00:00</td>

Any advice appreciated

7
  • 3
    i'd start by replacing 'system("perl -i -e 's/<td/<td bgcolor="blue"/g' $_");' to 's/<td/<td bgcolor="blue"/g;' you do not need to run a system command here Commented Sep 4, 2020 at 18:25
  • 3
    The inner perl -i -pe ... (assuming you meant -p) expects a filename argument but you are giving it $_ which is the current line in the HTML file. It is not going to work. Try this instead: perl -pie 's/<td/<td bgcolor="blue"/' file.html Commented Sep 4, 2020 at 18:40
  • 2
    A better approach is to edit HTML files is to use dedicated HTML parser like HTML::Parser Commented Sep 4, 2020 at 18:47
  • Are you trying to rewrite a file at the same time you are reading it? That sounds like a very bad idea. But it just so happens that if you use Tie::File you can edit a file as you are reading it. But don't do that in this case. Using Håkon's solution seems much better. Commented Sep 5, 2020 at 7:56
  • 1
    @TLP - yes i was trying to rewrite a file while reading. Didnt think that through Commented Sep 5, 2020 at 19:47

3 Answers 3

3

Regexes may become inefficient when it comes to parsing complex HTML files, a better apporach is then to use a dedicated HTML parser. Here is an example using XML::LibXML provided you have a valid HTML file:

use strict;
use warnings;
use XML::LibXML;

my $filename = 'file.html';
my $html = XML::LibXML->load_html( location  => $filename );
for my $node ($html->findnodes('//td')) {
    $node->setAttribute(bgcolor => "blue");
}
print $html->toStringHTML;
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for this. I need to install these modules. Will work on that
1

I think you need escape the " in the string since it complains about "near "grey"/g' (assumed you tried with grey in your code)

Since the whole string is: "perl -i -e '<string_no_quotes>' $_" if string_no_quotes has " it will give this error, so it needs to be escpaed.

Update:

Should something like this work you write it stdout and pipe it to the file instead?:

foreach my $i ('<td>ABC</td>', '<td>DEF</td>', '<td>20:00:00</td>', '<h1>test</h1>') {
  chomp;
  
  $_ = $i;
  if (/\<td/) {
      print 's/<td/<td bgcolor="blue"/g';
   } else {
      print $_;
   }
}

I replaced the while loop with for loop so I could test it in an online parser. The one I used was this: https://www.tutorialspoint.com/execute_perl_online.php

1 Comment

Tried that, no dice.
1

In OPs code we have following line, which should be corrected to next form

system("perl -i -e 's/<td/<td bgcolor=\"blue\"/g' $_");

It is wrong, $_ will hold current line read from <INFILE> but perl will expect input file instead.

Following code demonstrates alternative solution, which does not utilize any modules. This solution also is not best.

use strict;
use warnings;

while( <DATA> ) {
    s/<td>/<td bgcolor="blue">/;
    print;
}

__DATA__

<block>
  Some text goes in this place
</block>

 <td>ABC</td>
 <td>DEF</td>
 <td>20:00:00</td>
 
 <p>
    New paragraph describing something
 </p>

Instead of utilizing bgcolor="blue" more correct approach is external CSS style style='some_style'.

This approach would allow make changes in style file for desired tags without touching html file.

You edit CSS style file with desired style and magically you web page will be shown with new colors/text style/types of list/ etc.

HTML Styles CSS

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.