How to remove blank lines in .txt files



The question of how to remove blank lines in a text file is one that many IT admins face, especially when working with large files. The good news is that there are a few different ways to do this, depending on the size and complexity of the file in question.

The simplest way to remove blank lines in a text file is to use a text editor such as Notepad++. Notepad++ has a feature that allows you to search and replace text in a document. By entering a regular expression such as “^\\s+$” into the “Find what” box, you can quickly remove all blank lines in the document. The “^” symbol means the beginning of a line, and “\\s+” means any whitespace characters in the line. The “$” symbol indicates the end of the line. By replacing the blank lines with nothing, you can quickly remove them from the document.

If the text file is very large, or if you need to remove blank lines in multiple files at once, then you may need to resort to using a command line utility such as sed or awk. Sed is a stream editor, which means it edits data as it is read from or written to a stream. Awk is a pattern scanning and processing language, similar to sed but with more powerful features. Both sed and awk can be used to remove blank lines in a text file.

For example, to remove all blank lines in a text file using sed, you would use the following command:

sed '/^$/d' input.txt > output.txt

The “/^$/d” part of the command means to delete all empty lines. The “input.txt” part of the command tells sed to read from the named file, and the “> output.txt” part of the command tells sed to write the output to the named file.

To remove all blank lines in a text file with awk, you would use the following command:

awk 'NF > 0' input.txt > output.txt

The “NF > 0” part of the command means to print only those lines that have at least one non-whitespace character. The rest of the command works the same as the sed command above.

It is also possible to remove blank lines from a text file using a scripting language such as Python or Perl. For example, you could use the following Python code to remove all blank lines from a text file:

with open('input.txt', 'r') as f:
lines = f.readlines()

with open('output.txt', 'w') as f:
lines = filter(lambda x: x.strip(), lines)
f.writelines(lines)

The first line of code reads the contents of the input file into a list of lines. The second line of code filters the list by removing all blank lines, then writing the filtered list to the output file.

In summary, there are several ways to remove blank lines from a text file. The simplest way is to use a text editor such as Notepad++. For more complex text files, you can use a command line utility such as sed or awk. Finally, if you need more control over the process, you can use a scripting language such as Python or Perl.

Inquire Now

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Find your next full or part-time role here

ScaleDesk can either help you find your next full time gig or a part time side gig where you can create extra income!

Onboard with us once
Skip HR screening and go to the final interview with with only your resume and a video interview you never have to redo
Get paid electronically every month for the hours you work
We will be your reference even if you work for us once

IT Teams: Use ScaleDesk to augment your team

Schedule Demo