开发者

Parse sqlite query line by line

开发者 https://www.devze.com 2023-02-22 21:43 出处:网络
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 FRO开发者_如何学JAVAM p

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 FRO开发者_如何学JAVAM 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


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


#!/bin/bash

for line in $(sqlite3 db "SELECT prkey FROM printers"); do
  echo "line is --> $line" # do stuff with $line
done


You can use for loop on the return value of the sqlite command.

for prkey in `sqlite3 db "SELECT prkey FROM printers"`; do
    echo $prkey; #replace with your command
done;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号