[20190108]rlwrap sqlplus tee相关问题.txt
--//这个问题一直困扰我很久.如果我执行:
rlwrap sqlplus scoott/book | tee -a aa.txt
--//这样sqlplus窗口有1个小毛病,就是提示符要回车才显示,有什么好方法解决这个问题.实际上就是tee缓存一行没有输出.
--//向一些命令都有1个规避缓存的命令行参数.比如tcpdump 的-l参数,sed -u参数.
--//而tee没有参数控制这样的行为.
--//expect包里面包含一个unbuffer,可以禁止输出缓存.
--//一些新版本linux版本的coreutils包里面包括一个命令stdbuf(Red Hat Enterprise Linux Server release 7.5就有这个命令)
--//我自己也做了一些尝试,都是不行,如下方式不行.
$ rlwrap sqlplus sys as sysdba | stdbuf -i0 -o0 -e0 tee -a /tmp/aa.txt
$ rlwrap sqlplus scott/book | unbuffer -p tee -a /tmp/aa.txt
--//开始看rlwrap文档,我发现实际rlwrap提供filter功能.充分利用这个功能就可以实现:
# rlwrap -z listing
The following filters can be found in /usr/local/share/rlwrap/filters
count_in_prompt replace prompt by simple counter
ftp_filter run plain Netkit ftp with completion for commands, local and remote files
history_format Append
logger log messages to a file (for debugging)
null a filter that does nothing
paint_prompt paint the prompt in colour gradient between X11 colours
pipeline combines the effects of 2 or more filters
pipeto Allow piping of
scrub_prompt removes all junk from prompt
simple_macro simple on-the-fly macro processing
template filter template
unbackspace remove backspaces from output
--//做了尝试不行.开始google,百度.发现如下链接,里面提供一个outfilter脚本.链接如下:
--//https://unix.stackexchange.com/questions/189623/rlwrap-z-pipeto-piping-output-through-pagers
# cd /usr/local/share/rlwrap/filters
# vim outfilter
#! /usr/bin/perl
use lib ($ENV{RLWRAP_FILTERDIR} or ".");
use RlwrapFilter;
use strict;
my $filter = new RlwrapFilter;
my $name = $filter->name;
my $filter_command = join ' ', @ARGV;
$filter->help_text("Usage: rlwrap -z '$name
. "Filter
$filter->output_handler(sub {""});
$filter->prompt_handler(\&prompt);
$filter->run;
sub prompt {
my $prompt = shift;
my $output = $filter->cumulative_output;
$output =~ s/\r//g;
open (PIPE, "| $filter_command")
or die "Failed to create pipe: $!";
print PIPE $output;
close PIPE;
return $prompt;
}
# chmod 755 outfilter
$ rlwrap -z 'outfilter tee -a /tmp/aa.txt' sqlplus scott/book
--//现在没有问题了.
--//利用这个功能通过/tmp/aa.txt实现过滤功能.
--//例如在另外的终端执行如下,终端2:
$ tail -f /tmp/aa.txt |grep -i SUPP
--//在终端1 sqlplus下执行:
SCOTT@book> @ desc v$database ;
--//终端2输出如下:
30 SUPPLEMENTAL_LOG_DATA_MIN VARCHAR2(8)
31 SUPPLEMENTAL_LOG_DATA_PK VARCHAR2(3)
32 SUPPLEMENTAL_LOG_DATA_UI VARCHAR2(3)
40 SUPPLEMENTAL_LOG_DATA_FK VARCHAR2(3)
41 SUPPLEMENTAL_LOG_DATA_ALL VARCHAR2(3)
51 SUPPLEMENTAL_LOG_DATA_PL VARCHAR2(3)
--//这样就是知道那个字段对应位置,ctrl+c,然后执行如下:
$ tail -f /tmp/aa.txt |cut -d"|" -f1,2,30,31,32,40,41,51
--//终端1执行如下:
SCOTT@book> set linesize 3000
SCOTT@book> set colsep |
SCOTT@book> select * from v$database ;
--//终端2可以看到输出如下:
DBID|NAME |SUPPLEME|SUP|SUP|SUP|SUP|SUP
----------|--------------------|--------|---|---|---|---|---
1337401710|BOOK |NO |NO |NO |NO |NO |NO
--//这样就可以实现类似我以前链接写的功能:http://blog.itpub.net/267265/viewspace-2285749/