gpt4 book ai didi

bash - 如何在一行中字符串替换命令的结果?

转载 作者:行者123 更新时间:2023-11-29 09:47:31 24 4
gpt4 key购买 nike

例如,假设我想在 ps aux | 的输出中用 php 替换子字符串 python grep python.

如何在一行中完成此操作?

ps aux | grep python | /python/php/
^--- totally made up

期望的输出是看到如下所示的字符串:script.pythonscript.php。这与实际文件无关。这只是关于输出。我可以很容易地说用 f 替换所有 ph

最佳答案

你甚至不需要grep。你可以只使用 sed:

ps aux | sed -n '/python/s/python/php/pg'
  • -n 将抑制正常输出
  • /python/ 将搜索带有 python
  • 的行
  • s/python/php/pg 将用 php 替换所有出现的 python 并打印替换的行

关于bash - 如何在一行中字符串替换命令的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28050061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com