Archive for November, 2007

252 CHAPTER 15 (Sex offenders web site) WORKING WITH TEXT FILES

Friday, November 16th, 2007

252 CHAPTER 15 WORKING WITH TEXT FILES Figure 15-5. grep is a powerful tool that can search for text within files. Tip You might never choose to use grep for searching for text within files, but it can prove very handy when used to search through the output of other commands. This is done by piping the output from one command to another, as explained in Chapter 17. Using Regular Expressions The true power of grep is achieved by the use of search patterns known as regular expressions, or regexes for short. Put simply, regexes allow you to be vague rather than specific when searching, meaning that grep (and many similar tools that use the system of regexes, such as the find command discussed in Chapter 14) will return more results. For example, you can specify a selection or series of characters (called a string in regex terminology) that might appear in a word or phrase you re searching for. This can be useful if you re looking for a word that might be spelled differently from how you anticipate, for example. The most basic form of regex is the bracket expansion. This is where additional search terms are enclosed in square brackets within a search string. For example, suppose you want to find a file that refers to several drafts of a document you ve been working on. The files are called myfile_1draft.doc, myfile_2draft.doc, and so on. To find any document that mentions these files, you could type this: grep ‘myfile_[1-9]draft.doc’ * The use of square brackets tells grepto fill in details within the search string based on what s inside the square brackets. In this case, 1-9 means that all the numbers from one to nine should be applied to the search string. It s as if you ve told grep to search for myfile_1draft.doc, and

CHAPTER 15 (Multiple domain web hosting) WORKING WITH TEXT FILES USING

Thursday, November 15th, 2007

CHAPTER 15 WORKING WITH TEXT FILES USING GEDIT TO EDIT TEXT FILES If all this talk of visounds like too much hard work, don t forget that the GNOME desktop includes an excellent text editor in the form of Gedit. In fact, to describe Gedit as merely a text editor is to do it something of a disservice because it includes many handy word processor-like features. You can call Gedit and open a file in it from the command-line prompt as follows: gedit If you need to adopt superuser powers to edit the likes of configuration files, simply preface it with sudo: sudo gedit You ll find Gedit fairly straightforward to use. Searching Through Files You can search for particular words or phrases in text files by loading the file into lessor vi (see Table 15-1). The maneuverability offered by both programs lets you leap from point to point in the text, and their use is generally user-friendly. However, using vi or less can take precious seconds. There s a quicker command-line option that will search through a file in double-quick speed: grep. Using grep to Find Text grep stands for Global Regular Expression Print. grep is an extremely powerful tool that can use pattern-based searching techniques to find text in files. Pattern-based searching means that grep offers various options to loosen the search so that more results are returned. The simplest way of using grep is to specify some brief text, followed by the name of the file you want to search. Here s an example: grep ‘helloworld’ myfile This will search for the phrase helloworld within myfile. If it s found, the entire line that helloworld is on will be displayed on screen. If you specify the * wildcard instead of a filename, grep will search every file in the directory for the text. Adding the -r command option will cause grep to search all the files, and also search through any directories that are present: grep r ‘helloworld’ * Another handy command option is -i, which tells grep to ignore uppercase and lowercase letters when it s searching. Figure 15-5 shows an example of using grep.

250 CHAPTER 15 WORKING WITH TEXT FILES (1 on 1 web hosting)

Thursday, November 15th, 2007

