Sed Basic Syntax
Sed - Understanding Basic Syntax and Options
Section titled “Sed - Understanding Basic Syntax and Options”This chapter introduces the fundamental command-line syntax for invoking Sed and its common options. Sed can be invoked in primarily two ways:
1. `sed [options] 'command(s)' [file(s)]`2. `sed [options] -f scriptfile [file(s)]`The first form allows you to specify Sed commands directly on the command line, enclosed in single quotes (to protect them from shell expansion). The second form reads Sed commands from a specified scriptfile. You can combine these, and if no file(s) are given, Sed reads from standard input.
Specifying Multiple Commands
Section titled “Specifying Multiple Commands”You can provide multiple Sed commands in several ways:
- Using multiple
-eoptions:sed -e 'command1' -e 'command2' ... files - Separating commands with semicolons within a single script:
sed 'command1; command2; ...' files(Note: some commands like those taking labels might require newlines instead of semicolons). - Placing commands on separate lines in a script file and using
-f scriptfile.
Let’s prepare a sample file books.txt:
[user]$ cat books.txt1) 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, 864Example: Deleting lines 1, 2, and 5 using multiple -e options. The d command deletes lines.
[user]$ sed -e '1d' -e '2d' -e '5d' books.txtOutput:
3) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. R. Tolkien, 4326) A Game of Thrones, George R. R. Martin, 864Example: Same operation using a script file.
[user]$ echo -e "1d\n2d\n5d" > commands.sed[user]$ cat commands.sedContent of commands.sed:
1d2d5dNow, execute Sed with this script file:
[user]$ sed -f commands.sed books.txtOutput (same as above):
3) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. R. Tolkien, 4326) A Game of Thrones, George R. R. Martin, 864Standard POSIX Options
Section titled “Standard POSIX Options”These options are generally available in most Sed implementations.
-nor--quietor--silent: Suppress automatic printing of the pattern space. By default, Sed prints each line after processing. With-n, lines are only printed if explicitly commanded (e.g., by thepcommand).
Example with -n (prepare quote.txt):
[user]$ echo -e "First line.\nSecond line." > quote.txt
[user]$ sed -n 'p' quote.txt # With -n, 'p' is needed to printOutput:
First line.Second line.[user]$ sed 'p' quote.txt # Without -n, 'p' causes double printingOutput (each line printed twice: once by default, once by p):
First line.First line.Second line.Second line.-e script: Add thescriptto the set of commands to be run. Useful for multiple commands or scripts containing shell metacharacters.
[user]$ sed -e 's/First/Initial/' -e 's/Second/Final/' quote.txtOutput:
Initial line.Final line.-f script-file: Add the contents ofscript-fileto the commands.
[user]$ echo 's/line/LNE/g' > transform.sed[user]$ sed -f transform.sed quote.txtOutput:
First LNE.Second LNE.GNU Sed Specific Options
Section titled “GNU Sed Specific Options”These options are specific to GNU Sed and might not be available or work differently in other Sed versions (e.g., on macOS, which often uses BSD Sed).
--expression=script: Long form of-e script.--file=script-file: Long form of-f script-file.--follow-symlinks: When using-i(in-place editing), this option makes Sed follow symbolic links and edit the target file. By default,-imight create a new file and replace the symlink.-i[SUFFIX]or--in-place[=SUFFIX]: Edit files in-place. This is a very powerful and potentially destructive option. Use with caution! IfSUFFIXis provided (e.g.,-i.bak), a backup of the original file is created with that suffix. If noSUFFIXis given, the original file is overwritten directly. Example:sed -i.bak 's/old/new/' file.txtmodifiesfile.txtand createsfile.txt.bak.-l Nor--line-length=N: Specify the desired line-wrap length for thel(list unambiguous) command. Default is often 70.N=0means no wrapping.--posix: Disable all GNU extensions. This makes GNU Sed behave more like a standard POSIX Sed, useful for writing portable scripts.-ror--regexp-extended: Use Extended Regular Expressions (ERE) instead of Basic Regular Expressions (BRE). This makes many regex patterns cleaner as special characters like?,+,{},|,()do not need to be escaped. (Note: POSIX standard for ERE is often-E, which GNU Sed also supports). Example:sed -r 's/(word1|word2)/replacement/'vs BREsed 's/\(word1\|word2\)/replacement/'.-sor--separate: By default, Sed considers all input files as one continuous stream. With-s, files are treated separately. This affects line numbering and address ranges like$(last line), which will apply to each file individually.-uor--unbuffered: Load minimal amounts of data from input files and flush output buffers more frequently. This is useful when processing the output of commands liketail -f, ensuring that Sed processes and outputs data as it arrives without long delays.-zor--null-data: Separate lines by NUL characters (\0) instead of newlines. This is very useful for processing filenames generated byfind ... -print0, which can safely handle filenames with spaces, newlines, or other special characters.
Choosing the Right Options:
For portability, stick to POSIX options (-n, -e, -f) and BRE, or explicitly use sed --posix. For convenience in scripts where GNU Sed is guaranteed, -r for ERE and -i for in-place editing are extremely useful. Always be careful with -i and consider making backups.