calabi
calabi

Reputation: 303

Splitting HTML file using AWK

I was wondering if it's possible to split a HTML file into seperate .html files using awk? I'd like to look for the pattern:

<div class="post">

And when it finds this create the new file for each instance, I've tried to compile the command but can't get it working? My file is called working.html and this is what I got back from the command I've constructed.

awk '/<div class="post">/{x="F"++i;}{print > x;}' working.html

Any ideas?

Upvotes: 0

Views: 571

Answers (1)

DigitalRoss
DigitalRoss

Reputation: 146073

It looks like it's bombing out because x is not initialized and can't be used as a filename until it is first set on a <div> line.

One way to fix that is to add a BEGIN pattern to initialize it.

BEGIN {
  x = "F0"
}

/<div class="post">/ {
  x = "F" ++i
}

{ print > x }

Upvotes: 1

Related Questions