Reuben L.
Reuben L.

Reputation: 2859

Rewrite sed command to edit stream instead of file

In a normal case, sed can edit the stream from another command simply by piping:

./somecommand | sed 's/xx/yy/g'

However, the sed command I'm using is a little complex. Previously, I had gotten help with optimizing sed for a specific use case: Optimize shell script for multiple sed replacements

The eventual outcome was that for my use case, the following sed command was the most optimal:

./somecommand > file
sed -e 's/^/s|/; s/$/|g/;' replacement_list | sed -r -f - -i file

Basically, the first sed command creates a list of sed operations from a file with pairs of substitions. The second sed command then uses the stream via piping and utilises the -f option to process the file.

Things have changed a little since and now I've manage to get somecommand to output results in stdout instead of writing to a file, but I can't think of a way to rewrite the above set of commands correctly. Part of the difficulty is that the second sed command is already using the stream from the first sed command.

One thing I've tried is to assign the stdout of somecommand to a variable and attempt to <<< it into the second sed command. However, that didn't work.

Upvotes: 0

Views: 540

Answers (1)

randomusername
randomusername

Reputation: 8105

You can get around this by using inline expansion of the shell (I think that's what it's called, correct me if I'm wrong).

./somecommand | sed -r -e "`sed -e 's/^/s|/; s/$/|g/;' replacement_list`"

Note the backticks inside the double quotes, this will cause the stdout of the inner sed command to be placed inside the double quotes.

Although, this is dangerous so you might want to try saving the output of the inner sed command to a shell variable first like this

sedcommand="`sed -e 's/^/s|/; s/$/|g/;' replacement_list`"
./somecommand | sed -r -e "$sedcommand"

Upvotes: 1

Related Questions