Ubuntu – Find and report line numbers of empty lines in text file

command linetext processing

I have text file containing 14000+ lines. it contains some data I am using for data training of speech recognition.

I created that file via coding of java and due to some semantic error a few of the lines are empty. Every time I run training it gives an error after about 30 minutes complaining that there is a empty line.

Is there any code/script/command which can give me list of line numbers with empty lines, so I can fill those empty lines and save my time?

Working should be like:

I will input a file.txt and it will give me

line number 1121,1212,1450,13000 and so on ... are empty in file.txt

enter image description here

Best Answer

You can find the empty lines, and their line numbers, with

grep -E --line-number --with-filename '^$' file.txt  

An example:

w3@aardvark:~(0)$ grep -E --line-number --with-filename '^$' file.txt
file.txt:1:
file.txt:3:
file.txt:4:
w3@aardvark:~(0)$ cat -n file.txt
     1  
     2  Not empty
     3  
     4  
     5  Not empty
w3@aardvark:~(0)$ 

If your "empty" lines contain blanks or TABs, use:

grep -E --line-number --with-filename '^\s*$' file.txt