0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Remove Blank Lines from File

Use the command grep -v '^$' filename or a Bash script with grep -v '^$' inputfile > outputfile to remove blank lines from a file.
📋

Examples

InputLine 1 Line 3 Line 6
OutputLine 1 Line 3 Line 6
Input
Output
InputHello World !
OutputHello World !
🧠

How to Think About It

To remove blank lines, think of reading the file line by line and keeping only those lines that have some content. Blank lines are lines that have no characters or only whitespace. We can use a tool that filters out lines matching the pattern of empty lines.
📐

Algorithm

1
Read the input file line by line.
2
Check if the line is not empty (not a blank line).
3
If the line has content, keep it.
4
Write all kept lines to a new file or output.
💻

Code

bash
#!/bin/bash
input_file="input.txt"
output_file="output.txt"
grep -v '^$' "$input_file" > "$output_file"
echo "Blank lines removed. Output saved to $output_file."
Output
Blank lines removed. Output saved to output.txt.
🔍

Dry Run

Let's trace the input file with lines: 'Line 1', '', 'Line 3', '', '', 'Line 6' through the code.

1

Read line

Line 1

2

Check if line is blank

Line 1 is not blank, keep it

3

Read line

'' (empty line)

4

Check if line is blank

Line is blank, skip it

5

Read line

Line 3

6

Check if line is blank

Line 3 is not blank, keep it

7

Read line

'' (empty line)

8

Check if line is blank

Line is blank, skip it

9

Read line

'' (empty line)

10

Check if line is blank

Line is blank, skip it

11

Read line

Line 6

12

Check if line is blank

Line 6 is not blank, keep it

Line ContentAction
Line 1Kept
Skipped
Line 3Kept
Skipped
Skipped
Line 6Kept
💡

Why This Works

Step 1: Pattern Matching

The grep -v '^$' command matches lines that are empty (start and end with nothing) and excludes them with -v.

Step 2: Filtering Lines

Only lines that do not match the empty line pattern are printed, effectively removing blank lines.

Step 3: Redirecting Output

The filtered lines are saved to a new file using output redirection >.

🔄

Alternative Approaches

Using sed
bash
sed '/^$/d' input.txt > output.txt
echo "Blank lines removed with sed."
This uses sed to delete empty lines; it is equally efficient and widely supported.
Using awk
bash
awk 'NF' input.txt > output.txt
echo "Blank lines removed with awk."
This uses awk to print only lines with fields (non-empty lines); useful if lines with spaces should be removed.
Using while read loop
bash
while IFS= read -r line; do
  [[ -n "$line" ]] && echo "$line"
done < input.txt > output.txt
echo "Blank lines removed with while loop."
This is a pure Bash approach, good for learning but slower on large files.

Complexity: O(n) time, O(n) space

Time Complexity

The script reads each line once, so time grows linearly with the number of lines.

Space Complexity

Output is stored separately; memory usage depends on output size, but no extra large buffers are needed.

Which Approach is Fastest?

Using grep or sed is faster than a Bash loop because they are optimized native tools.

ApproachTimeSpaceBest For
grep -v '^$'O(n)O(n)Simple and fast filtering
sed '/^$/d'O(n)O(n)Powerful text editing
awk 'NF'O(n)O(n)Flexible field-based filtering
while read loopO(n)O(n)Learning and custom logic
💡
Use grep -v '^$' for a quick and simple way to remove blank lines.
⚠️
Forgetting to quote variables or using commands that do not handle lines with spaces correctly.