本文介绍了如何在搜索模式中将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一起使用
发布评论