250 CHAPTER 15 WORKING WITH TEXT FILES Note If you don t have the correct permissions to write a file, vi might tell you that you can use :w! to override. In this case, it s wrong. The only way to write to a file for which you don t have permissions is to change its permissions. Creating a New Text File Using vi Creating and editing a new file with vi is easy. From any command-line shell, simply type this: vi myfile This will start vi and give your new file a name. However, the file won t be saved until you manually issue the save command (:w) in vi. This means that if your computer crashes before you save, the file will be lost! Note The version of vi provided with Ubuntu, vim, includes some elementary file-save protection. If, for any reason, vim is not shut down properly, there s a chance you ll be able to recover a version of file the next time vim starts. However, as with all such protection in any kind of program, you shouldn t rely on this. You should use the :w command to save your file periodically. As always with vi, you start out in the default Command mode. To start typing immediately, enter Insert mode by typing i. You ll notice when typing that although the text is wrapped on each line, words are not carried over, and they often break across lines in an ugly way. This is because vi is primarily a text editor, not a word processor. For people who create text files, like programmers, having line breaks shown in this way can be useful. When you re finished typing a sentence or paragraph, you can press the Enter key as usual to start a new line. You should then be able to move between lines using the up and down cursor keys. You ll notice an odd thing when you try to do this, however: unlike with a word processor, moving up a line of text that spreads across more than one line on screen will take the cursor to the start of the line, rather than into the middle of it. This again relates to vi s text editor focus, where such a feature is useful when editing documents such as program configuration files. When you re finished, press the Esc key to switch to Command mode. Then type a colon to enter Command-Line mode. Type :w to save the file using the filename you gave it earlier. If you started vi without specifying a filename, you ll need to specify a filename with the save command, such as :w myfile.

CHAPTER 15 (Web hosting services) WORKING WITH TEXT FILES After

Wednesday, November 14th, 2007

CHAPTER 15 WORKING WITH TEXT FILES After this, you need to change the file s permissions, because, by default, this file is read- only for all users (r–r–r–). Use the following command to change the permissions: chmod +w README Then fire up vi with the file, like this: vi README Note Windows makes a lot of use of file extensions in order to recognize files and therefore know what program to use to run them. By default, a file with a .doc extension tells Windows that it should use Microsoft Word to open the file, for example. Linux uses a different system based on the first few bytes of each file. Because of this, file extensions are used within Linux simply to let the users know what type of file they re dealing with. Often, they re not used at all. If a file is called README, you can be fairly certain that it s a text file, for example. Once the file is opened, you ll find yourself automatically in Command mode and will be able to move around the file using the cursor keys. Altering the text is achieved using various commands (see Table 15-1). For example, typing dd will delete the line of text that the cursor is currently within. Typing x will delete the letter under the cursor. Typing dwwill delete the current word under the cursor. Try some of these to see how they work. To actually edit a file and type text, you ll need to switch to Insert mode. Type i to do this. Insert mode is fairly easy to understand. You can move around the text using the cursor keys, and then simply start typing wherever you want. The Backspace key will delete text behind the cursor, and the Delete key will delete text in front of the cursor. When you re finished, press the Esc key to return to Command mode. Once back in Command mode, you can page through the text. The Page Up and Page Down keys will move a screenful of text at a time. Pressing the up and down cursor keys will cause the screen to scroll when the cursor reaches the top or bottom. After you re finished editing, you ll need to save the file. This is done in Command-Line mode. You can enter this mode by typing a colon (:). You ll see a colon appear at the bottom of the screen, and this is where you type the commands. Note that after you type a command, you ll immediately exit Command-Line mode, so if you want to issue another command, you ll need to type a colon again. To save a file, in Command-Line mode, type :w (which stands for write ). If you want to save the current file with a different name, you ll need to enter a filename after the w command, like this: :w mytextfile To quit vi, type :q. However, if you ve edited a file, you won t be able to quit until the file has been saved. If you want to save the file and then quit, you can type :wq. If you don t want to save the file, type :q!. The exclamation point tells vi to override any objections it might have. You can also use it with the save command :w! to force the overwriting of a file that already exists.

Jetty web server - 248 CHAPTER 15 WORKING WITH TEXT FILES

Tuesday, November 13th, 2007

248 CHAPTER 15 WORKING WITH TEXT FILES Figure 15-4. Use vi s Command-Line mode to issue commands. For a list of basic Command-Line mode commands, see Table 15-2. Table 15-2. Some vi Command-Line Mode Commands Command Description :w Save the file :w! Save the file and ignore errors such as an existing file with the same filename :q Quit vi :q! Quit vi and ignore errors such as an unsaved file :s/word/replacement/ Search from the cursor downwards and replace any instances of the word with the replacement 1 :help View help documentation 1 The search tool is very powerful and uses a number of command options for additional flexibility. Read the vi help file to learn more. Using vi to Edit a File As an example, let s use vi to edit the OpenOffice.org README file. You don t want to actually alter this file, so start by making a copy of it in your home directory: cp /usr/lib/openoffice2/README ~ This will copy the file README01 to your /home directory, which you indicate using the ~ symbol.

