bash uncompress gz and store in variable

给你一囗甜甜゛ 提交于 2020-01-25 05:20:09

问题


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! ;)


回答1:


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"



回答2:


Use zcat:

 reads=$(zcat $file.fastq)



回答3:


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


来源:https://stackoverflow.com/questions/22177113/bash-uncompress-gz-and-store-in-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!