使用GREP/SED/等提取括号?

发布于 2025-01-26 15:17:00 字数 606 浏览 5 评论 0原文

我正在尝试使用以下管道命令来获取PHP-FPM的文件位置:

ps aux | grep php-fpm  | grep master

结果:

root        80  0.0  0.6 334912 26072 ?        Ss   14:18   0:00 php-fpm: master process (/root/.symfony/php/2b1b88a9c9ed1cd796c66569d386096e96bbe2bc/fpm-7.3.33.ini)

但是我仍然需要在支架之间提取文件路径,无法完全弄清楚,输入字符串正在抱怨非法令牌..

RESULT_FROM_ABOVE | grep -o '^[\(^.\)]*'

bash:语法错误接近意外令牌`('

这是不起作用的,但应该给出我要实现的目标的想法。

我需要在我试图应用的最后一个GREP之前逃脱result_from_above t找不到解决方案

I am trying to obtain a file location for PHP-FPM, using the following piped commands works:

ps aux | grep php-fpm  | grep master

Results:

root        80  0.0  0.6 334912 26072 ?        Ss   14:18   0:00 php-fpm: master process (/root/.symfony/php/2b1b88a9c9ed1cd796c66569d386096e96bbe2bc/fpm-7.3.33.ini)

However I still need to extract the file path between the brackets, can't quite figure it out, the input string is complaining about illegal tokens..

RESULT_FROM_ABOVE | grep -o '^[\(^.\)]*'

bash: syntax error near unexpected token `('

This doesn't work but should give the gist of the idea of what I am trying to achieve.

I need to escape the RESULT_FROM_ABOVE before the final grep I am trying to apply and I can't find any solution.

Thoughts?

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

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

发布评论

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

评论(1

无所谓啦 2025-02-02 15:17:00

设置一个变量来模拟PS/GREP/GREPpgg_out)和psps_out> ps_out)输出:

$ pgg_out='root 80 0.0 0.6 334912 26072 ? Ss 14:18 0:00 php-fpm: master process (/root/.symfony/php/2b1b88a9c9ed1cd796c66569d386096e96bbe2bc/fpm-7.3.33.ini)'
$ ps_out='
some stuff to ignore
root 80 0.0 0.6 334912 26072 ? Ss 14:18 0:00 php-fpm: master process (/root/.symfony/php/2b1b88a9c9ed1cd796c66569d386096e96bbe2bc/fpm-7.3.33.ini)
more stuff to ignore'

将当前PS/GREP/GREP输出到sed

$ echo "${pgg_out}" | sed -En 's/.*\((.*)\)/\1/p'
/root/.symfony/php/2b1b88a9c9ed1cd796c66569d386096e96bbe2bc/fpm-7.3.33.ini

消除OP的当前GREP调用并输送PS输出输出到awk

$ echo "${ps_out}" | awk -F'[()]' '/master/ && /php-fpm/ {print $2}'
/root/.symfony/php/2b1b88a9c9ed1cd796c66569d386096e96bbe2bc/fpm-7.3.33.ini

Setup a couple variables to simulate the ps/grep/grep (pgg_out) and ps (ps_out) output:

$ pgg_out='root 80 0.0 0.6 334912 26072 ? Ss 14:18 0:00 php-fpm: master process (/root/.symfony/php/2b1b88a9c9ed1cd796c66569d386096e96bbe2bc/fpm-7.3.33.ini)'
$ ps_out='
some stuff to ignore
root 80 0.0 0.6 334912 26072 ? Ss 14:18 0:00 php-fpm: master process (/root/.symfony/php/2b1b88a9c9ed1cd796c66569d386096e96bbe2bc/fpm-7.3.33.ini)
more stuff to ignore'

Piping the current ps/grep/grep output to sed:

$ echo "${pgg_out}" | sed -En 's/.*\((.*)\)/\1/p'
/root/.symfony/php/2b1b88a9c9ed1cd796c66569d386096e96bbe2bc/fpm-7.3.33.ini

Eliminating OP's current grep calls and piping the ps output to awk:

$ echo "${ps_out}" | awk -F'[()]' '/master/ && /php-fpm/ {print $2}'
/root/.symfony/php/2b1b88a9c9ed1cd796c66569d386096e96bbe2bc/fpm-7.3.33.ini
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文