【问题标题】:strace output extract file names from double quotesstrace 输出从双引号中提取文件名
【发布时间】:2014-12-15 14:01:25
【问题描述】:
open("/etc/ld.so.cache", O_RDONLY)      = 3
open("/lib/libselinux.so.1", O_RDONLY)  = 3
open("/lib/librt.so.1", O_RDONLY)       = 3
open("/lib/libacl.so.1", O_RDONLY)      = 3
open("/lib/libc.so.6", O_RDONLY)        = 3
open("/lib/libdl.so.2", O_RDONLY)       = 3
open("/lib/libpthread.so.0", O_RDONLY)  = 3
open("/lib/libattr.so.1", O_RDONLY)     = 3
......................

以上几行是带有文件选项的 strace 命令的典型输出。

我想从上面的输出中过滤掉双引号内的文件名(带/不带目录)...例如我的要求是获得以下列表。

/etc/ld.so.cache
/lib/libselinux.so.1
 ..........

如何在 linux 中使用正则表达式处理工具(grep / sed / awk / etc..)获取上述过滤掉的文件名?

【问题讨论】:

  • 你用的是什么正则表达式工具?
  • 我知道grep的初步用法。

标签: regex filter output strace


【解决方案1】:

你可以使用这个grep

grep -oP '"\K[^\n"]+(?=")' file
/etc/ld.so.cache
/lib/libselinux.so.1
/lib/librt.so.1
/lib/libacl.so.1
/lib/libc.so.6
/lib/libdl.so.2
/lib/libpthread.so.0
/lib/libattr.so.1

此正则表达式搜索双引号,然后使用\K 重置匹配结果。它使用 [^\n"]+ 后跟前瞻 (?=") 来查找双引号之间的文本,以确保另一侧有匹配的双引号。

RegEx Demo

【讨论】:

【解决方案2】:

简单实用:

$ strace -f -e trace=file  true 2>&1 | awk -F'"' '{print $2}'
/usr/bin/true
/etc/ld.so.preload
/etc/ld.so.cache
/usr/lib/libc.so.6

至于strace 命令的输出由双引号中的文件组成,我告诉(带有-F)将其用作分隔符,然后根据拆分的字符串打印第二列每一行。

【讨论】:

  • 感谢斯普特尼克!能简单介绍一下过滤部分吗?
【解决方案3】:

你可以使用grep,例如

grep ^open in.txt | grep -o '".\+[^"]"'

要过滤掉双引号,请添加tr -d '"'

这里是带有strace 的单行代码(将php 更改为您的命令名称):

strace -fe trace=file -p $(pgrep -n php) 2>&1 | grep ^stat | grep -o '".\+[^"]"'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-16
    相关资源
    最近更新 更多