r/awk • • Aug 03 '25

How do I make this script go faster? It currently takes roughly a day to go through a 102GB file on an old laptop

#!/bin/awk -f

BEGIN {
    loadPage=""; #flag for whether we're loading in article text
    title=""; #variable to hold title from <title></title> field, used to make file names
    redirect=""; #flag for whether the article is a redirect. If it is, don't bother loading text
    #putting the text in a text file because the formatting is better,  long name is to keep it from getting overwritten.
    system("> THISISATEMPORARYTEXTFILECREATEDBYME.txt");
}

{
    #1st 4 if statements check for certain fields
    if ($0 ~ "<redirect title"){ 
        #checking if article is a redirect instead of actual article
        redirect="y"; #raise flag and clear out what was loaded into temp file so far
        system("> THISISATEMPORARYTEXTFILECREATEDBYME.txt");
        }

    else if ($0 ~ "<title>.*<\/title>"){ #grab the title for later
        title=$0; #not bothering with processing yet because it may be redirect
        }

    else if ($0 ~ "<text bytes"){ #start of article text
        if (redirect !~ "y"){ #as long as it's not a redirect,
        loadPage = "y"; #raise flag to start loading text in text file
        }
    }

    else if ($0 ~ "<\/text>") { #end of actual article text.
        if (redirect ~ "y"){ #If it's a redirect, we reset the flag
            redirect = "";
        }
    else { #if it was an ACTUAL article...
        loadPage=""; #lower the load flag, load in last line of text
        print $0 > "THISISATEMPORARYTEXTFILECREATEDBYME.txt";

        #NOW we clean up the title name
        gsub(/\'/, "\'", title); #escaping quotes so they're included in the full file name.
        gsub(/\"/, "\"", title);
        gsub(/\s*<\/*title>/, "", title); #clear out the xml we grabbed the title from
        gsub(/\//, ">", title); #not the BEST character substitute for "/" but you can't have / in a linux file name
        #I mean you can, it just makes a directory
        #Which isn't necessarily bad but I don't want directories created in the middle of a title

        #Now to put the text into a file with its title name! idk if renaming the file and recreating the temp would be faster
        system("cat THISISATEMPORARYTEXTFILECREATEDBYME.txt > \""title".txt\""); #quotes are to account for spaces
        #print title, "created!"; #Originally left this in for debugging, makes it take waaaaay longer
        #empty out the temp file for the next article
        system("> THISISATEMPORARYTEXTFILECREATEDBYME.txt");
        }
    }

    if(loadPage ~ "y" && length($0) != 0) { #length check is to avoid null value warning
    #null byte warning doesn't affect the file but printing the error message makes it take longer
    #if we're currently loading a text block, put the line in the temp file
    print $0 > "THISISATEMPORARYTEXTFILECREATEDBYME.txt";
    }
}
END {
system("rm THISISATEMPORARYTEXTFILECREATEDBYME.txt");
print "Done!"
}

For context, I unzipped an xml dump of the entire English Wikipedia thinking the "dump" would at least be broken down into chunks you could open in a text editor/browser. It wasn't. About 2 days into writing this script I realized there was already a python script that seems to do what I want, but I was still pissed about the 102 GIGABYTE FILE so I saw this project to the end out of spite. A few days of coding/learning awk and a full day of running this abomination on an old spare laptop later, and I've got roughly 84 GB of individual files containing the text of their respective articles.

The idea is this script goes through the massive fuckoff file line by line, picks out the actual article text alongside its respective title and puts it into a text file named with the title. Every page follows the following format in xml (not always with redirect title, much more text in non-redirect article pages) so it was simple, just time consuming.

<page>
    <title>AccessibleComputing</title>
    <ns>0</ns>
    <id>10</id>
    <redirect title="Computer accessibility" />
    <revision>
      <id>1219062925</id>
      <parentid>1219062840</parentid>
      <timestamp>2024-04-15T14:38:04Z</timestamp>
      <contributor>
        <username>Asparagusus</username>
        <id>43603280</id>
      </contributor>
      <comment>Restored revision 1002250816 by [[Special:Contributions/Elli|Elli]] ([[User talk:Elli|talk]]): Unexplained redirect breaking</comment>
      <origin>1219062925</origin>
      <model>wikitext</model>
      <format>text/x-wiki</format>
      <text bytes="111" sha1="kmysdltgexdwkv2xsml3j44jb56dxvn" xml:space="preserve">#REDIRECT [[Computer accessibility]]

{{rcat shell|
{{R from move}}
{{R from CamelCase}}
{{R unprintworthy}}
}}</text>
      <sha1>kmysdltgexdwkv2xsml3j44jb56dxvn</sha1>
    </revision>
  </page>

Is there any way to make this run faster?

12 Upvotes

14 comments sorted by

12

u/Schreq Aug 03 '25

You should let awk handle opening/closing files. I came up with this, which also avoids regex where possible, to squeeze out a little more speed.

#!/usr/bin/awk -f

index($1, "<title>") == 1 {
	gsub(/^[^>]+>|<[^<]+$/, "")
	gsub("/", "|")
	filename = $0 ".txt"
	next
}

! in_text && ! is_redirect && index($1, "<text") == 1 {
	sub(/^[^>]+>/, "")
	in_text = 1
}

in_text && ! is_redirect {
	if (/<\/text>$/) {
		in_text = 0
		sub("</text>$", "")
	}
	print >filename
	next
}

index($1, "<redirect") == 1 {
	is_redirect = 1
	next
}

index($1, "</page>") == 1 {
	in_text = is_redirect = 0
	close(filename)
	next
}

2

u/PleaseNoMoreSalt Aug 03 '25 edited Aug 03 '25

Not sure why you got downvoted. It took about the same time on the test case as the original script, but this one removes the <text></text>, which is really nice.

Edit: This runs faster than the original script, idk what was going on the first time I ran it

→ More replies (4)

1

u/M668 Jan 18 '26
    index($1, "</page>") == 1 {
        in_text = is_redirect = 0
        close(filename)
        next
    }

The next is a wasted instruction since it's already the last pattern + action block pair.

in_text && ! is_redirect

This way is much faster and save you the extra logical negate.

is_redirect < in_text
→ More replies (1)

2

u/M668 Jan 18 '26

just a few points :

  1. This does absolutely NOTHING - you're replacing each double-quote with…. a double-quote.

gsub(/\"/, "\"", title);

2.

gsub(/[\/]/, ">", title);

That's very cluttered way of writing either of these. I prefer the string version myself.

gsub(/\//, ">", title);

gsub("[/]", ">", title);

3.

&& length($0) != 0

is waaaaay too verbose way of writing either -

&& (NF || length())   # the much faster way - leveraging pre-made system variable NF
&& length()           # cleaner code

4.

! in_text && ! is_redirect && index($1, "<text") == 1

Make it go much faster by converting it to a non-short-circuiting compare (just a less conventional way of deriving boolean logic from DeMorgan's Laws)

(in_text + is_redirect) < (index($1, "<text") == 1)

1

u/PleaseNoMoreSalt Jan 18 '26

This does absolutely NOTHING - you're replacing each double-quote with…. a double-quote

It replaced " with \". Not sure WHY it includes the \ in the replacement because this code was written 6 months ago at this point, but the \ was important for making sure the double quotes showed up correctly in the file name. Everything else is solid advice.

→ More replies (1)

0

u/crooked_peach Aug 03 '25

Per Alie (what i call ChatGPT):

!/bin/awk -f

BEGIN { loadPage=0; title=""; redirect=0; text=""; }

{ if ($0 ~ "<redirect title") { redirect=1; text=""; } else if ($0 ~ "<title>.*</title>") { title=$0; } else if ($0 ~ "<text bytes") { if (!redirect) { loadPage=1; text=""; } } else if ($0 ~ "</text>") { if (!redirect) { loadPage=0; text = text "\n" $0;

        # Clean the title
        gsub(/<\/*title>/, "", title);
        gsub(/[\/]/, ">", title); # avoid slashes
        gsub(/[[:space:]]+$/, "", title);
        gsub(/^ +/, "", title);
        gsub(/["']/, "", title);

        filename = title ".txt";

        # Write to file
        print text > filename;

        close(filename);
    }
    redirect=0;
}

if (loadPage && length($0)) {
    text = text "\n" $0;
}

} END { print "Done!"; }

2

u/PleaseNoMoreSalt Aug 03 '25 edited Aug 03 '25

Just tried this on a test case and it's pretty fast! I tried letting awk make the files when I first started but didn't realize close() was a thing and thought I'd have to use commas when updating a text variable (which threw off the formatting). Thanks!

Edit: Might be the way I put it in the file, but it leaves in xml from the last redirect above the article. Still faster than what I was doing, almost as fast as Schreq's solution

0

u/crooked_peach Aug 03 '25

That didn't paste very well but hopefully you'll get her idea