使用 sed 从日志文件中提取目录

发布于 2024-12-11 04:26:03 字数 625 浏览 0 评论 0原文

我正在尝试解析 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

儭儭莪哋寶赑 2024-12-18 04:26:03

您错误地使用了范围模式。您不能使用它来将命令(在本例中为打印)限制为行的一部分,而只能限制为行的范围。你也无法逃脱退格键。

试试这个: 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

清欢 2024-12-18 04:26:03

grep 方式:

grep -Po "(?<=c:\\\websites\\\)[^\\\]+(?=\\\)" yourFile

测试:

kent$  echo '"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 '' "'|grep -Po "(?<=c:\\\websites\\\)[^\\\]+(?=\\\)"
pj7fe4

grep way:

grep -Po "(?<=c:\\\websites\\\)[^\\\]+(?=\\\)" yourFile

test:

kent$  echo '"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 '' "'|grep -Po "(?<=c:\\\websites\\\)[^\\\]+(?=\\\)"
pj7fe4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文