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
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

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




Debian GNU/Linux Reference Guide
Prev Home Next

4.4.2 Replacement expressions

For the replacement expression, following characters have special meanings:

  • &

    • This represents what the regular expression matched. (use \& in emacs)

  • \n

    • This represents what the n-th bracketed regular expression matched.

For Perl replacement string, $n is used instead of \n and & has no special meaning.

For example:

     $ echo zzz1abc2efg3hij4 | \
       sed -e 's/\(1[a-z]*\)[0-9]*\(.*\)$/=&=/'
     $ echo zzz1abc2efg3hij4 | \
       sed -e 's/\(1[a-z]*\)[0-9]*\(.*\)$/\2===\1/'
     $ echo zzz1abc2efg3hij4 | \
       perl -pe 's/(1[a-z]*)[0-9]*(.*)$/$2===$1/'
     $ echo zzz1abc2efg3hij4 | \
       perl -pe 's/(1[a-z]*)[0-9]*(.*)$/=&=/'

Here please pay extra attention to the style of the bracketed regular expression and how the matched strings are used in the text replacement process on different tools.

These regular expressions can be used for the cursor movements and the text replacement actions in the editors too.

Please read all the related manual pages to learn these commands.

Debian GNU/Linux Reference Guide
Prev Home Next

  Published under the terms of the GNU General Public License Design by Interspire