Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count number of non empty lines in a file using sed?

Tags:

bash

shell

sed

how to find how many lines I have in file by sed (need to ignore spaces and empty lines)

for example

if I have file with 139 lines (line can include only one character) then sed should return 139

lidia

like image 225
lidia Avatar asked Sep 03 '10 06:09

lidia


People also ask

How do you count lines using sed?

Count Number Of Lines Using Sed 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.

How do you count non empty lines in Unix?

We primarily use the wc (word count) command to count lines, words, bytes, and characters.

How do I count the number of blank lines in a file?

'grep -cv -P '\S' filename' will count the total number of blank lines in the file.

How do I count the number of lines in a file in Linux?

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.


1 Answers

This is a job for grep, not sed:

<myfile grep -c '[^[:space:]]'
like image 94
Gilles 'SO- stop being evil' Avatar answered Oct 10 '22 01:10

Gilles 'SO- stop being evil'