Supertech
Supertech

Reputation: 770

How to count lines in compressed file in a directory and print the number of lines per file?

I am counting lines in several files in a directory without decompressing, and dividing the result by 4 as below:

gunzip -c *.fastq.gz | echo $((`wc -l`/4))

Everything looks good, except that it's giving me the total number of all the lines. I would like to print the lines per file. Can anyone help. I am using Darwin (Mac OSX). Thank you.

Upvotes: 3

Views: 5429

Answers (2)

ChristopheD
ChristopheD

Reputation: 116207

Just make it a small script?

#!/bin/bash
for i in *.fastq.gz
do
   echo "$i" $(gunzip -c $i | echo `wc -l`/4 | bc -l)
done

If you want a one-liner:

for i in *.fastq.gz; do echo "$i" $(gunzip -c $i | echo `wc -l`/4 | bc -l); done

Upvotes: 4

twalberg
twalberg

Reputation: 62399

gzip usually comes with zcat (maybe gzcat), which is essentially gzip -dc. So this invocation should work for a single file:

echo $(( $(zcat file.gz | wc -l) / 4 ))

If for some reason you don't have zcat, gzip -dc works just fine in its place.

Wrapping that in a for loop to handle different files should be relatively straightforward...

Upvotes: 2

Related Questions