Remove Comment Lines Using Linux Awk

In this tutorial, we’ll walk you through various methods to remove comment lines using AWK in Linux.

We’ll explore how to remove single comment lines, keep comments containing specific patterns, keep comments within range, and remove comment blocks.

 

 

Remove Comment Lines

First, let’s consider a sample data file sample-data.txt that look like this:

# This is a comment
User1, Plan A
# Another comment
User2, Plan B
User3, Plan C
# Yet another comment

To remove lines that start with #, you can use the following AWK command:

awk '!/^#/' sample-data.txt

Output:

User1, Plan A
User2, Plan B
User3, Plan C

The !/^#/ part selects lines that do not start (!) with the # character.

 

Remove Comment Lines and Keep Shebang

Suppose you have a script file, script.sh, with the following content:

#!/bin/bash
# This is a comment
echo "User1: Plan A"
# Another comment
echo "User2: Plan B"
# Yet another comment
echo "User3: Plan C"

To remove comment lines while keeping the shebang line, you can tell AWK to ignore lines that start with # except those starting with #!:

awk '!/^#|^$/ || /^#!/' script.sh

Output:

#!/bin/bash
echo "User1: Plan A"
echo "User2: Plan B"
echo "User3: Plan C"

The !/^#/ part filters out comment lines, |^$/ ignores empty lines, and || /^#!/ ensures the shebang line is retained.

 

Keep Lines Containing a Specific Pattern

Imagine you have a file, tasks.txt, with the following lines:

# This is a regular comment
# TODO: Update user plan
User1, Plan A
# Regular comment
# TODO: Check system status
User2, Plan B
# Another regular comment

To remove comment lines while preserving lines containing “TODO”, you can use this AWK command:

awk '!/^#/ || /TODO/' tasks.txt

Output:

# TODO: Update user plan
User1, Plan A
# TODO: Check system status
User2, Plan B

This AWK command works by combining two conditions: !/^#/ tells AWK to exclude lines starting with #, and || /TODO/ instructs AWK to include lines that contain “TODO”.

 

Keep Lines Within a Specific Range

Consider a file named config.txt that looks like this:

# Configuration File
# Default Settings
# User Preferences
# System Parameters
User1, Plan A
User2, Plan B
User3, Plan C
# End of User Data
User4, Plan D
User5, Plan E
# Additional Settings
User6, Plan F

To remove comment lines but keep lines 5 to 10, use the following AWK command:

awk 'NR>=5 && NR<=10 || !/^#/' config.txt

Output:

User1, Plan A
User2, Plan B
User3, Plan C
# End of User Data
User4, Plan D
User5, Plan E
User6, Plan F

This AWK command combines a range condition with a pattern exclusion. NR>=5 && NR<=10 ensures that lines 5 through 10 are included, regardless of their content.

The || !/^#/ part then excludes lines starting with # that are outside this range.

 

Remove Comment Blocks

Imagine a configuration file, settings.conf, with blocks of comments like this:

# Start of configuration
#
# This section defines user settings
# - User1: Active
# - User2: Inactive
#
# User settings end here

User1, Active
User2, Inactive

# Start of system settings
#
# This section is for system-level configurations
# - System Timeout
# - Memory Allocation
#
# System settings end

To remove entire blocks of comments, use the following AWK command:

awk '/^#/{if (f) print ""; f=0; next} {f=1; print}' settings.conf

Output:

User1, Active
User2, Inactive

This AWK command works by toggling a flag f whenever it encounters a comment line (lines starting with #).

If a line is not a comment, the flag is set and the line is printed.

When transitioning from a comment to a non-comment line, it prints an empty line for better readability.

Leave a Reply

Your email address will not be published. Required fields are marked *