在 sed 脚本中使用带有参数的命令替换

发布于 2024-11-15 21:34:03 字数 623 浏览 7 评论 0原文

我正在尝试编写一个简短的脚本,在其中使用 sed 搜索流,然后根据 shell 函数的结果对流执行替换,这需要来自 sed 的参数,例如

#!/bin/sh

function test {
    echo "running test"
    echo $1
    }

sed -n -e "s/.*\(00\).*/$(test)/p" < testfile.txt

testfile.txt 包含:(

1234
2345
3006
4567

带有换行符)在每个之间;它们会被您的网站格式删除)。好吧,该脚本对我有用(输出“正在运行测试”),但显然没有传递给测试的参数。我希望 sed 行类似于:

sed -n -e "s/.*\(00\).*/$(test \1)/p" < testfile.txt

和输出:

running test
00

以便将 sed 匹配的模式作为参数提供给测试。我真的没想到上面的方法会起作用,但我已经尝试了我能想到的 $() 括号、反引号和转义符的所有组合,并且在任何地方都找不到提到这种情况的地方。帮助?

I am trying to write a short script in which I use sed to search a stream, then perform a substitution on the stream based on the results of a shell function, which requires arguments from sed, e.g.

#!/bin/sh

function test {
    echo "running test"
    echo $1
    }

sed -n -e "s/.*\(00\).*/$(test)/p" < testfile.txt

where testfile.txt contains:

1234
2345
3006
4567

(with newlines between each; they are getting removed by your sites formatting). So ok that script works for me (output "running test"), but obviously has no arguments passed to test. I would like the sed line to be something like:

sed -n -e "s/.*\(00\).*/$(test \1)/p" < testfile.txt

and output:

running test
00

So that the pattern matched by sed is fed as an argument to test. I didn't really expect the above to work, but I have tried every combination of $() brackets, backticks, and escapes I could think of, and can find no mention of this situation anywhere. Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好多鱼好多余 2024-11-22 21:34:03

sed 不会执行命令。然而,Perl 将在正则表达式命令上使用 /e 选项。

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

如果您不想看到内嵌的 stderr 并搞砸输出,请将其重定向到 /dev/null 。

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.

做个ˇ局外人 2024-11-22 21:34:03

这可能对您有用:

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

或者如果您使用 GNU sed:

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

注意您必须记住首先导出该函数。

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.

七婞 2024-11-22 21:34:03

试试这个:

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

注意:我的正则表达式可能是错误的,但重要的是通过管道传输到 shell (sh)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文