bash 逐行解析sqlite查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5577280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse sqlite query line by line
提问by Proxx
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"
我有一个我自己无法解决的问题,我有一个 SQLite 数据库,其中包含用于检索数据的数据 我使用 bash 脚本和命令 sqlite3 db "SELECT prkey FROM printers"
the output is something like this:
输出是这样的:
1
1
3
3
4
4
5
5
6
6
7
7
i need to parse each line and use it for my next command. and with out the use of a > file
我需要解析每一行并将其用于我的下一个命令。并且不使用> file
regards Marco
问候马可
回答by Idelic
The forloops 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:
该for循环的工作,但他们都不是很强大的:他们打破如果输出线具有空间,且外壳必须在开始运行循环之前查询的整个输出存储在内存中。这两个问题很容易解决:
sqlite3 db "SELECT ..." | while read prkey; do
echo "do stuff with $prkey"
done
回答by SiegeX
#!/bin/bash
for line in $(sqlite3 db "SELECT prkey FROM printers"); do
echo "line is --> $line" # do stuff with $line
done
回答by Krishna
You can use for loop on the return value of the sqlite command.
您可以对 sqlite 命令的返回值使用 for 循环。
for prkey in `sqlite3 db "SELECT prkey FROM printers"`; do
echo $prkey; #replace with your command
done;

