Advanced Text Manipulation with Sed: Techniques and Examples

Hey everyone, I'm working on a bunch of log files and need to extract specific data patterns that go beyond simple find/replace. I've seen Sed used for some pretty complex stuff, but I'm struggling to get my head around the more advanced features. Can anyone share some cool tricks or real-world examples of using Sed for intricate text manipulation?

1 Answers

āœ“ Best Answer

šŸ”„ Advanced Text Manipulation with Sed

sed, the stream editor, is a powerful command-line tool for text manipulation. While basic usage is common, sed offers advanced features for complex tasks. Let's explore some techniques with examples.

🧰 Inserting Text Based on Patterns

You can insert text before or after a specific pattern using sed. The i command inserts text before a pattern, and the a command appends text after a pattern.

Example: Inserting a Line Before a Pattern


sed '/PATTERN/i\NEW_LINE'

This command inserts 'NEW_LINE' before each line containing 'PATTERN'.


echo "line1\nPATTERN_FOUND\nline3" | sed '/PATTERN_FOUND/i\INSERTED_LINE'

Output:


line1
INSERTED_LINE
PATTERN_FOUND
line3

Example: Appending a Line After a Pattern


sed '/PATTERN/a\NEW_LINE'

This command appends 'NEW_LINE' after each line containing 'PATTERN'.


echo "line1\nPATTERN_FOUND\nline3" | sed '/PATTERN_FOUND/a\APPENDED_LINE'

Output:


line1
PATTERN_FOUND
APPENDED_LINE
line3

šŸ’¾ Using Hold Space Effectively

The hold space in sed is a temporary buffer. You can use it to store and retrieve lines, enabling complex operations across multiple lines.

  • h: Copy pattern space to hold space.
  • H: Append pattern space to hold space.
  • g: Copy hold space to pattern space.
  • G: Append hold space to pattern space.
  • x: Exchange contents of hold and pattern spaces.

Example: Swapping Two Consecutive Lines


sed 'N;s/\(.*
\)\(.*\)/\2\1/'

Explanation:

  • N: Reads the next line and appends it to the pattern space (separated by a newline).
  • s/\(.* \)\(.*\)/\2\1/: Swaps the two lines.

echo "line1\nline2\nline3\nline4" | sed 'N;s/\(.*
\)\(.*\)/\2\1/'

Output:


line2
line1
line4
line3

šŸ“œ Performing Multi-Line Operations

sed can perform operations spanning multiple lines, which is useful for tasks like removing blocks of text or applying transformations across several lines.

Example: Removing a Block of Text

Suppose you want to remove a block of text between START and END markers.


sed '/START/,/END/d'

This command deletes all lines from the line containing START to the line containing END (inclusive).


echo "line1\nSTART\nline2\nline3\nEND\nline4" | sed '/START/,/END/d'

Output:


line1
line4

Example: Replacing Text Across Multiple Lines

To replace text across multiple lines, you can use the hold space in combination with labels and branching.


sed ':a;N;$!ba;s/SEARCH_PATTERN/REPLACEMENT/g'

Explanation:

  • :a: Defines a label 'a'.
  • N: Appends the next line to the pattern space.
  • $!ba: If it's not the last line ($!), branch back to label 'a' (ba).
  • s/SEARCH_PATTERN/REPLACEMENT/g: Performs the substitution globally across all lines in the pattern space.

echo "line1\nSEARCH_PATTERN\nline3" | sed ':a;N;$!ba;s/SEARCH_PATTERN/REPLACEMENT/g'

Output:


line1
REPLACEMENT
line3

šŸŽ‰ Conclusion

These advanced sed techniques provide powerful tools for text manipulation. Mastering hold space and multi-line operations can greatly enhance your command-line text processing capabilities. Experiment with these examples to deepen your understanding!

Know the answer? Login to help.