Get the App
SLTechnology News&Howtos  ›  Development  › 

What methods are there for Shell scripts to count the number of file lines?

Shulou Source: shulou.com Published: 2022-06-03 09:21:27 10月24日 Update

This article mainly explains the "which Shell scripts count the number of file lines of the method", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "which Shell scripts count the number of file lines" bar!

Get the number of lines in a single file

File: test1.sh

Line count: 20

Method one

The code is as follows:

Awk'{print NR} 'test1.sh | tail-N1

As shown in the figure:

Method two

The code is as follows:

Awk 'END {print NR}' test1.sh

As shown in the figure:

Method three

The code is as follows:

Grep-n "" test1.sh | awk-F:'{print'} | tail-N1

As shown in the figure:

Method 4

The code is as follows:

Sed-n'$= 'test1.sh

As shown in the figure:

Method five

The code is as follows:

Wc-l test1.sh

As shown in the figure:

Method 6

The code is as follows:

# cat test1.sh | wc-l

As shown in the figure:

Gets the number of lines of all files in a specific directory

The code is as follows:

#! / bin/bash

FilesCount=0

LinesCount=0

Function funCount ()

{

For file in `ls $1`

Do

If [- d $1 "/" $file]; then

FunCount $1 "/" $file

Else

Declare-I fileLines

FileLines= `sed-n'$='$1 "/" $file`

Let linesCount=$linesCount+$fileLines

Let filesCount=$filesCount+1

Fi

Done

}

If [$#-gt 0]; then

For m_dir in $@

Do

FunCount $m_dir

Done

Else

FunCount "."

Fi

Echo "filesCount = $filesCount"

Echo "linesCount = $linesCount"

How to use it:

1. For this catalog

The code is as follows:

. / count.sh

2. Count multiple directories

The code is as follows:

. / count.sh / tmp ~

The running effect is as follows:

Gets the number of lines of a specific directory-specific extension file

The code is as follows:

#! / bin/bash

Extens= (".c".cpp".h".hpp")

FilesCount=0

LinesCount=0

Function funCount ()

{

For file in `ls $1`

Do

If [- d $1 "/" $file]; then

FunCount $1 "/" $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 $@

Do

FunCount $m_dir

Done

Else

FunCount "."

Fi

Echo "filesCount = $filesCount"

Echo "linesCount = $linesCount"

How to use it:

1. For this catalog

The code is as follows:

. / count.sh

2. Count multiple directories

The code is as follows:

. / count.sh / tmp ~

The running effect is as follows:

Thank you for your reading, the above is the content of "what Shell scripts count the number of file lines", after the study of this article, I believe you have a deeper understanding of what Shell scripts count the number of file lines, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Methods files code statistics directories such as pictures scripts learning usage content multiple effects running generation single that is ideas situations extensions articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS Redmi Shulou Technology NVidia