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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 9 ■ WORKING WITH TEXT IN FILES# extended LDIF## LDAPv3# base with scope subtree# filter: (uid=jkemp)# requesting: ALL## jkemp, People, example.comdn: uid=jkemp,ou=People,dc=example,dc=comIf you want to do something with the information, it would be beneficial to lose at least those firsteight lines. This command will do that:ldapsearch "(uid=testuser)" | sed 1,8dThe syntax for a range is [first-line-#],[last-line-#].sed’s main power is in its regular expressions. To delete all lines that start with #, use this:ldapsearch "(uid=testuser)" | sed '/^#/d'The /pattern/ syntax is used to match regular expressions. ^ represents the start of the line. To alsoremove all the blank lines, use this:ldapsearch "(uid=testuser)" | sed '/^#/d' | sed '/^$/d'$ represents the end of the line, so here the pattern looked for is a line whose start and end havenothing between them.9-2b. Substitutionssed becomes more useful when you start using its editing capacity to edit within lines. The basic syntaxfor doing this is as follows:sed -e 's/foo/bar/g' filename.txtThis will replace all occurrences of foo in filename.txt with bar. The g means that the change ismade globally. Without it, only the first occurrence in each line will be edited.This syntax can be incredibly powerful, because you can use regular expressions and backreferences with it. For example, say that you use server-side includes in your web pages, so you have lotsof lines that look like this in your web site files for various values of filename (header, footer, and so on):However, for the local/directory of the web tree, you now want to use a different set of includes, soyou want to change filename.shtml to filename-local.shtml in all the files in this directory. This sed linewill do the trick:sed -i.bak 's/includes\/\(\w*\).shtml/includes\/\1-local.shtml/' local/*.html188Download at WoweBook.Com

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

Saved successfully!

Ooh no, something went wrong!