24.07.2018 Views

Bash-Beginners-Guide

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />

Multiple find and replace commands are separated with individual -e options:<br />

sandy ~> sed -e 's/erors/errors/g' -e 's/last/final/g' example<br />

This is the first line of an example text.<br />

It is a text with errors.<br />

Lots of errors.<br />

So much errors, all these errors are making me sick.<br />

This is a line not containing any errors.<br />

This is the final line.<br />

sandy ~><br />

Keep in mind that by default sed prints its results to the standard output, most likely your terminal window. If<br />

you want to save the output to a file, redirect it:<br />

sed option 'some/expression' file_to_process > sed_output_in_a_file<br />

More examples<br />

Plenty of sed examples can be found in the startup scripts for your machine, which are usually in<br />

/etc/init.d or /etc/rc.d/init.d. Change into the directory containing the initscripts on your<br />

system and issue the following command:<br />

grep sed *<br />

5.3. Non-interactive editing<br />

5.3.1. Reading sed commands from a file<br />

Multiple sed commands can be put in a file and executed using the -f option. When creating such a file,<br />

make sure that:<br />

• No trailing white spaces exist at the end of lines.<br />

• No quotes are used.<br />

• When entering text to add or replace, all except the last line end in a backslash.<br />

5.3.2. Writing output files<br />

Writing output is done using the output redirection operator >. This is an example script used to create very<br />

simple HTML files from plain text files.<br />

sandy ~> cat script.sed<br />

1i\<br />

\<br />

sed generated html\<br />

\<br />

<br />

$a\<br />

\<br />

\<br />

<br />

sandy ~> cat txt2html.sh<br />

#!/bin/bash<br />

Chapter 5. The GNU sed stream editor 66

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!