When I'm coding ...
How big are you guys creating files? How big is the existing file? I want to know the number of lines in each file.
I think there is.
For studying, I decided to get it with a command!
I installed Linux (Ubuntu) on windows10 using WSL and commanded it.
use wc command As below wc If you describe it as a target file
$ wc *.txt
13 13 38 t01.txt
38 33 106 t02.txt
4 3 7 t03.txt
21 18 43 t04.txt
75 33 116 t05Service.txt
18 16 35 t06Service.txt
148 14 171 t07Service.txt
317 130 516 total
You can get the information of the txt file that exists in the current directory. The acquired information is displayed for each item.
Number of lines Number of words File size (number of bytes))File name
13 13 38 t01.txt
...
...
317 130 516 total here is the total
Since there is a lot of extra information at this rate Get only the number of lines with the -l option
~/work$ wc *.txt -l
13 t01.txt
38 t02.txt
4 t03.txt
21 t04.txt
75 t05Service.txt
18 t06Service.txt
148 t07Service.txt
317 total
It's much easier to see because it's just the number of lines and the file name.
I used the wc command pipe'|' sort command.
$ wc *.txt -l | sort
4 t03.txt
13 t01.txt
18 t06Service.txt
21 t04.txt
38 t02.txt
75 t05Service.txt
148 t07Service.txt
317 total
It was nicely arranged in the order of the number of file lines.
Next, I will try to get only the "*** Service.txt" file.
$ wc *Service.txt -l | sort
18 t06Service.txt
75 t05Service.txt
148 t07Service.txt
241 total
Hooray! I was able to get only the number of lines in the service file.
・ ・ ・: Relaxed:
First, delete the TOTAL line because it is an obstacle.
I used the sed command. This represents the last line when $ is specified in the address part, so d (delete) is executed for it.
$ wc *.txt -l | sort | sed '$d'
4 t03.txt
13 t01.txt
18 t06Service.txt
21 t04.txt
38 t02.txt
75 t05Service.txt
148 t07Service.txt
It has disappeared! Furthermore Use the awk command to display the average.
$ wc *.txt -l | sort | sed '$d' | awk '{n += $1}{i +=1 }; END{print int(n/i) }'
45
I got the average number of lines "45" from the above command! !! : rolling_eyes:
By the way, what is awk doing? .. ..
Add the number of lines to n for each line to i+1 When all lines are finished, display: Decimal point truncation (n ÷ i)
awk '{n += $1} {i +=1 }; END {print int(n/i) }'
It's like
-l
Show the number of lines in the file.
-c
Display the number of bytes in the file.
-m
Display the number of characters in the file.
-L
Display the number of bytes in the longest line.
-w
Display the number of words in the file.
$ ls -1 | wc -l
The option of the ls command is "-1(Number 1)」
The option of the wc command is "l (lowercase letter L)"
reference What if I want to erase only the last line of text?
[Note about awk] (https://qiita.com/SYutaka/items/b6aadfa279c516a3b90b)
Recommended Posts