The Student Room Group

Sqlite3 / Bash Help

So I am trying to create a temporary csv file to store the result of running some sql commands on my database, but for some reason the temporary csv file always contains spaces instead of new lines. Here is my script:


#!/bin/bash
tmpcsv=$(mktemp --suffix=.csv)
echo $(sqlite3 -csv database.db < a.sql) > tempcsv
rm $tempcsv


The script should output in the form:


a,b
c,d
e,f
...


But for some reason, this script is giving me:


a,b c,d e,f...


I can't even remove the spaces with sed or cut or anything. If anyone knows what's causing this, please let me know, thanks!
Reply 1
Original post by Smallwatch
So I am trying to create a temporary csv file to store the result of running some sql commands on my database, but for some reason the temporary csv file always contains spaces instead of new lines. Here is my script:



#!/bin/bash
tmpcsv=$(mktemp --suffix=.csv)
echo $(sqlite3 -csv database.db < a.sql) > tempcsv
rm $tempcsv



The script should output in the form:



a,b
c,d
e,f
...



But for some reason, this script is giving me:



a,b c,d e,f...



I can't even remove the spaces with sed or cut or anything. If anyone knows what's causing this, please let me know, thanks!


OK, I can do an export on Windows with sqlite3 and an old db I found and the output is as expected - records separated by newlines.

My unix is extremely rusty and I don't have that OS now, so apologies for the questions below which may be completely off-base, but just trying to get my head around what your script is doing, so tell me if any of these comments are helpful, or just irrelevant :biggrin:

why do you need the mktemp line? Won't unix create the file for you when you issue the sqlite3 command (that's what Windows does!) ?
why have you got variables called tmpcsv and tempcsv? Is that intentional or a typo when you put this on TSR?
why do you have an echo statement wrapped around your sqlite3 command? I don't need the analogous statement in windows - I just issue sqlite3 -csv etc etc.
why issue rm $tempcsv at the end? Isn't that just deleting the file you've created?!

Again, apologies if these are silly questions to a regular unix user - just trying to suggest a few things that may / may not be relevant :smile:
Reply 2
Original post by davros
OK, I can do an export on Windows with sqlite3 and an old db I found and the output is as expected - records separated by newlines.

My unix is extremely rusty and I don't have that OS now, so apologies for the questions below which may be completely off-base, but just trying to get my head around what your script is doing, so tell me if any of these comments are helpful, or just irrelevant :biggrin:

why do you need the mktemp line? Won't unix create the file for you when you issue the sqlite3 command (that's what Windows does!) ?
why have you got variables called tmpcsv and tempcsv? Is that intentional or a typo when you put this on TSR?
why do you have an echo statement wrapped around your sqlite3 command? I don't need the analogous statement in windows - I just issue sqlite3 -csv etc etc.
why issue rm $tempcsv at the end? Isn't that just deleting the file you've created?!

Again, apologies if these are silly questions to a regular unix user - just trying to suggest a few things that may / may not be relevant :smile:

It seems to have been the echo statement that was what was causing the problems, after removing it it outputs to the file normally. Thanks for the pointers! :smile:
Reply 3
Original post by Smallwatch
It seems to have been the echo statement that was what was causing the problems, after removing it it outputs to the file normally. Thanks for the pointers! :smile:

That's weird! Glad you got it sorted in the end :smile:

Quick Reply

Latest

Trending

Trending