Count line number in linux

count lines in a file

The standard way is with wc , which takes arguments to specify what it should count (bytes, chars, words, etc.); -l is for lines:

$ wc -l file.txt 1020 file.txt 

How do I count the lines in a file if I want to ignore comments? Specifically, I want to not count lines that begin with a +, some white space (could be no white space) and then a %, which is the way comment lines appear in a git diff of a MATLAB file. I tried doing this with grep, but couldn’t figure out the correct regular expression.

@Gdalya I hope the following pipeline will do this (no tests were perfomed): cat matlab.git.diff | sed -e ‘/^\+[ ]*.*\%$/d’ | wc -l . /regexp/d deletes a line if it matches regexp , and -e turns on an adequate (IMNSHO) syntax for regexp .

@celtschk , as long as this is usual in comment lines: is it possible to modify your grep command in order to consider as comment cases like » + Hello» (note the space(s) before the + )?

@SopalajodeArrierez: Of course it is possible: grep -v ‘^ *+’ matlab.git.diff | wc -l (I’m assuming the quote signs were not actually meant to be part of the line; I also assume that both lines with and without spaces in front of the + are meant to be comments; if at least one space is mandatory, either replace the star * with \+ , or just add another space in front of the star). Probably instead of matching only spaces, you’d want to match arbitrary whitespace; for this replace the space with [[:space:]] . Note that I’ve also removed matching the % since it’s not in your example.

Источник

Count Lines in a File in Bash

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

1. Overview

Sometimes we want to check how large or small a given file is. The total number of lines of a given file helps us to know how big that file is. Linux, like any other operating system, provides us with several ways of achieving this task.

In this tutorial, we’ll look at the most common ways of counting the number of lines of a specified file using Bash.

Further reading:

How to Count Words in a File

Write to a Text File in Bash

Looping Through the Content of a File in Bash

In this tutorial, we’ll see how to loop through the contents of a file, line by line. This might sound like a trivial task, but there are some caveats that we need to be aware of.

Читайте также:  List all apps linux

2. Setup

For this quick tutorial, we’ll use a text file called programming.txt that contains some of the top programming languages in use today:

$ cat programming.txt JavaScript Java C Python C# PHP C++ Go R Ruby

If we count the number of lines manually, we’ll get 10. Counting manually will become tiresome and hard to achieve as the number of lines increases. However, getting the total number of lines can easily be achieved by using various terminal commands.

3. wc

The wc command is used to find the number of lines, characters, words, and bytes of a file.

To find the number of lines using wc, we add the -l option. This will give us the total number of lines and the name of the file.

Let’s check the number of lines of our file using the wc -l command:

$ wc -l programming.txt 10 programming.txt

From the output, we can see that it’s printed the number 10, which indicates the total number of lines, and the name of the file, programming.txt.

We can tell the shell to redirect the programming.txt file to standard input of the wc -l command. This will give us the number of lines without the filename.

Another common way to do this with wc is to use cat and pipe the file to the command:

$ cat programming.txt | wc -l 10

4. sed

sed is a stream editor that’s used to perform basic text transformation of an input file. This command is mostly used for find-and-replace functionality. We can also use it to find the number of lines of a specified file.

sed can receive different arguments in order to print the line numbers.

4.1. sed -n ‘=’

We can use the combination of sed, the -n option, and an equal sign (‘=’). The command will print the line numbers without the content of the file:

$ sed -n '=' programming.txt 1 2 3 4 5 6 7 8 9 10

From the results, we can see that the command has printed the number of lines only. However, this approach isn’t efficient for large files.

4.2. sed -n ‘$=’

Most of the time, we prefer to just get the overall number of lines. sed comes in handy by using the -n option and the ‘$=’ argument. The output of the command is the number of the last line of a file:

5. awk

The awk command treats every line as a record. The number of lines can then be printed in the END section using awk‘s built-in NR variable:

$ awk 'END < print NR >' programming.txt 10 

6. cat

The cat command concatenates the files passed to it as arguments and prints on the standard output. This is one of the most used commands. Using the cat command with the -n option prints the file contents with their line numbers:

$ cat -n programming.txt 1 JavaScript 2 Java 3 C 4 Python 5 C# 6 PHP 7 C++ 8 Go 9 R 10 Ruby

We can see that the command has printed both the line numbers and the content. Note that this approach is impractical when dealing with large files.

7. Conclusion

In this brief article, we learned several ways of counting the number of lines in a file using Bash.

Some of the commands that print the contents of the files become inefficient and impractical when working with large files.

The wc -l command is the most used, and also the easiest way to find the line numbers of a given file.

Читайте также:  Gtk темы для linux

Источник

5 Ways to Count the Number of Lines in a File

On Linux, you can do a single task in several ways. Likewise, if you want to count the number of lines in single or multiple files, you can use different commands. In this article, I’ll share five different ways including that you can use to print a total number of lines in a large file.

1. Count Number Of Lines Using wc Command

As wc stands for “word count“, it is the most suitable and easy command that has the sole purpose of counting words, characters, or lines in a file.

Let’s suppose you want to count the number of lines in a text file called distros.txt.

