sed scripts
Linux Commands - Managing Files and Text

sed scripts

If you need to create a script with sed that will allow you to repeat an action on a specific type of text you can create a sed script which will be activated by using the -f after the sed command followed by the filename of the script.


sed -f mysedscript


It is important not to leave trailing whitespace at the end of your lines in the sed script and it is important to put one sed command per line or terminate each command with a semi-colon. Remember, sed will edit your text file one line at a time because that is what is placed in memory.


# sed script evaluating the /etc directory

/hosts.allow/a\

This file determines which networks and individual\

computers can connect to the daemons on the server

/hosts.deny/c\

***********************************\

This is an important file for security that should\

be to deny all.\

************************************\


Here is the output of the sed script:


sed -f mysdsccript data


---cut---

hosts

hosts.allow

This file determines which networks and individual

computers can connect to the daemons on the server

***********************************

This is an important file for security that should

be to deny all.

************************************

---cut---


In the script that is run on the file data that was created from a list of files and directories in /etc, you can see that the first sed command looks for the line, hosts.allow and then appends several lines of text to hosts.allow. The append is indicated by the a\. The next sed command then looks for the line with hosts.deny and changes the hosts.deny line to the line indicated in the script. The change is created by using the c\ command.