The utility ed and here operator

The ed utility is a basic type of editor. We can edit text files using this editor:

  1. Write the script here_05.sh:
#!/bin/bash 
# flowers.txt contains the name of flowers 
cat flowers.txt 
ed flowers.txt << quit 
,s/Rose/Lily/g 
w 
q 
quit 
cat flowers.txt 
  1. Save the file, give the permission to execute, and run the script as follows:
    $ chmod u+x here_05.sh
    $ ./here_05.sh
  
  1. The output is here:
    Aster, Daffodil, Daisy, Jasmin, Lavender, Rose, Sunflower 59 59 Aster, Daffodil, Daisy, Jasmin, Lavender, Lily, Sunflower   
  

In this script, we have passed the here document to a utility for editing the flowers.txt file. We replaced the word Rose with Lily.

Get Learning Linux Shell Scripting - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.