how to use xargs with sed in search pattern

前端 未结 4 1431
再見小時候
再見小時候 2020-12-29 06:36

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          


        
4条回答
  •  囚心锁ツ
    2020-12-29 07:16

    This might work for you (GNU sed):

    echo "some pattern" | sed 's|.*|s/&/replacement/g|' | sed -f - -i file.txt
    

    Essentially turn the some pattern into a sed substitution command and feed it via a pipe to another sed invocation. The last sed invocation uses the -f switch which accepts the sed commands via a file, the file in this case being the standard input -.

    If you are using bash, the here-string can be employed:

    <<<"some pattern" sed 's|.*|s/&/replacement/g|' | sed -f - -i file.txt
    

    N.B. the sed separators | and / should not be a part of some pattern otherwise the regexp will not be formed properly.

提交回复
热议问题