Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report