Home > sed
![]() |
sed . . . the stream editorFind here the sed FAQ (Frequently Asked Questions) file, some one-line sed scripts, and a brief introduction to sed. I suggest several "flavors" of sed for DOS/Windows users. What is sed?Originally designed for Unix, sed has been available for DOS and other operating systems for many years. Sed is a non-interactive editor that works from a command line; it is not a Windows application with icons or a menu bar. What does it do? sed changes text: deleting, adding, inserting, substituting. Sed changes blocks of text "on the fly" by using a pre-written
list of commands. With sed, you don't open up a screen, push a cursor
around, and press "Hey," you might ask. "Is it just a find-and-replace program? You can find that in any word processor." Not like this. Sed is very powerful, and you can do things in sed you can't do in any standard word processor, even ones that support "regular expressions". Also, because sed is small and works on a "stream" of text, it can quickly alter files several gigabytes in size. How it works: You feed sed a script of editing commands (like, "change
every line that begins with a colon to such-and-such") and sed sends the
altered text to the screen. To save the revisions on disk, use the
redirection arrow, > This sample will change "black" to "white" throughout your file: sed "s/black/white/g" input.txt >output.txt This sample shows how to make sed read a multi-line sed script, myscript.sed, instead of entering multiple commands from the command prompt: sed -f myscript.sed input.txt >output.txt Setting up an environment for sedSince I expect that most of the visitors to this page will be people who (like me) work in a DOS/Windows environment, I am writing a separate document to explain the effective use of the Command prompt. Read it to avoid frustration and wasted effort on the command prompt. |
|
|
All of these versions will run in a DOS window or in a full-screen DOS session under Microsoft Windows.
recommended sed binaries for DOS and 16-bit operating systems (not modern Windows!) |
---|
I feel somewhat like a fossil in presenting the table below, as it presents versions of sed that are antique by modern standards and that I would never recommend for general use today. I myself use only two versions of sed under Windows: ssed, because I like having Perl regular expressions available, and gsed 4.2.1. Under Cygwin or in a true Linux environment, I use the latest version of GNU sed available, usually version 4.2.2 or 4.2.1. The following table is presented primarily for historic purposes.
Table of features in DOS and Windows versions of sed | ||||||||||||
Feature offered: | ssed | gsed421 | gsed407 | gsed302.80 | gsed302 | gsed205 | gsed103 | csed | sedmod | sed16 | sed15 | bsed |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Long filenames for Windows 9x, XP, and higher | Y | Y | Y | Y | Y | Y | N | Z1 | Z2 | |||
represent newline by \n on repl side of s/find/repl/ | Y | Y | Y | N | N | Y | Y | Y | Y | |||
represent tabs by \t | Y | Y | Y | N | N | Y | Y | Y | Z3 | |||
represent hex values by \xHH | Y | Y | Y | N | N | Y | N | Y | N | |||
support OR operator with \|, e.g., /blue\|cyan/ | Y | Y | Y | Y | Y | Y | N | N | N | |||
support interval expressions like \{m,n\} | Y | Y | Y | Y | N | N | Z4 | Y | ||||
support begin~step line selection with M~N | Y | Y | Y | Y | N | N | N | N | ||||
"a append-on-same-line" (for a,i, or c commands) | Y | Y | N | N | N | Y | Y | N | ||||
flag for N th match in substitution; e.g., s/find/repl/8 | Y | Y | Y | Y | Y | N | Y | |||||
flag to ignore case in /match/i or s/find/repl/i | Y | Y | Y | N | N | Y | N | N | ||||
command-line switch to edit files in-place, as -i | Y | Y | Y | N | N | Y | N | N | ||||
command-line switch to copy files if using -i, as -c | N | Y | N | N | N | N | N | N | N | N | N | |
command-line switch for extended regexes, as -r | Y | Y | ||||||||||
command-line switch for Perl regexes, as -R | Y | N | N | N | N | N | N | N | N | N | N | |
command-line switch to open files in binary mode, as -b | N | Y | N | N | N | N | N | N | N | N | N | |
unlimited line length or buffer size | Y | Y | Y | Y | 64K | 4K | 4K | |||||
case change with \L, \l, \U, \u, \E, \e | Y | N | N | N | N | Y | N | N | ||||
Non-standard extensions or other exceptional features | ||||||||||||
awk-style word arguments with $1, $2, ... $8, $9 | N | N | N | N | N | Y | Y | N | ||||
can run in MS-DOS mode, fully outside of Windows | N | Y | Y | N | N | Y | Z9 |
1 File SED15X.ZIP does not support Win95 long filenames. However, a version of SED15.EXE compiled with Mingw32 does support long filenames and is available from me, here.
2Berkeley sed docs say that it supports Windows Long Filenames, but it doesn't work under Windows 2000. Not yet tested under Win9x.
3Yes, but only substitutes TAB for \t on the "find" portion, not on the "replace" portion of a s/ubsti/tution/. Undocumented.
4 Interval range of \{0,n\} not supported in sed15.
9 sed15 will run in MS-DOS mode; the version compiled with Mingw32 will not.
mailing list for sed users |
---|
If you are interested in learning more about sed (its syntax, using regular expressions, etc.) you are welcome to subscribe to a sed-oriented mailing list. In fact, there are two mailing lists about sed: one in English named sed-users, moderated by Sven Guckes; and one in Portuguese named sed-BR, moderated by Aurelio Marinho Jargas. The average volume of mail for sed-users is about 10 messages a week.
related batch editing files |
---|
Console/command-line appsWindows apps
- MiniTrue, v2.02 - by Andrew Pipkin, but debugged and upgraded to version 2.0.6 by Jason Hood. MiniTrue ("mtr") is a truly powerful text view and replacement tool. Search/Replace syntax uses Perl-style regular expressions, with a non-sed style syntax ("find_regex" = "replace"). Editing commands can be entered from the DOS command line or from a script file. MT supports direct alteration of input files (unlike sed), directory recursion (ditto), prompted replacement (ditto!), video highlighting (ditto!!), multi-line searching (ditto!!!), and handling of compressed archive files like .ZIP, .ARJ, .LHA, and others (yowza!). Handles files and lines of unlimited size or length. Best of all, MT is distributed as freeware, with the source code available.
MiniTrue ran fine on my Windows 98 system, but under Windows 2000 there wee problems with the console display, the pager, and long filename expansion. I wrote this document to briefly summarize how to use it, and to suggest some workarounds under Win2K. My document is not finished, but I'm posting it anyway as a work in progress. It's better than having nothing at all. (NOTE: the problem I wrote about was fixed in Hood's version.)
- snr506.zip - Search'N'Replace v5.06, by Tom Lundin. Doesn't use Unix "regular expressions", but possibly THE most powerful tool ever written for doing massive search-and-replace operations on any file, including binary code and .EXE files. Find/replace commands may be up to 4,999 chars in length, and up to 2,500 find/replace commands may be entered in a single table, with all commands processed simultaneously. Supports variable-length search patterns, various wildcards, and multiple toggle-type (on/off) flags. $50 shareware, but worth every penny if you need full control over binary or nontext files. (filesize: 100k)
- snr61sw.zip - Search'N'Replace v6.1 (shareware zip file). This is a more recent version of SNR than the other. Includes 32-bit versions for DOS and a GUI version for Win95/98/NT (with long filename support). Requires 486 or higher to run. Shareware price is raised to $60. (filesize: 417k)
This section has been revised, to remove programs that don't work that well and to recommend better things in their place. If you find things that work better for you, drop me a line to show me your discoveries.
- BK ReplaceEm - ver. 2.0, by Boolean Dream. This software is free for both personal and commercial use, with no advertising stuck in. Supports strings, regular expressions, directory recursion, named "groups" of files to alter (you may regularly change the same set of files), line ranges (only replace within certain blocks or between certain lines), case-sensitive on/off, and much more. This is one of the best.
- Handy File Find and Replace - ver. 1.2, by SilverAge Software. Case sensitive, recurse down directories, select files by dates, attributes, generate log files, robust regular expressions, and more. This software is free for personal or corporate use. No nag screens.
- TextWiz - ver. 2.1. Features include multi-line find/replace in any number of files from the same folder; optionally create back-up files; optionally just searches for text in multiple files, returning a list of filenames; view files in TextWiz, in Notepad, or in a default application; case-sensitive matching; upload files via FTP; counts lines/words/chars, more. Free for personal use, but contains ad banners. Commercial use (or removing ad banners) costs $20.
- InfoRapid Search & Replace - ver. 3.1e. A Win32 app with built-in converters for searching and previewing HTML/RTF documents. In pure text files, the found passages can be replaced after the search is over. A preview displays hits in highlighting. InfoRapid can also search WinWord, Excel, Lotus, WordPerfect, etc. and preview them in their original layout. InfoRapid Search & Replace is distributed as freeware for private use; commercial use requires a small fee. Replacements can be performed in binary files as well as text.
- XReplace-32 - version 2.32, for Win XP/2K/9x, by Vestris, Inc. A 32-bit Windows utility for performing multiple replacements in multiple files in multiple directories. XReplace supports regular expressions, macros, scheduled replacement times, search patterns can cross line boundaries. Shareware from Switzerland, US$39; 15-day free trial version available.
sed debuggers
Obviously, you have to have access to Python and/or a genuine ksh (Korn shell) to run these debuggers. If I ever get a round tuit, I will write a mini-tutorial on using each of these. <smile>
Thanks for visiting. Hope you found what you wanted!
Eric Pement, sed FAQ maintainerLast modified: 14 Jan 2004, 15:00:04