sed command in Unix with example

In this article we will be studying one of the most powerful commands in Unix i.e. ‘sed’ which stands for ‘stream editor’. Whenever you want to modify any text, any string sed always comes handy.  Let’s study some of its features in details with the example

Let’s consider a file of name temp.txt which contains following data

sample_text_file

We are going to see below operation on above files with the help of sed command

  1. Replacing string on particular occurrence, all occurrences
  2. Replace string on particular line number
  3. Changing delimiter
  4. Deleting data
  5. Print only replaced lines

Let’s study one by one

  1. sed to replacing substring –

Let’s replace ‘sam’ with ‘clark’ in above text file.

replace_substring

Here, with the help of ‘sed’ we have replaced or substituted 1st occurrence of ‘sam’ with ‘clark’ from each line which is by default.

If the root string (here it is ‘sam’) multiple times in a line then only 1st occurrence will be replaced or substituted.

We can say ‘s’ = substitution operation.

If we want to replace all the occurrences of the root string ‘sam’, one can use below command.

global_substitution

Here, one can say ‘g’ is for Global substitution.

  1. sed to replace or substitute nth occurrence in the line.

nth_occurrence

In above example we have replaced 2nd occurrence of root string ‘sam’ with ‘clark’ from each line.

One can use below command to replace all the root strings from nth occurrence.

nth_global_occurence

Above command will replace all the occurrences of ‘sam’ with ‘clark’ from 2nd occurrence to all occurrences from a line.

3.  sed for Changing delimiter –

Till now we have seen that we used ‘/’ in command as delimiter.

If one want to replace the string which contains ‘/’ (slash) like data contains any website name then it will difficult for Unix to distinguish between slash as delimiter or as part of string and substitution won’t work.

Let’s consider one example –

sed_Delimiter


If we want to replace SAM/JOHN with CLARK/RICKY by usual method

sed_delimiter1

Unix will throw an syntax error.

To meet our requirement we can use sed command as below.

sed_delimiter2

In this case we have changed ‘/’ delimiter to another character ‘_’. We can use any character as delimiter.

4. Deleting data –

One can delete the lines in the file by specifying the line number or range of line which want to delete

sed_delete_lines

Third command will be used to delete 3rd line from file with next two lines ad will print the rest content of the file.

Last Command will delete everything from file except line number starting from 3rd to 7th line.

5. Print only replaced lines –

If anyone want to see the changes or print the output after replacing string then we can use below command.

sed_print

Here only last line from temp.txt has been printed as string ‘john’ has been replaced by ‘clark’.

6. Replace string from particular line –

From temp.txt sam appears on every line. If one want to replace string ‘sam’ from 2nd line only, below sed command can be used.

sed_replace_line

That it for sed. Hope you find this article worth reading. Will study awk in next article.


Leave a Reply

© 2017 Database ETL. All rights reserved.