2

I have a really long command that runs over a huge file and I am forced to run it twice which doubles the time it takes to run.

This is what I am doing at the moment:

x=$(command | sort -u)
y=$(command | sort -n)

I was wondering whether there is any way to redirect the output of command to both sort -u and sort -n and store output of each into separate variables or files like I did above with x and y.

I tried to use tee to do the following but no luck:

command | tee >(sort -n > x.txt) >(sort -u > y.txt)

I tried to redirect output to text files but it just printed it to standard output instead.

Any tips or ideas?

3
  • 1
    are you sure it didn't print to standard output as well rather than instead? Commented Oct 27, 2020 at 19:48
  • Oh you're right. I don't want it to print it to standard output though Commented Oct 27, 2020 at 19:49
  • You could just discard standard output by redirecting it to /dev/null Commented Oct 27, 2020 at 19:51

1 Answer 1

3

You're really close

command | tee >(sort -n >/tmp/x.txt) | sort -u >/tmp/y.txt

In case you're curious, the >( ... ) action gets replaced with a path to the descriptor pipe (eg /dev/fd/1234), so the resulting pipeline has this shape, with /dev/fd/1234 being attached to the other sort command

command | tee /dev/fd/1234 | sort > /path/to/elsewhere

To capture sorted output into a variable (from comment, not from question)

var=$(command | tee >(sort -n > x.txt) >(sort -u > y.txt) | sort -u)
7
  • Thank you so much for your response!! Do you know if I could redirect output to a variable as well or just a file? Rest of my code works with variables so it'll make my life easier haha. Commented Oct 27, 2020 at 19:52
  • What would you want put into the variable? Commented Oct 27, 2020 at 19:52
  • The output of sort -n and sort -u. Above you redirected it to text files but I was wondering if there is a way to store it into a variable instead perhaps. Commented Oct 27, 2020 at 19:54
  • You can put the output of one of those streams into a variable but not both simultaneously. Revert to the original code in your question and capture the output v="$( your_command )" Commented Oct 27, 2020 at 19:56
  • Makes sense! with your original solution, I get /dev/fd/63: Permission denied. Any ideas on how to fix this? Commented Oct 27, 2020 at 19:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .