Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The sed FAQ
Home Next
Archive-Name: editor-faq/sed
Posting-Frequency: irregular
Last-modified: 10 March 2003
Version: 015
Maintainer: Eric Pement (
[email protected])

THE SED FAQ

Frequently Asked Questions about

sed, the stream editor

CONTENTS

1. GENERAL INFORMATION

1.1. Introduction - How this FAQ is organized

1.2. Latest version of the sed FAQ

1.3. FAQ revision information

1.4. How do I add a question/answer to the sed FAQ?

1.5. FAQ abbreviations

1.6. Credits and acknowledgements

1.7. Standard disclaimers

2. BASIC SED

2.1. What is sed?

2.2. What versions of sed are there, and where can I get them?

2.2.1. Free versions

2.2.1.1. Unix platforms

2.2.1.2. OS/2

2.2.1.3. Microsoft Windows (Win3x, Win9x, WinNT, Win2K)

2.2.1.4. MS-DOS

2.2.1.5. CP/M

2.2.1.6. Macintosh v8 or v9

2.2.2. Shareware and Commercial versions

2.2.2.1. Unix platforms

2.2.2.2. OS/2

2.2.2.3. Windows 95/98, Windows NT, Windows 2000

2.2.2.4. MS-DOS

2.3. Where can I learn to use sed?

2.3.1. Books

2.3.2. Mailing list

2.3.3. Tutorials, electronic text

2.3.4. General web and ftp sites

3. TECHNICAL

3.1. More detailed explanation of basic sed

3.1.1. Regular expressions on the left side of "s///"

3.1.2. Escape characters on the right side of "s///"

3.1.3. Substitution switches

3.2. Common one-line sed scripts. How do I . . . ?

  • double/triple-space a file?
  • convert DOS/Unix newlines?
  • delete leading/trailing spaces?
  • do substitutions on all/certain lines?
  • delete consecutive blank lines?
  • delete blank lines at the top/end of the file?

3.3. Addressing and address ranges

3.4. Address ranges in GNU sed and HHsed

3.5. Debugging sed scripts

3.6. Notes about s2p, the sed-to-perl translator

3.7. GNU/POSIX extensions to regular expressions

4. EXAMPLES

ONE-CHARACTER QUESTIONS

4.1. How do I insert a newline into the RHS of a substitution?

4.2. How do I represent control-codes or non-printable characters?

4.3. How do I convert files with toggle characters, like +this+, to look like [i]this[/i]?

CHANGING STRINGS

4.10. How do I perform a case-insensitive search?

4.11. How do I match only the first occurrence of a pattern?

4.12. How do I parse a comma-delimited (CSV) data file?

4.13. How do I handle fixed-length, columnar data?

4.14. How do I commify a string of numbers?

4.15. How do I prevent regex expansion on substitutions?

4.16. How do I convert a string to all lowercase or capital letters?

CHANGING BLOCKS (consecutive lines)

4.20. How do I change only one section of a file?

4.21. How do I delete or change a block of text if the block contains a certain regular expression?

4.22. How do I locate a paragraph of text if the paragraph contains a certain regular expression?

4.23. How do I match a block of specific consecutive lines?

4.23.1. Try to use a "/range/, /expression/"

4.23.2. Try to use a "multi-line\nexpression"

4.23.3. Try to use a block of "literal strings"

4.24. How do I address all the lines between RE1 and RE2, excluding the lines themselves?

4.25. How do I join two lines if line #1 ends in a [certain string]?

4.26. How do I join two lines if line #2 begins in a [certain string]?

4.27. How do I change all paragraphs to long lines?

SHELL AND ENVIRONMENT

4.30. How do I read environment variables with sed ...

4.31.1. ... on Unix platforms?

4.31.2. ... on MS-DOS or 4DOS platforms?

4.32. How do I export or pass variables back into the environment ...

4.32.1. ... on Unix platforms?

4.32.2. ... on MS-DOS or 4DOS platforms?

4.33. How do I handle shell quoting in sed?

FILES, DIRECTORIES, AND PATHS

4.40. How do I read (insert/add) a file at the top of a textfile?

4.41. How do I make substitutions in every file in a directory, or in a complete directory tree?

4.41.1. ... ssed solution

4.41.2. ... Unix solution

4.41.3. ... DOS solution

4.42. How do I replace "/some/UNIX/path" in a substitution?

4.43. How do I replace "C:\SOME\DOS\PATH" in a substitution?

4.44. How do I emulate file-includes, using sed?

5. WHY ISN'T THIS WORKING?

5.1. Why don't my variables like $var get expanded in my sed script?

5.2. I'm using 'p' to print, but I have duplicate lines sometimes.

5.3. Why does my DOS version of sed process a file part-way through and then quit?

5.4. My RE isn't matching/deleting what I want it to. (Or, "Greedy vs. stingy pattern matching")

5.5. What is CSDPMI*B.ZIP and why do I need it?

5.6. Where are the man pages for GNU sed?

5.7. How do I tell what version of sed I am using?

5.8. Does sed issue an exit code?

5.9. The 'r' command isn't inserting the file into the text.

5.10. Why can't I match or delete a newline using the \n escape sequence? Why can't I match 2 or more lines using \n?

5.11. My script aborts with an error message, "event not found".

6. OTHER ISSUES

6.1. I have a problem that stumps me. Where can I get help?

6.2. How does sed compare with awk, perl, and other utilities?

6.3. When should I use sed?

6.4. When should I NOT use sed?

6.5. When should I ignore sed and use Awk or Perl instead?

6.6. Known limitations among sed versions

6.7. Known incompatibilities between sed versions

6.7.1. Issuing commands from the command line

6.7.2. Using comments (prefixed by the '#' sign)

6.7.3. Special syntax in REs

6.7.4. Word boundaries

6.7.5. Commands which operate differently

7. KNOWN BUGS AMONG SED VERSIONS

7.1. ssed v3.59

7.2. GNU sed v4.0 - v4.0.5

7.3. GNU sed v3.02.80

7.4. GNU sed v3.02

7.5. GNU sed v2.05

7.6. GNU sed v1.18

7.7. GNU sed v1.03

7.8. sed v1.6 (Briscoe)

7.9. sed v1.5 (Helman)

7.10. sedmod v1.0 (Chen)

7.11. HP-UX sed

7.12. SunOS sed v4.1

7.13. SunOS sed v5.6

7.14. Ultrix sed v4.3

7.15. Digital Unix sed

The sed FAQ
Home Next

 
 
   Reprinted courtesy of Eric Pement. Also available at https://sed.sourceforge.net/sedfaq.html Design by Interspire