Chapter 4 (Make my own web site) . Processing Text 185 Using the
Chapter 4 . Processing Text 185 Using the stream editor The sed utility is a stream editor that takes input either from a file or from data that is piped into the utility. The sed utility works globally within a file unless addressing symbols are used to limit the scope of the command. That the utility works globally means that every instance matching the specified pattern is replaced. Addressing can be used to specify the location that is to be searched for a matching pattern. This addressing can specify either a line or a range of lines in a file. You can also exclude line addresses from the search using the exclamation symbol (!). Regular expressions can also be used to specify locations within a file. The sed command can be used to make simple substitutions and more powerful changes to a file. Simple substitutions throughout a file are made using the following syntax: sed -option s/REGEXP/replacement/flag filename The sed command will work using text from standard input as well as text from specified files. The original file is left intact, and the changes are written to a new file. REGEXPstands for regular expression, which is a way of searching for particular characters. Regular expressions are discussed in the following section. The s command instructs the sed utility to locate the REGEXPand remove that while adding the replacementin its place. You can perform multiple substitutions using the -e option. Two examples of how to use this are: sed -e s/lisa/Lisa/ -e s/nikki/Nikki/ myfriends sed -e s/lisa/Lisa/ ; s/nikki/Nikki/ myfriends These commands search the my friends file and replace the characters lisa and nikki with Lisa and Nikki, respectively. You can also use the sed utility to run a script on a file by using the -f option. This enables you to store frequently used options and simplifies larger commands. The f option is used as shown below: sed -f scriptname filename When used with a script, the script must contain the sed command(s) in the following format: s/REGEXP/replacement/flags The various options for the sed utility are covered in Table 4-17.
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.