Stream Editor
sed command is use for stream editing.
Like example of :
# echo interactive | sed 's/inte/dist/'
# echo interactive | sed 's:inte:dist:'
# echo interactive | sed 's_inte_dist_'
# echo interactive | sed 's|inte|dist|'
See your terminal after run the command, string ‘interactive’ is change to ‘distractive’ with sed command.
The Linux forward slash ( / ), colon ( : ), underscore ( _ ) and pipe ( | ) will also work.
sed command is meant to be stream editor while it can also be use as interactive editor on a file.
For interactive editor option ‘i’ is used.
Like example of :
# sed -i 's/today/tomorrow/' file
See your terminal after run the command, stream ‘today’ is convert into ‘tomorrow’ in the ‘file‘.
Simple Back Referencing
Double ampersand is used to search and find the specifies string. Print the found string with sed command.
Like example of :
# echo fourty | sed 's/four/&&/'
See your terminal after run the commmand, ampersand has searched the string ‘four’ and printed it as ‘fourfourty’.
A Dot For Any Character
Regex is a simple dot can signify any character.,/p>
Like example of :
# echo xxxx-xx-xx | sed 's/....-..-../YYYY-MM-DD/'
After run the command and see your terminal, dots are replace by the date format.
Multiple Back Referencing
When more than one pair of parenthesis is use it is call grouping.
Here : Each of them can be reference separately as three consecutive numbers.
Like example of :
# echo 2014-06-30 | sed 's/\(....\)-\(..\)-\(..\)/\1:\2:\3/'
# echo 2014-06-30 | sed 's/\(....\)-\(..\)-\(..\)/\1_\2_\3/'
# echo 2014-06-30 | sed 's/\(....\)-\(..\)-\(..\)/\2:\3:\1/'
# echo 2014-06-30 | sed 's/\(....\)-\(..\)-\(..\)/\3:\2:\1/'
After run command and see your terminal, date is print in different formats.
Here : I hope you see, 2014 is reference as (1), 06 is reference as (2) and 30 is reference as (3).
White Space
The white space syntax is ‘\s’ and tab space syntax is ‘\t’.
Like example of :
# echo -e 'this\tis\tjavatpoint' | sed 's/\s/ /g'
After run the command and see your terminal, ‘\s’ is use for a single space.
Optional Occurrence
You can specify something optional by specifying it with (?) question mark.
Like example of :
# cat list2 | sed 's/iii\?/Y/
After run the command and see your terminal, we have made third ‘i’ as optional. It means that two ‘i’ are must to be convert into ‘Y’.
Exact n Times Occurrence
Exact times occurrence is specified by “{times}”.
Like example of the command :
# cat list2 | sed 's/i\{3\}/Y/'
After run the command and see your terminal, we have specified exactly three times occurrence of ‘i’.
Occurrence In Range
Now we can specify occurrence in terms of range also.
For example : If we will specify range as {m,n}, then ‘m‘ denotes minimum times occurrence and ‘n’ denotes maximum times occurrence.
Like example of this command :
# cat list2 | sed 's/i\{3,4\}/Y/'
After run the command and see your terminal, we have specified minimum range as 3 and maximum range as 4.