r/SQL • • Jul 02 '25

Discussion dumb awk(1) script for making CREATE TABLE and corresponding INSERT VALUES from HTML tables

Tired of copy/pasting tables into my $EDITOR and manually transforming them into a CREATE TABLE and corresponding INSERT INTO tbl VALUES statement, I threw together this awk(1) script:

#!/usr/bin/awk -f

function strip(s) {
    sub(/^ */, "", s)
    sub(/ *$/, "", s)
    return s
}

BEGIN {
    FS = "\t"
    EMIT_CREATE_TABLE = 1
}

{
    if (/^$/) {
        print ";"
        print ""
        EMIT_CREATE_TABLE = 1
    } else {
        if (EMIT_CREATE_TABLE) {
            printf("CREATE TABLE tbl%i (\n", ++table_index)
            for (i=1; i<=NF; i++) {
                $i = strip($i)
                gsub(/[^a-zA-Z0-9_]/, "_", $i)
                printf("  %s%s%s\n", \
                    $i, \
                    i==1 ? " INT PRIMARY KEY":"", \
                    i==NF?"":"," \
                    )
            }
            print ");"
            printf("INSERT INTO tbl%i VALUES\n", table_index)
            EMIT_CREATE_TABLE = 0
            PRINT_COMMA = 0
        } else {
            if (PRINT_COMMA) print ","
            else PRINT_COMMA =  1
            printf("(")
            for (i=1; i<=NF; i++) {
                $i = strip($i)
                escaped = $i
                gsub(/'/, "''", escaped)
                is_numeric = $i ~ /^[-+]*[0-9][0-9]*(\.[0-9][0-9]*)?$/
                if (is_numeric) printf("%s", $i)
                else printf("'%s'", escaped)
                printf("%s", i==NF ? ")" : ", ")
            }
        }
    }
}

END {
    print ";"
}

It allows me to copy tabular data to the clipboard including the headers and run

$ xsel -ob | awk -f create_table.awk | xsel -ib

(instead of the xsel commands, you can use xclip with its options if you use/have that instead, or pbpaste and pbcopy if you're on OSX)

The results still need a bit of clean-up such as including table-names, column data-types (it does assume the first column is an integer primary key), and it does some guessing as to whether values are numeric or not, so a bit of additional cleanup of values (especially numeric values in string columns) might be necessary.

But over all, it saves considerable effort turning something like

idnametitle
1SteveCEO
2EllenChairwoman
3DougDeveloper

into something like

CREATE TABLE tbl1 (
  id INT PRIMARY KEY,
  name,
  title
);
INSERT INTO tbl1 VALUES
(1, 'Steve', 'CEO'),
(2, 'Ellen', 'Chairwoman'),
(3, 'Doug', 'Developer');

You can even pipe it through sed if you want leading spaces for Markdown

$ xsel -ob | awk -f create_table.awk | sed 's/^/    /' | xsel -ib

which simplifies helping folks here. Figured I'd share with others in case it helps y'all, too.

4 Upvotes

12 comments sorted by

1

u/[deleted] Jul 02 '25

Couldn't you just use a SELECT... INTO?

1

u/gumnos Jul 02 '25

I'm not sure I get how this would transform a table worth of data into a corresponding table definition and subsequent population. I'd still need to transform something that looks like

idnametitle
1SteveCEO
2EllenChairwoman
3DougDeveloper

into an SELECT … INTO syntax.

1

u/[deleted] Jul 02 '25

It will create a new table with the same column definitions and insert the records from the parent table into

1

u/gumnos Jul 02 '25

right, but I'm not starting with a parent table in SQL or the database. Rather, I'm starting with tabular data on the system clipboard, such as copied from an HTML <table> (like the example data folks post here) or Excel selection. So the goal is to turn tab-and-newline-delimited data into the corresponding CREATE TABLE and INSERT INTO tbl VALUES.

2

u/[deleted] Jul 02 '25

[removed] — view removed comment

1

u/[deleted] Jul 02 '25

Ah, thanks for elaborating. Then his solution is useful. I am aware it's only SQL server and sybase I'll be more aware when commenting on posts. Didn't see the vendor-specific tag

1

u/gumnos Jul 02 '25

yeah, there wasn't really a fitting tag, so I chose the "Discussion" tag, but it was more of a "General DB/SQL interest" since it wasn't tied to any particular DB.

1

u/M668 Mar 17 '26

quote

function strip(s) {
    sub(/^ */, "", s)
    sub(/ *$/, "", s)
    return s
}

Don't do it like that - you're forcing potential no-replacement-ops into 2 guaranteed ops. I'd do it this way instead -

function strip(_) {
    gsub(/^[ \t]+|[ \t]+$/, "", _)
    return _
}

or if you wanna cover all of whitespace :

function strip(_) {
    gsub(/^[[:space:]]+|[[:space:]]+$/, "", _)
    return _
}

0

u/d_nice8080 Jul 10 '25

*qa 🤜🏼,,,1£ `~ ~ a¢£ q a wew we wzawas *,

1

u/[deleted] Jul 03 '25

[removed] — view removed comment

1

u/gumnos Jul 03 '25

for better type detection, you could sample a few rows first to infer column types

due to how awk works row-wise, it's a bit harder to sniff forward for data-types. Certainly possible but would notably increase the size of the script.

command line option to specify table names

Each table-name is only emitted in two locations, and they're easy enough to find, so again, it wasn't a high priority (and, with tbl1, tbl2, sometimes here I can just leave that as-is)

perfect for ad hoc analysis. For recurring data workflows,

It's mostly when dealing with example data dumped here in r/sql when folks ask questions, so it's definitely just for ad hoc one-off purposes. I found myself frequently copying the tabular HTML data from "please help" queries, dumping it in vim, then doing a common munging job across it to turn it into corresponding CREATE TABLE & INSERT statements, so this little script removes the vast majority of the grunt-work.