Linux Sed Regular Expressions

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Linux Sed Regular Expressions

Stream Editor

The sed command is used for stream editing.


  1. echo interactive | sed 's/inte/dist/'  
  2. echo interactive | sed 's:inte:dist:'  
  3. echo interactive | sed 's_inte_dist_'  
  4. echo interactive | sed 's|inte|dist|'  

Linux Sed Regular Expression1

Look at the above snapshot, string 'interactive' is changed to 'distractive' with sed command. Inspite of forward slash(/), colon (:), underscore (_) and pipe (|) will also work.

Interactive Editor

The sed command is meant to be stream editor while it can also be used as interactive editor on a file. For interactive editor option 'i' is used.


sed -i 's/today/tomorrow/' file 

Linux Sed Regular Expression2

Look at the above snapshot, stream 'today' is converted into 'tomorrow' in the 'file'.

Simple Back Referencing

Double ampersand is used to search and find the specified string. It will print the found string with sed command.


echo fourty | sed 's/four/&&/'

Linux Sed Regular Expression3

Look at the above snapshot, ampersand has searched the string 'four' and printed it as 'fourfourty'.

A Dot For Any Character

In regex a simple dot can signify any character.,/p>


echo xxxx-xx-xx | sed 's/....-..-../YYYY-MM-DD/'

Linux Sed Regular Expression4

Look at the above snapshot, dots are replaced by the date format.

Multiple Back Referencing

When more than one pair of parenthesis is used it is called grouping. Here each of them can be referenced separately as three consecutive numbers.


  1. echo 2014-06-30 | sed 's/(....)-(..)-(..)/1:2:3/'  
  2. echo 2014-06-30 | sed 's/(....)-(..)-(..)/1_2_3/'  
  3. echo 2014-06-30 | sed 's/(....)-(..)-(..)/2:3:1/'  
  4. echo 2014-06-30 | sed 's/(....)-(..)-(..)/3:2:1/'  

Linux Sed Regular Expression5

Look at the above snapshot, date is printed in different formats. Here, 2014 is referenced as (1), 06 is refernced as (2) and 30 is referenced as (3).

White Space

The white space syntax is 's' and tab space syntax is ' '.


  1. echo -e 'this is javatpoint' | sed 's/s/ /g'  

Linux Sed Regular Expression6

Look at the above snapshot, 's' is used for a single space.

Optional Occurrence

You can specify something optional by specifying it with (?) question mark.


cat list2 | sed 's/iii?/Y/' 

Linux Sed Regular Expression7

Look at the above snapshot, we have made third 'i' as optional. It mens that two 'i' are must to be converted into 'Y'.

Exact n Times Occurence

Exact times occurence is specified by "{times}".


  1. cat list2 | sed 's/i{3}/Y/'  

Linux Sed Regular Expression7

Look at the above snapshot, we have specified exactly three times occurence of 'i'.

Occurence In Range

We can specify occurence in terms of range also. For example, if we'll specify range as {m,n}, then 'm' denotes minimum times occurence and 'n' denotes maximum times occurence.


  1. cat list2 | sed 's/i{3,4}/Y/'  

Linux Sed Regular Expression8

Look at the above snapshot, we have specified minimum range as 3 and maximum range as 4.