如何在搜索模式中将xargs与sed一起使用 [英] how to use xargs with sed in search pattern

查看:635
本文介绍了如何在搜索模式中将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一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