How to find empty lines in files in Linux
In Linux, how to find an empty line in a file and output an empty line number.
Recently, such problems have been encountered in processing data analysis.
This article will show you how I use Linux commands to find empty lines in a file.
There are three common ways to find empty lines in a file, using the grep and awk commands.
Method 1
In the following method, we use the grep command and regular expression to find empty lines in the file.
➜ ~ grep -n -e "^$" test.log

Explain:
– n, indicating the number of output lines (starting from 1)
– e, indicating specify a pattern used during the search of the input
– ^, indicating the beginning
– $, indicating the end
Method 2 &Method 3
In the following two methods, we use the if conditional judgment expression of the awk command to find empty lines in the file.
➜ ~ awk '{if($0 == "") {print NR}}' test.log

➜ ~ awk '{if(!NF) {print NR}}' test.log

Explain:
– Awk NR variable, which indicates the serial number of the current record. It is the line number when processing a single file.
– Awk NF variable, indicating the number of fields in the current record.
– Awk $0 variable, indicating the current sequence number value.
OK. The above is the method I use to find empty lines in the file.