13.07.2015 Views

Linux System Administration Recipes A Problem-Solution Approach

Linux System Administration Recipes A Problem-Solution Approach

Linux System Administration Recipes A Problem-Solution Approach

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

CHAPTER 9 ■ WORKING WITH TEXT IN FILESThe -i.bak edits the file in place, as shown previously. This will also keep a filename.bak backupcopy of the original file; to omit this, just use -i. The matched brackets \( and \) mark off the backreferences. So, the first section looks for the following and then saves filename as the first back reference:includes/filename.shtmlThe forward slash has to be escaped with a backslash (thus \/) to avoid it being interpreted as theterminator of the search pattern. \w is any word character (alphanumerics plus underscore), and \w*means any number of word characters, terminated with .shtml. See Table 9-1 for a quick description ofregexp syntax in sed.In the second half of the substitution, \1 refers to the first back reference (the file name). So, asrequired, you get filename.shtml being replaced with filename-local.shtml. Note that you don’t havethe g option at the end, because there should be only one file name per include line.■ Note To make the forward and backward slashes a little less confusing here, you can use another character forthe search/replace delimiter, such as s###g instead of s///g. So, the previous command line would become thefollowing:sed -i.bak 's#includes/\(\w*\).shtml#includes/\1-local.shtml#' local/*.htmlTable 9-1. sed Regexp SyntaxCharacterDescription. Any character on the input line.^Doesn’t match a character but represents the start of the line.$ Doesn’t match a character but represents the end of the line.\ Use to escape special meaning. \. means a literal ., and \/ means aliteral /.\( \) Use to mark off a back reference in the first part of the expression.\n (where n is a number 1–9) The nth back reference in the previous part of the expression (see theprevious table entry and main recipe), as in \1.exp*Zero or more strings matching the expression exp (so A* matches zeroor more A characters).189Download at WoweBook.Com

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

Saved successfully!

Ooh no, something went wrong!