我所知道和经常使用的
less
我喜欢如何使用+
命令行中的 - 参数添加命令。我喜欢这样进行即时搜索:
$ less +/DHCP /var/log/syslog
# result: syslog at the point of the first occurrence of DHCP
但我也喜欢将其设置为遵循这样的输出:
$ less +F /var/log/syslog
# result: a syslog that follows the file, very much like tail -f would.
我想用什么
但每隔一段时间我就会两者都想要。但我不知道该怎么做。
$ less +F +/DHCP /var/log/syslog
# result: "Pattern not found" - it will actually bunch up both things to one string.
谁能告诉我如何自动过滤而不必在开始时按下,谁能给我加分?
$ less +\&DHCP /var/log/syslog
# result: "please press enter" after which the filtering search _is_
# running correctly. I would love to get rid of that extra <enter>
edit2:有趣的是,我可以将这些结合起来:
$ less +?DHCP +G /var/log/syslog
# result: jumps to the end and reverse-searches for DHCP
# But I have to press enter (which I'd like to avoid)
但我不能这样做:
$ less +G +?DHCP /var/log/syslog
# this is being bunched up to ?DHCPG and subsequently not found.
那么,顺序似乎很重要,所有字符串都被解释为一个字符串?
版本信息
编辑这是我的系统上安装的 less 版本,但如果需要的话我愿意安装另一个版本!
$ less --version
less 458 (GNU regular expressions)
Copyright (C) 1984-2012 Mark Nudelman
[...]
答案1
根据您后来的评论,我认为我不理解您问题的第一部分。对我来说,使用less +F +/pattern logfile
有效并继续突出显示pattern
更新文件中出现的新实例。
至于问题的奖励部分,您可以尝试以下命令之一:
less +\&DHCP$'\n' /var/log/syslog
或者
less +\&DHCP^M /var/log/syslog
在第二个命令中,通过按then^M
生成。当 less 启动时,按 Enter 会更容易,除非你想编写脚本或其他东西。Ctrl-V
Enter
答案2
在巨大的过度复杂性方面:通过包装器使用可更改的搜索词自动尾随expect
!
#!/usr/bin/env expect
#
# Sticky search wrapper for less; tails the given file, though with
# input of
# /asdf
# or whatever will search for that new term then re-tail the file.
# Probably needs `less` with (the default) hilight mode enabled.
if {[llength $argv] == 0} {
puts stderr {Usage: $argv0 [searchterm] file}
exit 64
} elseif {[llength $argv] == 1} {
set fname [lindex $argv 0]
set search ""
} else {
set fname [lindex $argv 1]
set search [lindex $argv 0]
}
# FIXME soas to nix any default options (like turning off hilight) and
# on account of LESSOPEN and LESSCLOSE being security risks due to the
# defaults certain vendors set.
foreach ev [list LESS LESSOPEN LESSCLOSE] {
if {[info exists env($ev)]} {
unset env($ev)
}
}
match_max 999
set timeout -1
spawn -noecho less $fname
expect {
# TODO need better way to detect that less didn't fly...
-ex "No such file or directory" { exit }
-re "." { }
default { exit }
}
if {$search ne ""} {
send -- "/$search\r"
}
send -raw "F"
interact {
-echo -re "/(.*)\[\n\r]" {
# send_user "DBG search $interact_out(1,string)"
send -raw "\003\r/"
send -- $interact_out(1,string)
send -raw "\rF"
}
}
答案3
据我了解,您想要读取该/var/log/syslog
文件,并查看带有“DHCP”的所有条目,并不断查看出现的新条目。如果这是正确的,则可以通过 来实现tail -f /var/log/syslog | grep DHCP
。如果我对您的请求的解释不正确,我哪里出错了?