Javatpoint Logo
Javatpoint Logo

Linux Sed Regular Expressions


Stream Editor

The sed command is used for stream editing.

Example:

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.

Example:

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.

Example:

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>

Example:

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.

Example:

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 '\t'.

Example:

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.

Example:

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}".

Example:

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.

Example:

Linux Sed Regular Expression8

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

Next TopicLinux Users




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


B.Tech / MCA