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 FILESYou can now use these commands, and all should work seamlessly:less file.txttgless file.tar.gztless file.tarzless file.gz■ Note There’s also a helper application, lesspipe, that you can use and that will handle both .tar and .gz.Since Ubuntu 8.04 LESSOPEN is set to use this by default.9-2. Using the power of sedsed is a powerful stream editor. It can edit on the fly, reading from a file or from standard input. It’sincredibly complicated, and the man page is not helpful, but remember that you can learn to use it alittle at a time. Anything you learn when using sed will come in handy when using regular expressionselsewhere, including in languages such as Perl and Ruby. If this quick run-through interests you, entirebooks have been written about sed from which you can get more information!Importantly, sed is not interactive. You create your changes on the command line as a commandand then pipe the file/input stream through. sed will output to stdout by default, or you can redirect it toa file:sed command filename.txt > output.txtIt doesn’t edit the existing file in place. You can tell it to do so with this:sed command -ibak filename.txtThis will edit filename.txt in place and save a backup copy of the old file at filename.txt.bak.One of its main uses from a sysadmin point of view is as part of a bash file or as part of a complicatedcommand line.■ Note Remember that sed operates line by line and command by command. So, it will perform all thecommands it can on every line before moving on to the next line. It’s important to think about the order you wantcommands to be performed in. If you want to change foo bar to foo baz and then each foo to goo, it’s importantto do it in that order, or there won’t be any foo bar to find and alter.9-2a. Deleting LinesTo delete lines, you use d as the command. For example, if you do an LDAP search, you will usually get ahandful of comment lines at the start of the output. Here’s an example:187Download at WoweBook.Com

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

Saved successfully!

Ooh no, something went wrong!