Remove Brackets Using Linux awk

In this tutorial, you’ll learn how to use awk command to remove brackets in different forms, including simple brackets, nested brackets, and brackets containing specific types of content.

 

 

Simple Bracket Removal

Imagine you have a file named data.txt containing the following sample data:

[1001] Account_Status
[1002] Service_Upgrade
[1003] Payment_Details

To remove the square brackets, you can use awk gsub function to globally substitute the brackets ([, ]) with an empty string:

awk '{gsub(/\[|\]/,"")}1' data.txt

Output:

1001 Account_Status
1002 Service_Upgrade
1003 Payment_Details

The 1 at the end of the command triggers the default printing action in awk.

 

Remove Brackets with Content Inside

To remove the brackets and their contents, you can use a regular expression in gsub to match the pattern of an opening bracket [ followed by any character not a closing bracket [^]]* and then a closing bracket ]:

awk '{gsub(/\[[^]]*\]/,"")}1' data.txt

Output:

Account_Status
Service_Upgrade
Payment_Details

 

Remove Only Opening/Closing Bracket

To remove only the opening bracket [, you can use gsub to replace the opening bracket [ with an empty string:

awk '{gsub(/\[/,"")}1' data.txt

Output:

1001] Account_Status
1002] Service_Upgrade
1003] Payment_Details

Similarly, to remove only the closing bracket ], use:

awk '{gsub(/\]/,"")}1' data.txt

Output:

[1001 Account_Status
[1002 Service_Upgrade
[1003 Payment_Details

Here, gsub is used to substitute the closing bracket ] with nothing.

 

Remove Nested Brackets

Given the same data.txt:

[1001 [A1]] Account_Status
[1002 [B1 [B2]]] Service_Upgrade
[1003] Payment_Details

To remove only the brackets, use the following command:

awk '{while(match($0, /\[[^][]*\]/)) $0=substr($0, 1, RSTART-1) substr($0, RSTART+1, RLENGTH-2) substr($0, RSTART+RLENGTH)}1' data.txt

Output:

1001 A1 Account_Status
1002 B1 B2 Service_Upgrade
1003 Payment_Details

This command uses a while loop with the match function to find each bracket pair.

It then reconstructs each line by concatenating the parts of the line before the bracket, the content inside the bracket (excluding the brackets themselves), and the remainder of the line.

The process repeats until all brackets are processed.

 

Remove Brackets If the Content Is Numeric

Suppose your data.txt looks like this:

[1001] Account_Status
[Service] Service_Upgrade
[1003] Payment_Details

To remove brackets only when they contain numeric content only, you can use a regular expression in gsub to match brackets containing a numeric sequence ([0-9]+):

awk '{gsub(/\[[0-9]+\]/,"")}1' data.txt

Output:

 Account_Status
[Service] Service_Upgrade
 Payment_Details

The match is then replaced with an empty string, removing only the numeric-bracketed content.

Leave a Reply

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