'Piping to Secondary Output File
Trying to pipe commands and redirect to a second output file.
Individually, I can operate the following command lines and I get the correct sorted file.
cut -c 63-69 Data.txt >T_DAILY_AVG.txt
sort <T_DAILY_AVG.txt >T_DAILY_AVG_sorted.txt
However, when I attempt to pipe the commands together, I get the correct T_DAILY_AVG.txt file but the T_DAILY_AVG_sorted.txt returns empty. This is the command line I am attempting to use.
cut -c 63-69 Data.txt >T_DAILY_AVG.txt | sort <T_DAILY_AVG.txt >T_DAILY_AVG_sorted.txt
Solution 1:[1]
You want to use tee which is the Linux program that writes the input to a file AND prints it back out to stdout.
cut -c 63-69 Data.txt | tee T_DAILY_AVG.txt | sort > T_DAILY_AVG_sorted.txt
I tested it with some dummy data to show that it works:
$ echo -e 'one\ntwo\nthree' | tee T_DAILY_AVG.txt | sort > T_DAILY_AVG_sorted.txt
$ cat T_DAILY_AVG.txt
one
two
three
$ cat T_DAILY_AVG_sorted.txt
one
three
two
Alternately, you could just run it as two separate commands. The first command writes to a file. The second command reads that file and writes to a different file. I would use && in between them so that the second command runs only if the first command succeeds.
$ echo -e 'one\ntwo\nthree' > T_DAILY_AVG.txt && sort T_DAILY_AVG.txt > T_DAILY_AVG_sorted.txt
$ cat T_DAILY_AVG.txt
one
two
three
$ cat T_DAILY_AVG_sorted.txt
one
three
two
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