View File Contents

You can use «-l» or «—line» option with wc command as follows:

Count Lines in File

You can see that wc also counts the blank line and print the number of lines along with the filename. In case, you want to display only the total number of lines, you can also hide the filename by redirecting the content of the file to wc using a left-angle bracket (<) instead of passing the file as a parameter.

Print Total Lines in File

Moreover, to display a number of lines from more than one file at the same time, you need to pass the filenames as arguments separated by space.

$ wc --lines distros.txt distros.txt distros.txt

Count Lines in Multiple Files

In another way, you can also make use of the cat command to redirect the file content to the wc command as input via pipe (‘|’) .

Redirect File Content

Though it will also count the number of lines in a file, here the use of cat seems redundant.

2. Count Number Of Lines Using Awk Command

Awk is a very powerful command-line utility for text processing. If you already know awk, you can use it for several purposes including counting the number of lines in files.

However, mastering it may take time if you’re at a beginner level. Hence, if you just want to use it to count the total number of lines in a file, you can remember the following command:

Count Lines in File Using Awk

Here, NR is the number of records or say line numbers in a file being processed at the END section.

3. Count Number Of Lines Using Sed Command

Sed is a also very useful tool for filtering and editing text. More than a text stream editor, you can also use sed for counting the number of lines in a file using the command:

Count Lines in File Using Sed

Here, ‘=’ prints the current line number to standard output. So, combining it with the -n option, it counts the total number of lines in a file passed as an argument.

4. Count Number Of Lines Using Grep Command

Using yet another useful pattern search command grep, you can get the total number of lines in a file using ‘-e’ or ‘—regexp’ and ‘-c’ or ‘—count’ options.

$ grep -e "$" -c distros.txt Or $ grep -e "^" -c distros.txt

Count Lines in File Using Grep

Here, ‘$’ is a regular expression representing the end of a line and the ‘^’ start of a line. You can use either of the regular expression.

5. Count Number Of Lines Using nl and cat Commands

Instead of directly getting the total no of lines, you can also print the file content and get the total number of lines by peeking at the last line number. For such purpose, nl is a simple command to print data with numbered lines.

Print Numbering Lines in File

For large files, it does not seem like a suitable method to display all data in a terminal. So what you can also do is pipe the data to tail command to just print only some of the last numbered lines.

Читайте также:  Astra linux автозапуск скрипта

List Numbering Lines in File

Likewise, a cat command with ‘-n’ can also be used to print file content with line numbers.

$ cat -n distros.txt | tail -n1

Print File Content With Line Numbers

Conclusion

After learning five ways to count a number of lines, you must be wondering which is the best way for you? In my opinion, whether you’re a beginner or advanced user of Linux, the wc command is the easiest and fastest approach.

However, if you’re in process of learning other powerful tools like grep, awk, and sed, you can also practice these commands for such purpose to hone your skills.

Источник

How to Count lines in a file in UNIX/Linux

Question: I have a file on my Linux system having a lot of lines. How do I count the total number of lines in the file?

Using “wc -l”

There are several ways to count lines in a file. But one of the easiest and widely used way is to use “wc -l”. The wc utility displays the number of lines, words, and bytes contained in each input file, or standard input (if no file is specified) to the standard output.

So consider the file shown below:

$ cat file01.txt this is a sample file with some sample data

1. The “wc -l” command when run on this file, outputs the line count along with the filename.

$ wc -l file01.txt 5 file01.txt

2. To omit the filename from the result, use:

3. You can always provide the command output to the wc command using pipe. For example:

You can have any command here instead of cat. Output from any command can be piped to wc command to count the lines in the output.

Using awk

If you must want to use awk to find the line count, use the below awk command:

Using sed

Use the below sed command syntax to find line count using GNU sed:

Using grep

Our good old friend «grep» can also be used to count the number of lines in a file. These examples are just to let you know that there are multiple ways to count the lines without using «wc -l». But if asked I will always use «wc -l» instead of these options as it is way too easy to remember.

With GNU grep, you can use the below grep syntax:

Here is another version of grep command to find line count.

$ grep -Hc ".*" file01.txt file01.txt:5

Some more commands

Along with the above commands, its good to know some rarely used commands to find the line count in a file.

1. Use the nl command (line numbering filter) to get each line numbered. The syntax for the command is:

$ nl file01.txt 1 this is a sample 2 file 3 with 4 some sample 5 data

Not so direct way to get line count. But you can use awk or sed to get the count from last line. For example:

$ nl file01.txt | tail -1 | awk '' 5

2. You can also use vi and vim with the command «:set number» to set the number on each line as shown below. If the file is very big, you can use «Shift+G» to go to the last line and get the line count.

How to Count lines in a file in linux

3. Use the cat command with -n switch to get each line numbered. Again, here you can get the line count from the last line.

$ cat -n file01.txt 1 this is a sample 2 file 3 with 4 some sample 5 data
$ cat -n file01.txt | tail -1 | awk '' 5

4. You can also use perl one lines to find line count:

$ perl -lne 'END < print $. >' file01.txt 5

Источник

Оцените статью
Adblock
detector