Find the answer to your Linux question:
Results 1 to 2 of 2
Hi all, I have the following code in a shell script. Code: if [ "$3" == "gz" ] then echo "#[FILENAME_WITH_PATH]#" echo "#[ROW_COUNT]#" echo "##################################" for fn in `find "$1" ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2007
    Posts
    11

    Calculating sum


    Hi all,

    I have the following code in a shell script.

    Code:
    if [ "$3" == "gz" ]
            then
            echo "#[FILENAME_WITH_PATH]#"
            echo "#[ROW_COUNT]#"
            echo "##################################"
    
            for fn in `find "$1" -maxdepth 1 -iname \*"$2"* -type f`
                    do
    
                    echo "$fn"
                    zcat $fn | wc -l
            #       pause 'Press [Enter] key to continue...'
    
                    done
    exit 0
    which prints the following output:

    Code:
    #[FILENAME_WITH_PATH]#
    #[ROW_COUNT]#
    ##################################
    /data/EVDO_102_20130918132900.dat.gz
    107108
    /data/EVDO_102_20130918172800.dat.gz
    127657
    /data/EVDO_102_20130918163800.dat.gz
    121060
    /data/EVDO_102_20130918133000.dat.gz
    111790
    /data/EVDO_105_20130918133200.dat.gz
    76544
    The zcat command piped to 'wc -l' is producing the line count in each file in the specified directory.

    I'm trying to print the sum of all the records in all the files. That is, 107108 + 127657 + 121060 + 111790 + 76544 = 544159.

    Any inputs or thoughts on how I can achieve that would be greatly appreciated.

    Thank you!

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    630
    Code:
    sum=0
    for fn in `find "$1" -maxdepth 1 -iname \*"$2"* -type f`; do
        count=( $( wc -l <( zcat ${fn} ) ) )
        count=${count[0]}
        printf "%-10s %s\n" "${count}" "${fn}"
        sum=$(( sum + count ))
    done
    printf "%-10s  total\n" "${sum}"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •