使用 sed 从日志文件中提取目录
我正在尝试解析 application.log,其中有许多行遵循以下相同的语法。
"Error","jrpp-237","10/13/11","02:55:04",,"File not found: /indexUsa~.cfm The specific sequence of files included or processed is: c:\websites\pj7fe4\indexUsa~.cfm '' "
我需要使用某种类型的命令来提取 c:\websites\
和下一个 \
之间列出的内容,
例如在本例中它将是 pj7fe4< /code>
我认为以下命令会起作用。
bin/sed -n '/c:\\websites\\/,/\\/p' upload/test.log
不幸的是,通过进一步阅读,我现在了解到这将通过 \
返回包含 c:\websites
的整行,并且我需要知道中间的部分,而不是整条线。
更困难的是,我需要匹配所有目录子路径,而不仅仅是某一特定行,因为这是针对多个站点的。
I'm trying to parse through an application.log that has many lines that follow the same syntax below.
"Error","jrpp-237","10/13/11","02:55:04",,"File not found: /indexUsa~.cfm The specific sequence of files included or processed is: c:\websites\pj7fe4\indexUsa~.cfm '' "
I need to use some type of command to pull out what is listed between c:\websites\
and the next \
e.g. in this case it would be pj7fe4
I thought that the following command would work..
bin/sed -n '/c:\\websites\\/,/\\/p' upload/test.log
Unfortunately from reading further I now understand that this will return the entire line containing c:\websites
through the \
and I need to know the in between, not the whole line.
To be more difficult I need to match all of the directory sub paths, not just one particular line as this is for multiple sites.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您错误地使用了范围模式。您不能使用它来将命令(在本例中为打印)限制为行的一部分,而只能限制为行的范围。你也无法逃脱退格键。
试试这个:
sed 's/.*c:\\websites\\\([0-9a-zA-Z]*\)\\.*/\1/'
有一个很好的 sed教程在这里:Sed - Bruce Barnett 的介绍和教程
You're using range patterns incorrectly. You can't use it to limit the command (print in this case) to a part of the line, only to a range of lines. You also don't escape the backspaces.
Try this:
sed 's/.*c:\\websites\\\([0-9a-zA-Z]*\)\\.*/\1/'
There's a good sed tutorial here: Sed - An Introduction and Tutorial by Bruce Barnett
grep 方式:
测试:
grep way:
test: