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:
As shown in the figure:
Method Five
Copy Code code as follows:
As shown in the figure:
Method Six
Copy Code code as follows:
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:
2. Count Multiple Catalogs
Copy Code code as follows:
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:
2. Count Multiple Catalogs
Copy Code code as follows:
The operation effect is as follows:
Well, that's all, I hope to help you.