Io-Redirection

如何將 sqlite3 輸出重定向到文件

  • January 24, 2019

我安裝sqlite3並想使用它來恢復stylish.sqlite 位於我的 Firefox 配置文件文件夾中並由Stylish 擴展生成的資訊:

$ cd ~/.mozilla/firefox/w4wcp85s.default
$ sqlite3 stylish.sqlite
SQLite version 3.7.17 2013-05-20 00:56:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
style_meta  styles
sqlite> SELECT * FROM styles;
6||||YouTube|/* AGENT_SHEET */ 
/* ▓▓ NIGHTSHIFT - eye care:                                 ▓▓
  ▓▓_http://userstyles.org/styles/18192/nightshift-eye-care_▓▓ */

@namespace url(http://www.w3.org/1999/xhtml);
@-moz-document regexp("https?://www.youtube.com/.*")  {
body,html {min-height: 100%!important; }
html, body{background-color:#111!important}
body>*:not(:empty){background-color:#222!important}
body>*>*:not(:empty){background-color:#222!important}
body>*>*>*:not(:empty){background-color:#282828!important}
... 

我的問題是:如何將輸出擷取sqlite> SELECT * FROM styles;到文件中?(我知道我可以在終端中選擇輸出並將其複製到文件中。)

將其全部作為單個命令執行:

$ sqlite3 stylish.sqlite "SELECT * FROM styles;" > somefile.txt

例子

$ sqlite3 addons.sqlite "select * from icon;" > somefile.txt

$ cat somefile.txt 
1|32|https://addons.cdn.mozilla.net/img/uploads/addon_icons/354/354399-32.png?modified=1369154804
1|64|https://addons.cdn.mozilla.net/img/uploads/addon_icons/354/354399-64.png?modified=1369154804

使用三通

如果您想查看正在寫入文件的輸出,您可以tee改用它。

$ sqlite3 addons.sqlite "select * from icon;" | tee somefile.txt
1|32|https://addons.cdn.mozilla.net/img/uploads/addon_icons/354/354399-32.png?modified=1369154804
1|64|https://addons.cdn.mozilla.net/img/uploads/addon_icons/354/354399-64.png?modified=1369154804

引用自:https://unix.stackexchange.com/questions/111608