With following shell script,
- I read file
- and for each line
- generates SQL fragments
#!/bin/bash
CURRDATE=$(date +'%Y-%m-%d %H:%M:%S')
echo $CURRDATE
echo $OUTPUT
echo "SET SESSION AUTOCOMMIT = 0;" > $OUTPUT
echo 'START TRANSACTION;' >> $OUTPUT
echo $'INSERT INTO ...' >> $OUTPUT
echo 'VALUES' >> $OUTPUT
cat $INPUT | tr -d '\r' | while IFS= read -r LINE; do
echo $"(..., '$LINE', ...)," >> $OUTPUT
done
echo "COMMIT;" >> $OUTPUT
The output look like this,
SET SESSION AUTOCOMMIT = 0;
START TRANSACTION;
SET @created_at = '2024-08-09 09:30:45';
INSERT INTO ...
VALUES
(...),
(...),
(...), -- <<<<<<<<<<<<<<<< should be a semi-colon
COMMIT;
How can suppress the last comma? So that I put semi-colon?
echo $"(..., '$LINE', ...)," >> $OUTPUT
makes it a comma.cat
andread
in the first place. I'd make an array, from which I could find the last line and treat it differently. As others suggest, a shell script might not be the best tool for this, but it can be done.