How to Remove Square Brackets in Strings Using sed: A Regex Escaping Guide


2 views

When processing text in bash, many developers encounter this common scenario: needing to remove both opening and closing square brackets from strings in a single sed operation. The naive approach of simple character substitution fails due to regex's special interpretation of square brackets.

The original command:

echo '[123]' | sed 's/[\$$\$$]//'

Doesn't work because:

  1. The inner square brackets create a character class
  2. The escaped brackets become literal characters within that class
  3. The pattern matches either [ or ], but not as literal brackets

Here are three working solutions:

Method 1: Using Character Classes Correctly

echo '[123]' | sed 's/[][]//g'

This works because placing the closing bracket first in the character class makes it literal.

Method 2: Escaping with Backslashes

echo '[123]' | sed 's/$$\|$$//g'

The $$acts as an OR operator between the escaped brackets.

Method 3: Using Extended Regex

echo '[123]' | sed -E 's/\[|$$//g'

The -E flag enables extended regex where | doesn't need escaping.

For more complex cases, like nested brackets or preserving content between brackets:

# Remove only outer brackets
echo '[[123]]' | sed 's/^$$//;s/$$$//'

# Remove all brackets globally
echo '[1[2]3]' | sed 's/[][]//g'

When processing large files:

  • The character class method (Method 1) is fastest
  • For multiple operations, combine substitutions: sed -e 's/$$//g' -e 's/$$//g'
  • Consider awk for very large files (>1GB)

Many developers encounter this common text processing scenario: you need to strip both the opening [ and closing ] brackets from strings in Linux. While it seems simple, the special regex meaning of square brackets can cause unexpected behavior.

echo '[123]' | sed 's/[\$$\$$]//g'

The initial approach often misses two key points:

  1. Brackets need proper escaping in character classes
  2. The g flag is required for global replacement

Here are three reliable methods:

1. Using Character Class with Global Flag:

echo '[123]' | sed 's/[][]//g'

2. Escaped Brackets Alternative:

echo '[123]' | sed 's/\$$\$$//g'

3. Multiple Pattern Approach:

echo '[123]' | sed -e 's/\$$//g' -e 's/\$$//g'

For files with multiple bracketed sections:

sed -i 's/[][]//g' input.txt

To preserve content between nested brackets:

echo '[[123]]' | sed 's/\$$\\|\$$//g'

The character class method [][] is generally fastest as it:

  • Requires only one pattern match
  • Processes the entire string in a single pass
  • Minimizes backslash escaping