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

标签 linux sed grep

我正在尝试通过 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

我认为下面的命令会起作用..

bin/sed -n '/c:\\websites\\/,/\\/p' upload/test.log

不幸的是,通过进一步阅读,我现在明白这将通过 \ 返回包含 c:\websites 的整行,我需要知道两者之间,而不是整行。

为了更加困难,我需要匹配所有目录子路径,而不仅仅是一个特定的行,因为这是针对多个站点的。

最佳答案

您使用的范围模式不正确。您不能使用它来将命令(在本例中为 print)限制为该行的一部分,而只能限制为一定范围的行。您也不会逃避退格键。

试试这个:sed 's/.*c:\\websites\\\([0-9a-zA-Z]*\)\\.*/\1/'

这里有一个很好的 sed 教程:Sed - An Introduction and Tutorial by Bruce Barnett

关于linux - 使用 sed 从日志文件中提取目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7811237/

相关文章:

linux - 如何在 Makefile 中隐藏编译信息?

linux - linux 或 mac 终端命令可以向上滚动而不是向下滚动吗?

linux - 使用 sed 或 awk 重复提取两个字符串之间的文本?

shell - shell 脚本中的 egrep

linux - 如何通过 grep 响应找出有多少调用超时?

Linux-Ubuntu : Get the global size of all pdf's inside a directory

c++ - 静态链接库是否需要链接到它们的依赖项?

regex - 我在 GNU sed 中的前瞻正则表达式有什么问题?

linux - 使用 sed 和正则表达式替换 html 内容

ubuntu - 在 grep 中使用正则表达式元字符