使用正则表达式提取命令

发布于 2024-12-12 06:43:11 字数 312 浏览 2 评论 0原文

我需要提取命令,无论我们在下一行 CMD 一词后面的 () 中看到什么。

Oct 29 08:00:01 data2 crond[14368]: (root) CMD (sh -xv /home//ste-telnet.sh > /home/hari/logs/ste-telnet$(date +'%Y_%m_%d_%h_%m').succ 2> /home/hari/logs/ste-telnet$(date +'%Y_%m_%d_%h_%m').err)

我需要为此使用正则表达式,因为 splunk 只能理解正则表达式。

I need to extract the command, whatever we see in ( ) after the word CMD in the following line.

Oct 29 08:00:01 data2 crond[14368]: (root) CMD (sh -xv /home//ste-telnet.sh > /home/hari/logs/ste-telnet$(date +'%Y_%m_%d_%h_%m').succ 2> /home/hari/logs/ste-telnet$(date +'%Y_%m_%d_%h_%m').err)

I need to use regular expression for this since splunk understands only that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

软糯酥胸 2024-12-19 06:43:11

试试这个

CMD (.*)$

如果该行的最后一个是命令(包含 CMD 的列是最后一列),这将起作用

Try this

CMD (.*)$

This will work if the last thing on the line is a command (the column containing CMD is the last column)

把昨日还给我 2024-12-19 06:43:11

这有点棘手。问题是你在那里有嵌套的括号。因此,如果您使用,

CMD \((.*)\)

您将在您的情况下得到正确的结果(实际命令将在比赛的组号 1 中),但是一旦您有多个命令(或多于一组最外层的命令),该结果就会失败。字符串中 CMD 后面的括号。

This is a bit tricky. The problem is that you're having nested parentheses there. So if you use

CMD \((.*)\)

you will get a correct result in your case (the actual command will be in the match's group number 1), but that would fail as soon as you'd have more than one command (or more than one set of outermost parentheses after CMD) in your string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文