5

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

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Perhaps you're right. That's so sad though, so many magical things could be done if only sed would execute commands.
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.

Comments

0

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)

1 Comment

I'm afraid I don't understand what you are trying to do here, sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.