Use bash scripts to calculate the number of lines of code

Source: Internet
Author: User
Obtain the number of lines of a single file

File:Test1.sh

Number of rows:20

Method 1

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

:

Method 2

Awk 'end {print Nr} 'test1.sh

:

Method 3

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

:

Method 4

Sed-n' $ = 'test1.sh

:

Method 5

WC-l test1.sh

:

Method 6

# Cat test1.sh | WC-l

:

Obtains the number of rows of all objects in a specified directory.

CodeAs follows:

#! /Bin/ Bashfilescount = 0  Linescount = 0  Function  Funcount (){  For   File   In ' Ls $ 1  '  Do          If [-D $ 1  "  /  " $ File ];Then  Funcount $  1  "  /  " $ File          Else  Declare - I 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 " 

Usage:

1For the local directory

./Count. Sh

2Statistics on multiple directories

./Count. sh/tmp ~

The running effect is as follows:

Obtains the number of rows of files with a specified extension in a specified directory.

The Code is as follows:

#! /Bin/ Bashextens = ( "  . 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 =' 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  " 

Usage:

1For the local directory

./Count. Sh

2Statistics on multiple directories

./Count. sh/tmp ~

The running effect is as follows:

well, that's all. I hope it will help you.

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.