8 ways to count the number of rows in a shell script statistic file _linux Shell

Source: Internet
Author: User

Get the number of rows in a single file

File: test1.sh

Number of rows: 20

Method One

Copy Code code as follows:

awk ' {print NR} ' test1.sh|tail-n1

As shown in the figure:

Method Two

Copy Code code as follows:

awk ' End{print NR} ' test1.sh

As shown in the figure:

Method Three

Copy Code code as follows:

Grep-n "" Test1.sh|awk-f: ' {print '}|tail-n1

As shown in the figure:

Method Four

Copy Code code as follows:

Sed-n ' $= ' test1.sh

As shown in the figure:

Method Five

Copy Code code as follows:

Wc-l test1.sh

As shown in the figure:

Method Six

Copy Code code as follows:

#cat test1.sh |wc-l

As shown in the figure:

Get the number of rows for all files in a particular directory

The code is as follows:

Copy Code code as follows:

#!/bin/bash

Filescount=0
Linescount=0
function Funcount ()
{
For file in ' ls $ '
Todo
If [-D $/$file];then
Funcount "/" $file
Else
Declare-i Filelines
filelines= ' sed-n ' $= ' $ '/' $file '
Let linescount= $linesCount + $fileLines
Let filescount= $filesCount +1
Fi
Done
}

If [$#-gt 0];then
For M_dir in $@
Todo
Funcount $m _dir
Done
Else
Funcount "."
Fi
echo "Filescount = $filesCount"
echo "Linescount = $linesCount"

How to use:

1, for this directory

Copy Code code as follows:

./count.sh

2. Count Multiple Catalogs
Copy Code code as follows:

./count.sh/tmp ~

The operation effect is as follows:

To get the number of rows for a specific file of the specified name extension
The code is as follows:

Copy Code code as follows:

#!/bin/bash

Extens= (". C" ". cpp" ". H" ". hpp")

Filescount=0
Linescount=0
function Funcount ()
{
For file in ' ls $ '
Todo
If [-D $/$file];then
Funcount "/" $file
Else
Filename=$1 "/" $file

Extension= "." ${filename##*.}
echo "FileName = $fileName Extension = $EXTENSION"

            if [[${extens[@]/$EXTENSION/} "!=" ${extens [@]}" ]]; Then
                declare-i Filelines
                filelines= ' sed-n ' $= ' $fileName '
                 echo $fileName ":" $fileLines
                 let linescount= $linesCount + $fileLines
                 let filescount= $filesCount +1
             fi
        fi
     Done
}

If [$#-gt 0];then
For M_dir in $@
Todo
Funcount $m _dir
Done
Else
Funcount "."
Fi
echo "Filescount = $filesCount"
echo "Linescount = $linesCount"

How to use:

1, for this directory

Copy Code code as follows:

./count.sh

2. Count Multiple Catalogs
Copy Code code as follows:

./count.sh/tmp ~

The operation effect is as follows:

Well, that's all, I hope to help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.