xo / usql

Universal command-line interface for SQL databases
MIT License
8.81k stars 346 forks source link

I hope to add a feature similar to the grep filtering in Linux. #449

Closed xxoxx closed 3 months ago

xxoxx commented 3 months ago

For example, similar to the following operations.

grep:

mysql> show variables; | grep log_bin
Variable_name   Value
log_bin ON
log_bin_basename        /var/lib/mysql/binlog
log_bin_index   /var/lib/mysql/binlog.index
log_bin_trust_function_creators OFF
log_bin_use_v1_row_events       OFF
sql_log_bin     ON
6 rows in set (0.01 sec)

grep -v exclude:

mysql> show processlist; | grepv sleep
Id      User    Host    db      Command Time    State   Info
5       event_scheduler localhost       NULL    Daemon  1902954 Waiting on empty queue  NULL
115     root    192.168.3.101:63598     NULL    Query   0       init    show processlist
2 rows in set (0.00 sec)
kenshaw commented 3 months ago

I understand the appeal of this, and it could definitely be implemented somewhat easily, but the question is why this would be helpful? For most queries you'd execute against a database, you would likely prefer/use a SQL WHERE clause to filter the fields. Even for metadata such as this, there's usually a pure SQL query that can be executed to filter the rows/columns.

xxoxx commented 3 months ago

I'm sure that in SQL statements, it's possible to achieve the desired outcome by adding WHERE conditions and LIKE statements, etc. However, this kind of filtering, similar to pipe filtering in Linux commands, would be simpler. For example, I don't need to pay attention to field names. For instance, when I want to view MySQL processes with select * from information_schema.processlist, I don't need to focus on the content of a specific field; I only want to focus on whether the result should contain a certain string or not contain a certain string. This would be a bit more convenient.

kenshaw commented 3 months ago

@xxoxx BTW -- are you aware that you can already pipe the output to any command? Use \g instead of a ;:

Connected with driver mysql (11.3.2-MariaDB-1:11.3.2+maria~ubu2204)
Type "help" for help.

my:root@=> show variables \g |grep log_bin
 log_bin                                                  | OFF 
 log_bin_basename                                         |  
 log_bin_compress                                         | OFF 
 log_bin_compress_min_len                                 | 256 
 log_bin_index                                            |  
 log_bin_trust_function_creators                          | OFF 
 sql_log_bin                                              | ON 
my:root@=>