bash uncompress gz and store in variable

2019-07-24 23:19发布

I need to uncompress a .gz file and store it in a variable, so I can use it later. So, the idea is that I generate *.fastq.gz files, and I need to uncompress them and keep just the *.fastq file. Then, I would like to store its name in a variable, so I can call the file for further processing.

Here, there is the code I am executing: input: $file.fastq.gz Where $file is the name of the file (it changes, as this code is inside a loop)

reads=$(gunzip $file.fastq)
echo $reads

Does anybody know what is wrong with this code? Why it does not produce any output and the program stays in that point? Thank you very much! ;)

3条回答
干净又极端
2楼-- · 2019-07-24 23:58

1) reads=$(gunzip $file.fastq) <--- first you should be doing your gunzip on the .gz file

2) echo $reads - You cannot store the uncompressed file in the variable .. so you cannot expect that the variable reads would have the name of the uncompressed file.

You should rather be using

gunzip $file.fastq.gz
if [[ $? -eq 0 ]]
then 
    reads="$file.fastq"
fi

Or a shorter syntax as suggested by Charles

if gunzip $file.fastq.gz
then 
    reads="$file.fastq"
fi
查看更多
\"骚年 ilove
3楼-- · 2019-07-25 00:00

If the input file is $file.fastq.gz, the resulting output file is just that file with the .gz extension removed.

gunzip "$file.fastq.gz" & gunzip_pid=$!
reads="$file.fastq"
# Do some more work that doesn't depend on the contents of $file.fastq
# ...
wait $gunzip_pid || { echo "Problem with gunzip"; exit; }
# Do something with the now-complete $file.fastq here

(Original answer to misinterpreted question, saved as a useful non-sequitor.)

You need to tell gunzip to write the uncompressed stream to standard output, rather than uncompressing the file in-place.

reads=$(gunzip -c "$file.fastq.gz") || { echo "Problem with gunzip; exit; }
echo "$reads"
查看更多
Anthone
4楼-- · 2019-07-25 00:10

Use zcat:

 reads=$(zcat $file.fastq)
查看更多
登录 后发表回答