Linux count lines in file

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

wc -l < [filename] on a green background

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.

Читайте также:  Arch linux gui installer

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.

Источник

Wc Command in Linux (Count Number of Lines, Words, and Characters)

On Linux and Unix-like operating systems, the wc command allows you to count the number of lines, words, characters, and bytes of each given file or standard input and print the result.

In this tutorial, we will show you how to use the wc command through simple and practical examples.

Читайте также:  Настройка под линукс минт

How to Use the wc Command #

The syntax for the wc command is as follows:

The wc command can accept zero or more input FILE names. If no FILE is specified, or when FILE is — , wc will read the standard input. A word is a string of characters delimited by a space, tab, or newline.

In it’s simplest form when used without any options, the wc command will print four columns, the number of lines, words, byte counts and the name of the file for each file passed as an argument. When using the standard input the fourth column (filename) is not displayed.

For example, the following command will display information about the virtual file /proc/cpuinfo :

The output will look something like the following:

448 3632 22226 /proc/cpuinfo 
  • 448 is the number of lines.
  • 3632 is the number of words.
  • 22226 is the number of characters.

When using the standard input, the file name is not shown:

To display information about more than one file, pass the filenames, as arguments, separated by space:

wc /proc/cpuinfo /proc/meminfo

The command will give you information about each file and a line including total statistics:

448 3632 22226 /proc/cpuinfo 49 143 1363 /proc/meminfo 497 3775 23589 total 

The options below allow you to select which counts are printed.

  • -l , —lines — Print the number of lines.
  • -w , —words — Print the number of words.
  • -m , —chars — Print the number of characters.
  • -c , —bytes — Print the number of bytes.
  • -L , —max-line-length — Print the length of the longest line.

When using multiple options counts are printed in the following order: newline, words, characters, bytes, maximum line length.

For example, to display only the number of words you would use:

Here is another example that will print the number of lines and the length of the longest line.

The —files0-from=F option allows wc to read input from the files specified by NUL-terminated names in file F . If F is — then read names from standard input. For example, you can search for files using the find command and provide those files as an input to wc :

find /etc -name 'host*' -printf0 | wc -l --files0-from=-

The output will show the number of lines for all files in the /etc directory whose names start with “host”:

4 /etc/host.conf 27 /etc/avahi/hosts 1 /etc/hostname 14 /etc/hosts 46 total 

Count the Number of Lines #

The wc command is mostly used with the -l option to count only the number of lines in a text file. For example, to count the number of lines in the /etc/passwd file you would type:

The first column is the number of lines and the second one is the name of the file:

Читайте также:  Image backup linux system

Count the Number of Words #

To count only the number of words in a text file use wc -w followed by the file name. The following example counts the number of words in the ~/Documents/file.txt file:

The number of words is shown in the first column:

513 /home/linuxize/Documents/file.txt 

Wc Command Examples #

The wc command can be used in combination with other commands through piping. Here are a few examples.

Counting Files in the Current Directory #

The find command passes a list of all files in the current directory with each file name on a single line to the wc command, which counts the number of lines and prints the result:

Count the number of users #

In the example below wc is used to count the number of lines from the output of the getent command .

Conclusion #

The wc command stands for “word count” and has a quite simple syntax. It allows you to count the number of lines, words, bytes, and characters in one or multiple text files.

If you have any questions or feedback, feel free to leave a comment.

Источник

Calculating the number of lines in a file?

Be aware that the documentation says: «print the newline counts» when using the -l option, so if your file have three lines (two \n), thus, wc -l myfile.sh will return 2.

As mentioned by souravc, you can use wc for this:

$ wc -w statusToFiles.sh 10 statusToFiles.sh $ wc -l statusToFiles.sh 6 statusToFiles.sh 

To only display the count itself, you can pipe that output to awk , like this:

$ wc -l statusToFiles.sh | awk '< print $1 >' 6 

. or as kos mentioned below:

You can use grep command with blank matching string

Why does this is answer not have more upvotes? Is there something fishy about it? It works like a charm for me and the code looks very simple.

or with non-blank matching string if you want to count non-empty lines grep -c . file (The period . matches any single character.)

You can also output the entire file with line numbers in front of every line using the command below:

You must log in to answer this question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.12.43529

Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

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