- count lines in a file
- 8 Ways to Count Lines in a File in Linux
- The concept of Data Streams and Piping
- Data streams
- Piping
- Ways to Count Lines in a File in Linux
- WC
- How to Count lines in a file in UNIX/Linux
- Using “wc -l”
- Using awk
- Using sed
- Using grep
- Some more commands
- 5 Ways to Count the Number of Lines in a File
- 1. Count Number Of Lines Using wc Command
- 2. Count Number Of Lines Using Awk Command
- 3. Count Number Of Lines Using Sed Command
- 4. Count Number Of Lines Using Grep Command
- 5. Count Number Of Lines Using nl and cat Commands
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.
8 Ways to Count Lines in a File in Linux
Counting lines in a Linux file can be hectic if you don’t know the applicable commands and how to combine them. This tutorial makes the process comfortable by walking you through eight typical commands to count lines in a file in Linux.
For example, the word count, wc , command’s primary role, as the name suggests, is to count words. However, since a group of words forms a line, you can use the command to count lines besides characters and words.
All you do is redirect the input of a file to the command alongside the -l flag.
Apart from the wc , you can use the awk, sed, grep, nl , pr , cat and perl commands. Before that, it would help to understand data streams and piping in Linux.
Table of Contents
The concept of Data Streams and Piping
Data streams
Three files come together to complete the request when you run a command: standard input, standard output, and error files.
The standard input, abbreviated as stdin and redirected as < , feeds the computer with data. The standard output, abbreviated as stdout and redirected as >, shows the result of running a command. If an error occurs when processing the result, we see the standard error, often abbreviated as stderr .
The primary stdin is the keyboard, while the stdout is the (monitor) screen. However, due to the flexibility of Linux and the fact that everything in Linux is a file, we can change the stdin , stdout , or stderr to suit our needs, as you will see when counting lines with the wc command.
Before that, you should understand the concept of piping in Linux.
Piping
Piping in Linux, denoted by | , means running two or more commands simultaneously on the terminal. For example, we can cat a file, let’s call the file index.txt . But instead of waiting to see the output, we redirect it to the sort command, which outputs the data alphabetically.
Now that you understand the main concepts applied when customizing a file’s input to get the number of lines, let’s see eight ways to count lines in a file in Linux.
Ways to Count Lines in a File in Linux
WC
The wc command returns a file’s line numbers, words, and characters, respectively.
Let’s create a file, practice.txt , and append the following lines.
We are counting file lines. We use the wc, awk, sed, grep, and perl commands. The process is easy because we can redirect ouptut and pipe commands. Linux is becoming fun!
Running the wc command on the file, we get the following output:
Likewise, we can control the output using specific flags with the input redirection symbol.
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.
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
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.
You can use «-l» or «—line» option with wc command as follows:
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.
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
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 (‘|’) .
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:
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:
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
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.
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.
Likewise, a cat command with ‘-n’ can also be used to print file content with line numbers.
$ cat -n distros.txt | tail -n1
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.