zcat
, like cat
, works with multiple file names as arguments, up to the argument limit (on Linux, that's usually at least a thousand), so counting multiple lines in multiple files still works, assuming the fastq files are formatted in the expected way with only four lines per record:
zcat filename_R1.fastq.gz filename_R2.fastq.gz ... | wc -l
If there's a consistent file pattern, that can be used with file name glob wildcards:
zcat *.fastq.gz | wc -l
If you have a file containing a list of files, this can be processed as shell arguments as well:
zcat $(cat list_of_files.txt) | wc -l
For an unlimited number of files, something like GNU parallel can be used to feed file names from piped input. It's also a bit safer for file names containing weird characters in them:
cat list_of_files.txt | parallel zcat {} | wc -l
... but [after checking the question again] given that you want counts for individual files, the file name needs to be added as well:
cat list_of_files.txt | parallel echo {} $(zcat {} | wc -l)
The read
function provides an alternate way to do the same thing:
cat list_of_files.txt | while read fileName; do echo ${fileName} $(zcat ${fileName} | wc -l); done
Assuming you are using bash, dividing by 4 can be done via the arithmetic substitution operator, $((X))
:
cat list_of_files.txt | parallel echo {} $(( $(zcat {} | wc -l) / 4 ))
Or with read
:
cat list_of_files.txt | while read fileName; do echo ${fileName} $(( $(zcat ${fileName} | wc -l) / 4 )); done
And, finally, this can be output to a file using >
:
(cat list_of_files.txt | while read fileName; do echo ${fileName} $(( $(zcat ${fileName} | wc -l) / 4 )); done) > fastq_record_counts.txt