using command substitution inside a sed script, with arguments

感情迁移 提交于 2019-12-05 15:32:50

Sed won't execute commands. Perl will, however, with the /e option on a regex command.

perl -pe 'sub testit { print STDERR "running test"; return @_[0]; }; s/.*(00).*/testit($1)/e' <testfile.txt

Redirect stderr to /dev/null if you don't want to see it in-line and screw up the output.

This might work for you:

tester () { echo "running test"; echo $1; }
export -f tester
echo -e "1234\n2345\n3006\n4567" |
sed -n 's/.*\(00\).*/echo "$(tester \1)"/p' | sh
running test
00

Or if your using GNU sed:

echo -e "1234\n2345\n3006\n4567" |
sed -n 's/.*\(00\).*/echo "$(tester \1)"/ep'
running test
00

N.B. You must remember to export the function first.

try this:

sed -n -e "s/.*\(00\).*/$1$2/p" testfile.txt | sh

Note: I might have the regex wrong, but the important bit is piping to shell (sh)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!