CHAPTER 15 (How to cite a web site) WORKING WITH TEXT FILES the

Tuesday, November 13th, 2007

CHAPTER 15 WORKING WITH TEXT FILES the commands required to switch into Insert mode is that some let you insert before or after the cursor. Generally, i is most useful, because what you type will appear before the character under the cursor, as with most word processors. The commands that activate Insert Mode are listed in Table 15-1, under Insert Text. Tip By typing A (Shift+A), you can add text to the end of the line on which the cursor currently resides. Figure 15-3. Use vi s Insert mode to add and edit text. In Insert mode, you can still move around the text using the cursor keys. Anything you type will appear the point of the cursor. To quit this mode, press the Esc key. This will return you to Command mode. Command-Line Mode The third mode you should be aware of is Command-Line mode (note that, irritatingly, this is not the same as the Command mode). As its name suggests, this is the mode in which you can enter commands to save and load files, as well as perform other fundamental tasks to control vi or to quit the program. You can enter Command-Line mode by typing a colon (:), although if you re in Insert mode, you ll first need to leave it by pressing the Esc key. You can identify when vi is in this mode because the cursor will be at the bottom of the screen next to a colon symbol, as shown in Figure 15-4. To quit Command-Line mode, press the Esc key. You ll be returned to Command mode. Note that you ll automatically leave Command-Line mode after each command you issue has completed.

246 CHAPTER 15 WORKING WITH TEXT (Web host 4 life) FILES

Monday, November 12th, 2007

246 CHAPTER 15 WORKING WITH TEXT FILES Table 15-1. vi Command Mode Commands (Continued) Command Description Search / Search forward (type the search text directly after the slash) ? Search backwards n Repeat search in a forward direction N Repeat search in a backward direction Cut and Paste yy Copy the current line3 nyy Copy n number of lines into the buffer from the cursor downwards (for example, 5yy copies five lines of text) p Paste the contents of the clipboard3 Insert Text i Switch to Insert mode at the cursor o Switch to Insert mode, placing the cursor below current line O Switch to Insert mode, placing the cursor above current line A Append text to end of line Navigation4 $ Move the cursor to the end of the current line w Move the cursor to the next word b Move the cursor to the previous word Miscellaneous . Repeat the last command u Undo the last command 1 A line ends where a line-break control character occurs in the file. Because of this, a line of text may actually take up several lines of the on-screen display. 2 This will delete the remainder of current word before/after the cursor if the cursor is in the middle of a word. 3 The standard documentation refers to copying as “yanking and the clipboard as the buffer. 4 You can also use the cursor keys to move around the file and the Page Up and Page Down keys to move up and down a page at a time. Additionally, press 0 (zero) on the main keyboard, not the numeric keypad, to move the cursor to the start of the current line, or Shift+0 to move forward one sentence (until the next full stop). Insert Mode To type your own text or edit text, you need to switch to Insert mode. This is normally done by typing i, but you can also type Oor o to change to Insert mode, which is indicated by the word INSERT appearing at the bottom of the screen, as shown in Figure 15-3. The difference between

CHAPTER 15 WORKING WITH TEXT FILES Command (Best web design)

Sunday, November 11th, 2007

CHAPTER 15 WORKING WITH TEXT FILES Command Mode Command mode is vi s central mode. When the editor starts up, it s in Command mode, as shown in Figure 15-2. This lets you move around the text and delete words or lines of text. vi returns to Command mode after most operations. In this mode, the status bar at the bottom of the screen shows information such as the percentage progress through the document. Although you cannot insert text in this mode, you can delete and otherwise manipulate words and lines within the file. You can also move through the text using the cursor keys and the Page Up and Page Down keys. Figure 15-2. In vi, the main mode is Command mode. Table 15-1 shows a list of the commands you can use in Command mode (consider photocopying it and sticking it to the side of your monitor as a handy reference). Table 15-1. vi Command Mode Commands Command Description Delete Text dd Delete current line ndd Delete n number of lines (for example, 5dd will delete five lines)1 dw Delete the current word under the cursor 2 db Delete the word before the cursor2 D Delete everything from the cursor to the end of the line1

