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 FILESThis snippet will print only the lines that match your search string (set in line 05) to the output file.(Alternatively, use awk to do this; see recipe 9-3.) To append to OUTPUT rather than overwriting, replaceline 07 with the following:open (OUTPUT, ">>outputfile");You can put a + in front of the > or < to indicate that you want both read and write access to the file.+< is almost always preferred for read-write updates, because the +> mode would clobber the file first, sothere won’t be anything there to read from.9-4a. Perl, Files, and Command-Line OptionsA couple of command-line options make Perl particularly useful for editing files, enabling you to dosome quite powerful things without having to leave the command line.Specifically, the -p option sets Perl up as a stream editor, rather like sed (see recipe 9-2) and awk (seerecipe 9-3). It prints out each line that it processes as it processes it. -n does the same thing except itsuppresses the printing (so if you want output, you have to put in a print statement). This one-liner doesexactly the same thing as the previous script:perl -ne 'print if ! /192.168/' < /var/log/auth.log > testout.txtIt checks each line of /var/log/auth.log, and if it doesn’t find part of the line that matches 192.168,it prints it to standard out, which is then redirected to testout.txt.Unlike a traditional editor, because Perl is reading the file in only a single line at a time, it can dealwith enormous files quite happily and without any memory impact. So, if you have a couple of monthsof Apache log files to deal with, you can search through them and edit them with a Perl one-liner likethis, where you might have to wait for a noticeable time to get vim to open up the file.To some extent, this does much the same as sed and awk. Perl syntax may be more familiar to you,and Perl is a little more powerful. However, its power also means that if you’re doing somethingstraightforwardly text-based, sed or awk may require less coding. To some extent, it depends on whichyou get along with best!You can edit a file with Perl as well. For example, say someone wants a long document on a webpage to have all the references to widgets changed to wodgets. Use this one-liner:perl -pe 's/widget/wodget/g' < biglist.html > newbiglist.htmlThe g at the end of the search command means that the search and replace is to be done globally.Otherwise, it’ll just be done on the first instance of every line. We use -p here because there’s no explicitprint statement in the code. We want each line to be printed with the change made (any lines that arenot changed will also be printed, which again is what we want here).■ Note You can edit a file in place with the -i option, overwriting the previous version. However, if somethinggoes wrong, you’ve lost your file, and you’ll have to restore from backup or from your version control systemrepository. If you use -i.bak, it’ll save a backup copy as filename.bak before making any changes.194Download at WoweBook.Com

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

Saved successfully!

Ooh no, something went wrong!