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 FILESyou can use sed to add further lines. So, if you want to add the following (the -- is a record delimiter):changetype: addobjectclass: localDesktop--you can use this sed line:sed 'a\changetype: add\nobjectclass: localDesktop\n--' ldapmodify.ldifAs it stands, this will also insert the line after any blank lines you may have at the end of your file. Toavoid this, use a match first:sed '/dn/ a\changetype: add\nobjectclass: localDesktop\n--' ldapmodify.ldifThis will match for the characters dn anywhere in the line and then add the relevant lines after onlythe lines that match.To add the specified line before rather than after the searched-for lines, use \i instead of \a.■ Note http://www.grymoire.com/Unix/Sed.html gives a very useful full sed tutorial.9-3. Using awk: Snippets and Quick Referenceawk is a command-line text-processing language that, like sed (see recipe 9-1), can be plugged intocommand lines to process text input and send the output either to files or to another program. It’s agreat little tool, and it is in fact a complete programming language, but if you’re doing more than one ortwo lines in it, you should probably be using Perl instead. As always with <strong>Linux</strong>, use the right tool for thejob. Technically on most <strong>Linux</strong> systems you’ll be using gawk (the GNU version of awk), but invoking it asawk is fine and normal.This is the simplest general form of an awk program:awk [search pattern] { [program actions] } fileThe default action is to print, so if you omit the action, awk will just print the lines that match thesearch pattern:awk /juliet/ mylist.txtThis will print all lines of the file mylist.txt that contain the word juliet. As with sed (recipe 9-2),use forward slashes to delimit the pattern.One of the advantages of awk is that you can use it on files that contain table-type data, for example,the /etc/passwd file. If you’re moving to LDAP, you might want a list of the home directories that userson each system have. To get a list of home directories with their users, you can use this:# cat /etc/passwd | awk -F: '{print $1 "\n" $6}'191Download at WoweBook.Com

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

Saved successfully!

Ooh no, something went wrong!