244 CHAPTER 15 WORKING WITH TEXT FILES (Business web site)

Saturday, November 10th, 2007

244 CHAPTER 15 WORKING WITH TEXT FILES Using a Command-Line Text Editor A variety of text editors can be used within the shell, but three stand out as being ubiquitous: ed, vi, and Emacs. The first in that list, ed, is by far the simplest. That doesn t necessarily mean that it s simple to use or lacks powerful features, but it just doesn t match the astonishing power of both vi and Emacs. To call viand Emacs simple text editors is to do them a disservice, because both are extremely powerful interactive environments. In particular, Emacs is considered practically an operating system in itself, and some users of Linux treat it as their shell, executing commands and performing everyday tasks, such as reading and sending e-mail from within it. There are entire books written solely about Emacs and vi. Tip A fourth shell-based text editor found on many Linux systems is nano. This offers many word processor-like features that can be helpful if you ve come to Linux from a Windows background. The downside of all the power within Emacs and vi is that both packages can be difficult to learn to use. They re considered idiosyncratic by even their most ardent fans. Both involve the user learning certain unfamiliar concepts, as well as keyboard shortcuts and commands. Although there are debates about which text editor is better and which is best, it s generally agreed that vi offers substantial text-editing power but isn t too all-encompassing. It s also installed by default on Ubuntu. On Ubuntu, Emacs must be installed as an optional extra. Both text editors are normally available on virtually every installation of Linux or Unix. We ll concentrate on using vi here. It s important to understand that there isn t just one program called vi. There are many versions. The original vi program, supplied with Unix, is rarely used nowadays. The most common version of vi is a clone called vim, for vi improved, and this is the version supplied with Ubuntu. However, there are other versions, such as Elvis. Most work in a virtually identical way. Note There s always been a constant flame war between advocates of vi and Emacs, as to which is better. This could be quite a vicious and desperate debate, and the text editor you used was often taken as a measure of your character! Nowadays, the battle between the two camps has softened, and the Emacs versus vi debate is considered an entertaining clich of Linux and Unix use. Declaring online which text editor a user prefers is often followed by a smiley symbol to acknowledge the once-fevered emotions. Understanding vi Modes The key to understanding how vi works is to learn the difference between the various modes. Three modes are important: Command mode, Insert mode, and Command-Line mode.

CHAPTER 15 WORKING WITH TEXT FILES Figure (Web hosting companies)

Saturday, November 10th, 2007

CHAPTER 15 WORKING WITH TEXT FILES Figure 15-1. The tail command can be very useful for viewing the last few lines of a log file. STANDARD INPUT AND OUTPUT If you ve read any of the Ubuntu man pages, you might have seen references to standard input and standard output. Like many things in Ubuntu, this sounds complicated but is merely a long-winded way of referring to something that is relatively simple. Standard input is simply the device that Ubuntu normally takes input from. In other words, on the majority of desktop PCs when you re using the command-line shell, standard input refers to the keyboard. However, it s important to note that it could also feasibly refer to the mouse or any other device on your system capable of providing input; even some software can take the role of providing standard input. Standard output is similar. It refers to the device to which output from a command is usually sent. In the majority of cases at the command line, this refers to the monitor screen, although it could feasibly be any kind of output device, such as your PC s sound card and speakers. The man page for the cat command says that it will concatenate files and print on the standard output. In other words, for the majority of desktop Ubuntu installations, it will combine (concatenate) any number of files together and print the results on screen. If you specify just one file, it will display that single file on your screen. In addition to hardware devices, input can also come from a file containing commands, and output can also be sent to a file instead of the screen, or even sent directly to another command. This is just one reason why the command-line shell is so flexible and powerful.