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?