问题
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?
回答1:
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.
回答2:
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.
回答3:
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
)
来源:https://stackoverflow.com/questions/6355443/using-command-substitution-inside-a-sed-script-with-arguments