[LINUX] I wanted to know the number of lines in multiple files, so I tried to get it with a command

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!

Execution environment

I installed Linux (Ubuntu) on windows10 using WSL and commanded it.

Get row

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.

Displayed in line order.

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:

Now display the average!

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

The options of the wc command are as follows.

-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.

I didn't use it, but it seems that you can check the number of files with the following command.

$ 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

I wanted to know the number of lines in multiple files, so I tried to get it with a command
How to get a list of files in the same directory with python
In IPython, when I tried to see the value, it was a generator, so I came up with it when I was frustrated.
I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python
I tried to create a Python script to get the value of a cell in Microsoft Excel
I tried to predict the number of domestically infected people of the new corona with a mathematical model
[Linux] Command to get a list of commands executed in the past
I tried to make a calculator with Tkinter so I will write it
I tried to get the authentication code of Qiita API with Python.
I tried to display the altitude value of DTM in a graph
[Python] Get the files in a folder with Python
I failed to install django with pip, so a reminder of the solution
I made an appdo command to execute a command in the context of the app
I want to see a list of WebDAV files in the Requests module
I didn't understand the Resize of TensorFlow so I tried to summarize it visually.
I tried to create a model with the sample of Amazon SageMaker Autopilot
I wanted to use the find module of Ansible2, but it took some time, so make a note
How to get the number of digits in Python
[Python & SQLite] I tried to analyze the expected value of a race with horses in the 1x win range ①
I tried to predict the number of people infected with coronavirus in Japan by the method of the latest paper in China
I tried to predict the number of people infected with coronavirus in consideration of the effect of refraining from going out
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
How to identify the element with the smallest number of characters in a Python list?
I tried to create a class to search files with Python's Glob method in VBA
I wanted to operate google spread sheet with AWS lambda, so I tried it [Part 2]
I tried to find the entropy of the image with python
I tried to get the location information of Odakyu Bus
I tried to find the average of the sequence with TensorFlow
Get UNIXTIME at the beginning of today with a command
[Command] Command to get a list of files containing double-byte characters
I tried "How to get a method decorated in Python"
I tried to get started with Hy ・ Define a class
I want to know the legend of the IT technology world
I wanted to delete multiple objects in s3 with boto3
I wanted to run the motor with Raspberry Pi, so I tried using Waveshare's Motor Driver Board
I made a class to get the analysis result by MeCab in ndarray with python
Recursively get the Excel list in a specific folder with python and write it to Excel.
I made a function to crop the image of python openCV, so please use it.
There was a doppelganger, so I tried to distinguish it with artificial intelligence (laughs) (Part 1)
[Graph drawing] I tried to write a bar graph of multiple series with matplotlib and seaborn
I tried to find the trend of the number of ships in Tokyo Bay from satellite images.
I don't like to be frustrated with the release of Pokemon Go, so I made a script to detect the release and tweet it
I tried to describe the traffic in real time with WebSocket
[Linux] I tried to summarize the command of resource confirmation system
I tried to get a database of horse racing using Pandas
I tried to automate the watering of the planter with Raspberry Pi
[Python] A memo that I tried to get started with asyncio
[Introduction to StyleGAN] I played with "The Life of a Man" ♬
I tried to create a list of prime numbers with python
I tried to get a list of AMI Names using Boto3
I tried to get started with Bitcoin Systre on the weekend
How to get the vertex coordinates of a feature in ArcPy
I wanted to solve the ABC164 A ~ D problem with Python
I tried to process the image in "pencil style" with OpenCV
I tried to expand the size of the logical volume with LVM
Create a function to get the contents of the database in Go
I made a command to display a colorful calendar in the terminal
Get a list of files in a folder with python without a path
Get the number of readers of a treatise on Mendeley in Python
PhytoMine-I tried to get the genetic information of plants with Python
I tried to make a mechanism of exclusive control with Go