I have to process a file using my Linux machine.
When I try to write my output to a csv file then gzip it in the same line of script:
processing > output.csv | gzip -f output.csv
I get an 'unexpected end of file' error. Even when I download the file using the Linux machine I get the same error.
When I do not gzip via terminal (or in a single line) everything works fine.
Why does it fail like this when the commands are all in a single line?
2 Answers
You should remove > output.csv
You can either:
- Use a pipe:
|or: - Redirect to a file
For the same stream (stdout)
You can redirect errors from stderr to a file with 2>errors.txt or they will display on screen
When you redirect a process' IO with the > operator, its output cannot be used by a pipe afterwards (because there's no "output" anymore to be piped). You have two options:
processing > output.csv && gzip output.csvWrites the unprocessed output of your program to the file
output.csvand then in a second task gzips this file, replacing it withoutput.gz. Depending on the amount of data, this might not be feasible (storage reqirements are the full uncompressed output PLUS the compressed size)processing | gzip > output.csv.gzThis will compress the output of your process in-line and write it directly to the output file, without storing the uncompressed output in an intermediate file.