Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse sqlite query line by line

Tags:

bash

sql

sqlite

i have a problem which i cant figure out myself, i have a SQLite database which contains data to retrieve the data i use a bash script and the command sqlite3 db "SELECT prkey FROM printers"

the output is something like this:

1

3

4

5

6

7

i need to parse each line and use it for my next command. and with out the use of a > file

regards Marco

like image 355
Proxx Avatar asked Nov 16 '25 06:11

Proxx


1 Answers

The for loops work, but they are not very robust: they break if a line in the output has a space, and the shell must store the whole output of the query in memory before it starts running the loop. These two issues are easily dealt with:

sqlite3 db "SELECT ..." | while read prkey; do
   echo "do stuff with $prkey"
done
like image 55
Idelic Avatar answered Nov 17 '25 20:11

Idelic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!