如何在搜索模式中将xargs与sed一起使用

编程入门 行业动态 更新时间:2024-10-28 10:34:51
本文介绍了如何在搜索模式中将xargs与sed一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要使用命令的输出作为sed中的搜索模式.我将使用echo做一个例子,但假设这可能是一个更复杂的命令:

I need to use the output of a command as a search pattern in sed. I will make an example using echo, but assume that can be a more complicated command:

echo "some pattern" | xargs sed -i 's/{}/replacement/g' file.txt

该命令不起作用,因为某些模式"具有空格,但是我认为这清楚地说明了我的问题.

That command doesn't work because "some pattern" has a whitespace, but I think that clearly illustrate my problem.

如何使该命令起作用?

预先感谢

推荐答案

请改用命令替换,因此您的示例如下所示:

Use command substitution instead, so your example would look like:

sed -i "s/$(echo "some pattern")/replacement/g" file.txt

双引号允许命令替换在防止空格分割的同时起作用.

The double quotes allow for the command substitution to work while preventing spaces from being split.

更多推荐

如何在搜索模式中将xargs与sed一起使用

本文发布于:2023-11-25 05:02:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628437.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   模式   如何在   sed   xargs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!