Martin R. Martin R. - 16 days ago 5
Linux Question

Redirecting standard input / output to substituted command

I am trying to run a command, which can be simplified to this structure:

echo "A,B" | cp $(sed -e 's/A,//') $(sed -e 's/,B//')


But I am struggling getting the input / output of the substituted commands right.
Running the code above yields this error message:
sed: read error on stdin: Input/output error

When I run this instead:

echo "A,B" | cp <(sed -e 's/A,//') <(sed -e 's/,B//')


the command doesn’t terminate.

What am I doing wrong here?

Tim Tim
Answer

Try this:

echo "A,B" | (IN=$(cat); cp $(echo $IN | sed -e 's/A,//') $(echo $IN | sed -e 's/,B//'))

This creates a subshell and saves the stdin input to the $IN variable. Then it runs the 2 sed commands on the input and uses the outputs for the arguments for cp.

Comments