English 中文(简体)
利用纸面上的指挥替换,提出论点
原标题:using command substitution inside a sed script, with arguments

我试图写一个短篇的文字,在短文中,我用来搜索一个溪流,然后根据弹道功能的结果,在溪流上进行替代,这就需要争论,比如说。

#!/bin/sh

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

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

试验文件。 注

1234
2345
3006
4567

(每条新线之间有新线;它们正在被贵点清除。) So ok that script work for me (output "running test”),但显然没有经过任何论证。 我愿这样说:

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

产出:

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?

问题回答

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.

这或许有助于你:

tester () { echo "running test"; echo $1; }
export -f tester
echo -e "1234
2345
3006
4567" |
sed -n  s/.*(00).*/echo "$(tester 1)"/p  | sh
running test
00

或者,如果你使用德国国籍联盟的话:

echo -e "1234
2345
3006
4567" |
sed -n  s/.*(00).*/echo "$(tester 1)"/ep 
running test
00

N.B. 你们必须记住首先出口这一职能。

try this:

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

注:我可能有失职之处,但重要的仲裁范围正在ll上。





相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签