- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一些格式如下的包罗万象的日志文件:
timestamp event summary
foo details
account name: userA
bar more details
timestamp event summary
baz details
account name: userB
qux more details
timestamp etc.
我想在日志文件中搜索 userB
,如果找到,则从前面的时间戳回显到(但不包括)下面的时间戳。可能会有几个事件与我的搜索相匹配。最好在每个匹配项周围回显某种 --- start ---
和 --- end ---
。
这对于 pcregrep -M
来说是完美的,对吧?问题是,GnuWin32 的 pcregrep
会因多行正则表达式搜索大文件而崩溃,并且这些包罗万象的日志可能有 100 兆或更多。
我尝试过的
到目前为止,我的 hackish 解决方法包括使用 grep -B15 -A30
来查找匹配行并打印周围的内容,然后将现在更易于管理的 block 输送到 pcregrep
中进行抛光。问题是有些事件少于 10 行,而另一些则超过 30 行;在遇到较短的事件时,我得到了一些意想不到的结果。
:parselog <username> <logfile>
set silent=1
set count=0
set deez=20\d\d-\d\d-\d\d \d\d:\d\d:\d\d
echo Searching %~2 for records containing %~1...
for /f "delims=" %%I in (
'grep -P -i -B15 -A30 ":\s+\b%~1\b(@mydomain\.ext)?$" "%~2" ^| pcregrep -M -i "^%deez%(.|\n)+?\b%~1\b(@mydomain\.ext|\r?\n)(.|\n)+?\n%deez%" 2^>NUL'
) do (
echo(%%I| findstr "^20[0-9][0-9]-[0-9][0-9]-[0-9][0-9].[0-9][0-9]:[0-9][0-9]:[0-9][0-9]" >NUL && (
if defined silent (
set silent=
set found=1
set /a "count+=1"
echo;
echo ---------------start of record !count!-------------
) else (
set silent=1
echo ----------------end of record !count!--------------
echo;
)
)
if not defined silent echo(%%I
)
goto :EOF
有更好的方法吗?我遇到了一个看起来很有趣的 awk
命令,类似于:
awk "/start pattern/,/end pattern/" logfile
...但它也需要匹配一个中间模式。不幸的是,我不太熟悉 awk
语法。有什么建议吗?
Ed Morton 建议我提供一些示例日志记录和预期输出。
包罗万象的例子
2013-03-25 08:02:32 Auth.Critical 169.254.8.110 Mar 25 08:02:32 dc3 MSWinEventLog 2 Security 11730158 Mon Mar 25 08:02:28 2013 529 Security NT AUTHORITY\SYSTEM N/A Audit Failure dc3 2 Logon Failure:
Reason: Unknown user name or bad password
User Name: user5f
Domain: MYDOMAIN
Logon Type: 3
Logon Process: Advapi
Authentication Package: Negotiate
Workstation Name: dc3
Caller User Name: dc3$
Caller Domain: MYDOMAIN
Caller Logon ID: (0x0,0x3E7)
Caller Process ID: 400
Transited Services: -
Source Network Address: 169.254.7.86
Source Port: 40838
2013-03-25 08:02:32 Auth.Critical 169.254.8.110 Mar 25 08:02:32 dc3 MSWinEventLog 2 Security 11730159 Mon Mar 25 08:02:29 2013 680 Security NT AUTHORITY\SYSTEM N/A Audit Failure dc3 9 Logon attempt by: MICROSOFT_AUTHENTICATION_PACKAGE_V1_0
Logon account: USER6Q
Source Workstation: dc3
Error Code: 0xC0000234
2013-03-25 08:02:32 Auth.Critical 169.254.8.110 Mar 25 08:02:32 dc3 MSWinEventLog 2 Security 11730160 Mon Mar 25 08:02:29 2013 539 Security NT AUTHORITY\SYSTEM N/A Audit Failure dc3 2 Logon Failure:
Reason: Account locked out
User Name: USER6Q@MYDOMAIN.TLD
Domain: MYDOMAIN
Logon Type: 3
Logon Process: Advapi
Authentication Package: Negotiate
Workstation Name: dc3
Caller User Name: dc3$
Caller Domain: MYDOMAIN
Caller Logon ID: (0x0,0x3E7)
Caller Process ID: 400
Transited Services: -
Source Network Address: 169.254.7.89
Source Port: 55314
2013-03-25 08:02:32 Auth.Notice 169.254.5.62 Mar 25 08:36:38 DC4.mydomain.tld MSWinEventLog 5 Security 201326798 Mon Mar 25 08:36:37 2013 4624 Microsoft-Windows-Security-Auditing N/A Audit Success DC4.mydomain.tld 12544 An account was successfully logged on.
Subject:
Security ID: S-1-0-0
Account Name: -
Account Domain: -
Logon ID: 0x0
Logon Type: 3
New Logon:
Security ID: S-1-5-21-606747145-1409082233-725345543-160838
Account Name: DEPTACCT16$
Account Domain: MYDOMAIN
Logon ID: 0x1158e6012c
Logon GUID: {BCC72986-82A0-4EE9-3729-847BA6FA3A98}
Process Information:
Process ID: 0x0
Process Name: -
Network Information:
Workstation Name:
Source Network Address: 169.254.114.62
Source Port: 42183
Detailed Authentication Information:
Logon Process: Kerberos
Authentication Package: Kerberos
Transited Services: -
Package Name (NTLM only): -
Key Length: 0
This event is generated when a logon session is created. It is generated on the computer that was accessed.
The subject fields indicate...
2013-03-25 08:02:32 Auth.Critical 169.254.8.110 Mar 25 08:02:32 dc3 MSWinEventLog 2 Security 11730162 Mon Mar 25 08:02:30 2013 675 Security NT AUTHORITY\SYSTEM N/A Audit Failure dc3 9 Pre-authentication failed:
User Name: USER8Y
User ID: %{S-1-5-21-606747145-1409082233-725345543-3904}
Service Name: krbtgt/MYDOMAIN
Pre-Authentication Type: 0x0
Failure Code: 0x19
Client Address: 169.254.87.158
2013-03-25 08:02:32 Auth.Critical etc.
示例命令
call :parselog user6q \\path\to\catch-all.log
预期结果
---------------start of record 1-------------
2013-03-25 08:02:32 Auth.Critical 169.254.8.110 Mar 25 08:02:32 dc3 MSWinEventLog 2 Security 11730159 Mon Mar 25 08:02:29 2013 680 Security NT AUTHORITY\SYSTEM N/A Audit Failure dc3 9 Logon attempt by: MICROSOFT_AUTHENTICATION_PACKAGE_V1_0
Logon account: USER6Q
Source Workstation: dc3
Error Code: 0xC0000234
---------------end of record 1-------------
---------------start of record 2-------------
2013-03-25 08:02:32 Auth.Critical 169.254.8.110 Mar 25 08:02:32 dc3 MSWinEventLog 2 Security 11730160 Mon Mar 25 08:02:29 2013 539 Security NT AUTHORITY\SYSTEM N/A Audit Failure dc3 2 Logon Failure:
Reason: Account locked out
User Name: USER6Q@MYDOMAIN.TLD
Domain: MYDOMAIN
Logon Type: 3
Logon Process: Advapi
Authentication Package: Negotiate
Workstation Name: dc3
Caller User Name: dc3$
Caller Domain: MYDOMAIN
Caller Logon ID: (0x0,0x3E7)
Caller Process ID: 400
Transited Services: -
Source Network Address: 169.254.7.89
Source Port: 55314
---------------end of record 2-------------
最佳答案
这就是您使用 GNU awk(对于 IGNORECASE)所需的全部:
$ cat tst.awk
function prtRecord() {
if (record ~ regexp) {
printf "-------- start of record %d --------%s", ++numRecords, ORS
printf "%s", record
printf "--------- end of record %d ---------%s%s", numRecords, ORS, ORS
}
record = ""
}
BEGIN{ IGNORECASE=1 }
/^[[:digit:]]+-[[:digit:]]+-[[:digit:]]+/ { prtRecord() }
{ record = record $0 ORS }
END { prtRecord() }
或使用任何 awk:
$ cat tst.awk
function prtRecord() {
if (tolower(record) ~ tolower(regexp)) {
printf "-------- start of record %d --------%s", ++numRecords, ORS
printf "%s", record
printf "--------- end of record %d ---------%s%s", numRecords, ORS, ORS
}
record = ""
}
/^[[:digit:]]+-[[:digit:]]+-[[:digit:]]+/ { prtRecord() }
{ record = record $0 ORS }
END { prtRecord() }
无论哪种方式,您都可以在 UNIX 上将其运行为:
$ awk -v regexp=user6q -f tst.awk file
我不知道 Windows 语法,但我希望它即使不相同也非常相似。
请注意在脚本中使用 tolower() 使比较的两边都小写,因此匹配不区分大小写。如果您可以改为传递大小写正确的搜索正则表达式,则无需在比较的任何一侧调用 tolower()。 nbd,它可能只会稍微加快脚本的速度。
$ awk -v regexp=user6q -f tst.awk file
-------- start of record 1 --------
2013-03-25 08:02:32 Auth.Critical 169.254.8.110 Mar 25 08:02:32 dc3 MSWinEventLog 2 Security
11730159 Mon Mar 25 08:02:29 2013 680 Security NT AUTHORITY\SYSTEM N/A Audit Failure
dc3 9 Logon attempt by: MICROSOFT_AUTHENTICATION_PACKAGE_V1_0
Logon account: USER6Q
Source Workstation: dc3
Error Code: 0xC0000234
--------- end of record 1 ---------
-------- start of record 2 --------
2013-03-25 08:02:32 Auth.Critical 169.254.8.110 Mar 25 08:02:32 dc3 MSWinEventLog 2 Security
11730160 Mon Mar 25 08:02:29 2013 539 Security NT AUTHORITY\SYSTEM N/A Audit Failure
dc3 2 Logon Failure:
Reason: Account locked out
User Name: USER6Q@MYDOMAIN.TLD
Domain: MYDOMAIN
Logon Type: 3
Logon Process: Advapi
Authentication Package: Negotiate
Workstation Name: dc3
Caller User Name: dc3$
Caller Domain: MYDOMAIN
Caller Logon ID: (0x0,0x3E7)
Caller Process ID: 400
Transited Services: -
Source Network Address: 169.254.7.89
Source Port: 55314
--------- end of record 2 ---------
关于windows - 日志文件中的正则表达式匹配,返回匹配上方和下方的动态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15628017/
假设有一个具有非常简单的 UI 层次结构的窗口,它只有两个同级: NSTextView 和 NSButton,并且它们确实重叠。 我的问题是,为什么光标悬停在按钮上时会有所不同,具体取决于其下方是否有
例如,当我将 TPanel 添加到表单并使用 alTop 对齐它,然后将 TMainMenu 添加到同一表单时,主菜单位置会覆盖面板位置,以便主菜单保持在窗体的最顶层,面板显示在其下方。 是否可以覆盖
我想构建一个 UI,其中我在屏幕顶部有一些固定的小部件(在所有选项卡上可见),然后在它们下面我想要一个 TabBarView(底部有选项卡栏),这是否可能而不使您的自己的标签小部件还是 TabBarV
我一直在这里关注 Phaser 3 的介绍 http://phaser.io/tutorials/making-your-first-phaser-3-game并发现我的 Angular 色“悬停”在
我在 View 中间有一个 UITextView,在 View 底部有一个 UIToolBar。当我触摸 UITextView 时,我希望在 UIToolBar 上方出现一个可视键盘。我该怎么做? 最
我正在尝试制作井字游戏,我有一个 3 x 3 按钮网格,但我想在按钮上方有一个栏,显示分数和玩家姓名,有人可以帮助我吗?到java,不知道从哪里开始。 import javax.swing.*; im
是否可以指定 noUiSlider 绘制的工具提示的位置?似乎默认情况下它将第一个放在 slider 上,第二个放在下面。我理解这里的可用性目标,但我想把两者都放在上面或下面。 示例(带有上方和下方的
我的应用程序中有主详细布局(参见左图): 我为操作栏设置了导航模式列表(使用它来过滤第二个 fragment 中的 ListView ): final ActionBar actionBar = ge
我是 iOS 编程新手,在使用 XLPagerTabStrip(github.com/xmartlabs/XLPagerTabStrip) 时遇到了困难。我按照它的教程,成功在VC的顶部添加了Page
这个问题适用于任何项目,但在这种情况下,我想将我的图像放置在 map 上方,目前它位于 map 下方。 我是 swift 1.2 的新手,所以我不知道如何组合一个示例,任何帮助将不胜感激。 最佳答案
我如何在 javascript 中确定一个元素是否在视口(viewport)上方(用户已经滚动过它)(不仅仅是它是否可见)? 在这段代码中: 控制台日志(iselement1aboveviewport
我正在尝试将工具栏添加到我的 View Controller 。 我的 View Controller : -(void)loadView { UIWebView *webView = [[U
我知道这是一个一般性问题,但任何方向都会有所帮助。 所以我正在使用这个模块 https://github.com/maxep/MXSegmentedPager尝试重新创建 Twitter 的个人资料页
我需要在保存按钮下放置一个带有 id 的 div。这是 html 代码: save 这是我的 : 通过这种方式,div 隐藏了按钮,我的目的是在 div 上方显示按钮。
我试图将我的整个 refreshControl 放在 tableview 后面。我可以通过这样做来放置实际的微调器: self.tableView.addSubview(refreshContro
这在 Safari 和 Firefox 中都会发生。我的导航栏看起来像这样: Test 相关的 CSS 看起来像: li { float: left } a:hover {
我在 CSS 中的 #footermain div 上有一个框阴影,但我无法让它显示在绿色页脚上方。我什么都试过了,还是想不通。 #footermain { background: #1f6c
我试图将图像部分放置在旋转 slider 上方( slider 上侧的一部分应该被图像覆盖)我试过 image{position:relative; z-index:10; } slider{posi
我认为这是一个简单的问题,但我无法理解它。我在页面顶部有一个固定的 div,当我向下滚动查看内容时,内容(文本)显示在该固定 div 上方 5 像素的边距中。该边距(黑色,请参阅 https://js
我有两个 div,在页脚配置中。目的是在悬停时通过显示隐藏的 div 向上扩展页脚。 #extendedFooter, #test { display: none; } #standardFo
我是一名优秀的程序员,十分优秀!