10.2. Calling sed

You can invoke sed in three ways: by typing the commands on the command line; by inserting sed commands in a script file then calling sed; or by inserting sed commands in a script file and making the script executable.

To use sed from the command line for one-liners use:


sed [options] 'sed-commands' input-file(s) 

Remember when using sed commands on the command line to surround the actual commands with single quotes, although sed will tolerate double quotes.

To use a sed script file use this format:


sed [ options] -f sed-script-file input-file(s) 

To use a sed script file that has the sed command interpreter line as the first line of the script file use this format:


sed-script-file [options] input-file(s) 

Whether you are running a sed script file or sed commands from the shell, if you do not specify an input file sed will take its input from the standard input. This is generally either from the keyboard or from a redirection.

The sed options are as follows:

-n No print please; sed will not write the edited lines to the standard output. The default is to print ‘ all ’ (edited and non-edited) lines. The p command (more on this later) can be used to print the edited lines.
-e The next command is an editing command. This option is used if you are using multiple edits. If you are using only one sed command this option is not necessary, but if you specify it sed will not complain.
-f Use this if you are calling a sed script file. It lets sed know a script file will hold all the sed commands. Here’s an example: sed -f myscript.sed input-file, where myscript.sed is the file holding the sed commands.

10.2.1. Saving sed output

If the original file is not touched, and you want to save the contents of the changes simply redirect the output to a file. The following example redirects all output from the sed commands to a file called ‘ myoutfile ’. Use this method when you are happy with the results you are getting.

							$ sed 'some-sed-commands' input-file > myoutfile
						

10.2.2. Ways to find text in a file using sed

When sed scans an input-file, it will start by default at the first line. There are two ways to locate text:

  1. Using line numbers. These can be either a single number, or a range of line numbers.

  2. Using regular expressions. See Chapter 7 for more information on how these patterns are constructed.

Table 10.1 gives some of the ways to locate text using sed.

Table 10.1. Ways to locate text in a file using sed
x When x is a line number, e.g. 1
x,y Address a range of lines from x to y, e.g. 2,5 from line 2 to line 5
/pattern/ Find lines containing a pattern, e.g. /disk/ or /[a-z]/
/pattern/pattern/ Find lines containing these two patterns, e.g. /disk/disks/
/pattern/,x Find lines containing a pattern on the given line number, e.g. /ribbon/,3
x,/pattern/ Find matching lines by line number and a pattern, e.g. 3./vdu/
x,y! Find lines except those specified by x and y line numbers, e.g. 1,2!

10.2.3. Basic sed editing commands

Table 10.2. Sed ’s editing commands
p Print the matched lines
= Display the line number of the file
a Append the text after the addressed line
i Insert new text after the addressed line
d Delete addressed lines
c Replace addressed text with new text
s Substitute pattern with replacement pattern
r Read text from another file
w Write text to file
q Quit after first pattern has been matched, or just quit
l Show control characters in their octal ASCII page equivalent
{ } Group a series of commands to be performed only on addressed lines
n Read the next line of text from another file and append it
g Paste the contents of pattern2 into pattern1
y Translate characters
n Append next input line; this allows pattern matching across two lines

Below is a text file called quote.txt that we will use as we go through the sed examples, unless otherwise stated.

							$ pg quote.txt 
The honeysuckle band played all night long for only $90. 
It was an evening of splendid music and company. 
Too bad the disco floor fell through at 23:10. 
The local nurse Miss P.Neave was in attendance.
						

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.206.10