Linux count lines in output

Is there a way to find out the number of lines from a command output?

I am trying to compare command outputs between two servers. I want to find out the number of lines from the output of commands such as ls -l . Is there a way to do so? So far I have not found anything.

Depends on what you are compairing. diff /dir1 /dir2 will show the differneces. If there is no output they are the same

5 Answers 5

For someone this may be easier to remember:

Please note both grep and wc will count auxiliary lines like total 32044 or or ./dirname: . To avoid this especially in recursive output like ls -lR try this:

where first . is the directory path and -type f means that find will list files only. If you need all types (including directories, sockets etc) then just omit -type f .

Please note: wc -l DOES count empty lines (just ‘\n’) whilst grep . -c — does NOT.

$ wc -l .emacs.d/init.el 73 .emacs.d/init.el 
NAME wc - print newline, word, and byte counts for each file SYNOPSIS wc [OPTION]. [FILE]. wc [OPTION]. --files0-from=F . -l, --lines print the newline counts 

wc is part of GNU coreutils , you can have it in most Unix-like system.

Note that wc -l really counts newline characters, as the manpage says! If your input doesn’t end in a newline, the last line will not count. Example ( echo from bash suppresses the trailing newline with -n ): echo -n «foobar» | wc -l results in 0 .

Читайте также:  Linux изменить сетевое имя

you can pipe your command to wc command:

Yep, you could find number of lines from the output of ls -l through awk command also,

Awk’s NR variable stores the last record number at the end(After reading all the input lines).

If you want to compare a count of the files processed by either server then you can do that reliably like:

That recursively lists all files — not directories — one per line including .dotfiles beneath the current directory using shell globs as necessary to replace non-printable characters. grep filters out any parent directory listings or .. or */ or blank lines — so there should only be one line per file — the total count of which grep returns to you. If you want child directories included as well do:

Note that the -A option to ls is POSIX as of the latest version.

Источник

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

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.

Источник

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.

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.

Читайте также:  Linux for samba server

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.

Источник

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