I will have a CSV file (say, ids.csv) that I need to ETL into a SQL script (say, update_products.sql). The CSV will be headerless and will consist of comma-delimited numbers (product IDs in a database), for instance:
29294848,29294849,29294850,29294851,29294853,29294857,29294858,29294860,29294861,29294863,29294887,29294888,
29294889,29294890,29294891,29294892,29294895,29294897,29294898,29294899,29294901,29294903,29294912,29294916
Starting with a SQL "template" file (template.sql) that looks something like this:
UPDATE products SET quantity = 0 WHERE id = %ID%;
I'm looking for a way via bash, awk, sed (or any other type of shell scripting tool), to templatize %IDS% with the values in the CSV, hence turning the generated SQL into something like:
UPDATE products SET quantity = 0 WHERE id = 29294848;
UPDATE products SET quantity = 0 WHERE id = 29294849;
UPDATE products SET quantity = 0 WHERE id = 29294850;
... etc, for all the IDs in the CSV...
Super flexible here:
- Don't care which tool gets the job done (
awk,sed, bash, whatever...as long as I can run it from the command line) - Don't necessarily NEED a template file (
template.sql) to start with, perhaps the solution can just "inject" this template into the script as an argument - Ideally it would read the input CSV but this is not a hard requirement, if the solution requires me pasting the contents of the CSV file into the script as an argument, I'm OK with that, but not thrilled...
- Ideally it would generate an actual SQL file (
update_products.sql) for me, but if we're limited to console output thats OK to (just not preferred)
Any ideas how I might be able to accomplish this?