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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.