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
Prev Home Next

6.4. When should I NOT use sed?

You should not use sed when you have "dedicated" tools which can do the job faster or with an easier syntax. Do not use sed when you only want to:

  • print individual lines, based on patterns within the line itself. Instead, use "grep".
  • print blocks of lines, with 1 or more lines of context above or below a specific regular expression. Instead, use the GNU version of grep as follows:
            grep -A{number} -B{number} "regex"
    
  • remove individual lines, based on patterns within the line itself. Instead, use "grep -v".
  • print line numbers. Instead, use "nl" or "cat -n".
  • reformat lines or paragraphs. Instead, use "fold", "fmt" or "par".

The tr utility is also more suited than sed to some simple tasks. For example, to:

  • delete individual characters. Instead of "s/[a-d]//g", use
            tr -d "[a-d]"
    
  • squeeze sequential characters. Instead of "s/ee*/e/g", use
            tr -s "{character-set}"
    
  • change individual characters. Instead of "y/abcdef/ABCDEF/", use
            tr "[a-f]" "[A-F]"
    

Note, however, that tr does not support giving input files on the command line, so the syntax is:

     tr {options-and-patterns} < input-file

or, to process multiple files:

     cat input-file1 input-file2 | tr {options-and-patterns}

If you have multiple files, using tr instead of sed is often more of an exercise than a useful thing. Although sed can perfectly emulate certain functions of cat, grep, nl, rev, sort, tac, tail, tr, uniq, and other utilities, producing identical output, the native utilities are usually optimized to do the job more quickly than sed.

The sed FAQ
Prev Home Next

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