Sed Pattern Buffer
Stream Editor - Understanding the Pattern Buffer
Section titled “Stream Editor - Understanding the Pattern Buffer”A core concept in SED is the pattern buffer. When SED processes input, it reads one line at a time (unless modified by commands like N) into this temporary storage area. Most SED commands operate on the text held in the pattern buffer. After all commands in the script have been applied to the line in the pattern buffer, SED typically prints the buffer’s contents to standard output and then reads the next line, repeating the cycle.
Let’s create a sample file named books.txt which we’ll use throughout these examples. Ensure your file content matches for consistent results.
File: `books.txt`1) A Storm of Swords, George R. R. Martin, 12162) The Two Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 2886) A Game of Thrones, George R. R. Martin, 864The Print Command (p) and Default Output
Section titled “The Print Command (p) and Default Output”The p command explicitly prints the current contents of the pattern buffer.
[jerry]$ sed 'p' books.txtThis command produces the following result:
1) A Storm of Swords, George R. R. Martin, 12161) A Storm of Swords, George R. R. Martin, 12162) The Two Towers, J. R. R. Tolkien, 3522) The Two Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 1973) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. R. Tolkien, 4324) The Fellowship of the Ring, J. R. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 2885) The Pilgrimage, Paulo Coelho, 2886) A Game of Thrones, George R. R. Martin, 8646) A Game of Thrones, George R. R. Martin, 864Each line appears twice because:
- SED, by default, prints the pattern buffer after all commands for a line are executed.
- We explicitly used the
pcommand, which also prints the pattern buffer. To suppress the default printing action, use the-n(no-print) option.
[jerry]$ sed -n 'p' books.txtNow, each line is printed only once, solely due to the p command:
1) A Storm of Swords, George R. R. Martin, 12162) The Two Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 2886) A Game of Thrones, George R. R. Martin, 864Addressing Lines for Operations
Section titled “Addressing Lines for Operations”SED commands can be restricted to operate only on specific lines using addresses. An address can be a line number, a regular expression, or a range.
Print a specific line number (e.g., line 3):
[jerry]$ sed -n '3p' books.txtOutput:
3) The Alchemist, Paulo Coelho, 197Print a range of lines (e.g., lines 2 through 5):
[jerry]$ sed -n '2,5p' books.txtOutput:
2) The Two Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 288Special Addresses: $ (Last Line)
Section titled “Special Addresses: $ (Last Line)”The $ character represents the last line of the input.
Print the last line:
[jerry]$ sed -n '$p' books.txtOutput:
6) A Game of Thrones, George R. R. Martin, 864Print from line 3 to the last line:
[jerry]$ sed -n '3,$p' books.txtOutput:
3) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 2886) A Game of Thrones, George R. R. Martin, 864Advanced Addressing (GNU SED Extensions)
Section titled “Advanced Addressing (GNU SED Extensions)”GNU SED offers more flexible addressing schemes.
M,+n: Matches lineMand thenlines immediately following it. So, it acts onn+1lines in total.
Example: Print line 2 and the next 3 lines (total 4 lines: 2, 3, 4, 5).
[jerry]$ sed -n '2,+3p' books.txtOutput:
2) The Two Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 288M~n: Matches lineMand then everyn-th line after lineM.
Example: Print only odd-numbered lines (start at line 1, step by 2).
[jerry]$ sed -n '1~2p' books.txtOutput:
1) A Storm of Swords, George R. R. Martin, 12163) The Alchemist, Paulo Coelho, 1975) The Pilgrimage, Paulo Coelho, 288Example: Print only even-numbered lines (start at line 2, step by 2).
[jerry]$ sed -n '2~2p' books.txtOutput:
2) The Two Towers, J. R. R. Tolkien, 3524) The Fellowship of the Ring, J. R. R. Tolkien, 4326) A Game of Thrones, George R. R. Martin, 864Understanding the pattern buffer and addressing is fundamental to effectively using SED for text manipulation. Remember that these operations, by default, do not change the original file; they only affect the output stream. To modify the file in-place, use the -i option (e.g., sed -i 'command' file), but use it with caution, perhaps backing up your file first.