bash 在目录上递归计算 .gz 文件的行数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22064932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:43:36  来源:igfitidea点击:

Count number of lines of .gz files recursively on directory?

bashshellunix

提问by Umesh K

I have hundreds of directories and each directory contains .gzfile and I want to count lines of that .gzfile.

我有数百个目录,每个目录都包含.gz文件,我想计算该.gz文件的行数。

I tried the following one liner to achieve this but it does not work says gzip: stdin: unexpected end of file

我尝试了以下一个班轮来实现这一点,但它不起作用说 gzip:stdin:unexpected end of file

find . type -f -name '*.data.gz' | gunzip -c  | wc -l 

for e.g. if there are two directories with one .gz file each, and suppose each has 100 lines, then I want 200 total lines a output.

例如,如果有两个目录,每个目录都有一个 .gz 文件,并且假设每个目录有 100 行,那么我希望输出总共 200 行。

回答by MarcoS

You were quite there...:

你在那里...:

find . -type f -name '*.gz' | xargs zcat | wc -l

回答by Spike Hodge

gzip -dc *.gz | wc -l

-d decompress -c to STDOUT (not to disk)

-d 解压 -c 到 STDOUT(不是到磁盘)

or

或者

gzip -dcr * | wc -l

-d decompress

-d 解压

-c to STDOUT (not to disk)

-c 到 STDOUT(不是到磁盘)

-r recursive (look in directorties)

-r 递归(查看目录)

回答by Alex Jurado - Bitendian

my directory:

我的目录:

.
├── a.gz
├── b.gz
└── t
    └── f.gz

command to echo and count lines of every gz file found:

命令回显和计算找到的每个 gz 文件的行数:

find . -type f -name '*.gz' -exec bash -c 'echo ;gunzip -c  | wc -l' dummy {} \;

output:

输出:

./a.gz
5
./b.gz
6
./t/f.gz
3

then, in order to obtain a grand total:

然后,为了获得总计:

echo $((`find . -type f -name '*.gz' -exec bash -c 'gunzip -c  | wc -l' dummy {} \;  | paste -sd+`))

output:

输出:

14

回答by akm

You may use this code snippet.

您可以使用此代码片段。

#!/bin/bash

gzfiles=`find . -name '*.data.gz'`

total_line_count=0
if [ $? -eq 0 ]; then
        echo "Found at:"
        for file in $gzfiles
                do
                        echo " - $file"
                        fl_line_count=`gunzip $file -c | wc -l`
                        total_line_count=`expr $total_line_count + $fl_line_count`
                done
        else
                echo "No GZIP file found :("
fi

echo "Line count: $total_line_count"