Lieuwe
Lieuwe

Reputation: 1840

Redirect sed output to stdin

I have a file which I currently send as an email like so

/usr/sbin/sendmail [email protected] < f1.txt

I made some changes to the file so that it includes a placeholder which I adjust using sed like so

sed -e s/PLACEHOLDER/TEST/g f1.txt

How can I combine the 2? I thought it would be done like

/usr/sbin/sendmail [email protected] <(sed -e s/PLACEHOLDER/TEST/g f1.txt)

But that 'hangs'. Can anyone point me in the right direction?

Upvotes: 3

Views: 466

Answers (1)

ghoti
ghoti

Reputation: 46876

You've almost got it.

Your command line was:

/usr/sbin/sendmail [email protected] <(sed -e s/PLACEHOLDER/TEST/g f1.txt)

You're using Process Substitution to turn the sed command inside the <( .. ) construct into a temporary file handle which will be read by sendmail. Unfortunately, you're not actually redirecting in from that temporary file handle.

The fix should simply be to add a < before the command substitution:

/usr/sbin/sendmail [email protected] < <(sed -e 's/PLACEHOLDER/TEST/g' f1.txt)
                                  ↑

That said, it might be preferable to use PIPES instead, so as to avoid the dependency on bash:

sed -e 's/PLACEHOLDER/TEST/g' f1.txt | /usr/sbin/sendmail [email protected]

The eventual effect is the same, but the process is a little simpler, and is portable to POSIX shell interpreters which do not support process substitution.

Upvotes: 8

Related Questions