LPI Linux Certification in a Nutshell Part 11

You’re reading novel LPI Linux Certification in a Nutshell Part 11 online at LightNovelFree.com. Please use the follow button to get notification about the latest chapter next time when you visit LightNovelFree.com. Use F11 button to read novel in full-screen(PC only). Drop by anytime you want to read free – fast – latest novel. It’s great if you could leave a comment, share your opinion about the new chapters, new novel with others on the internet. We’ll do our best to bring you the finest, latest novel everyday. Enjoy!

-f file file file is a is a sed sed script. script.

-g Treat all subst.i.tutions as global.

The sed sed utility operates on text through the use of utility operates on text through the use of addresses addresses and and editing commands editing commands. The address is used to locate lines of text to be operated on, and editing commands modify text. During operation, each line (that is, text separated by newline characters) of input to sed sed is processed individually and without regard to adjacent lines. If multiple editing commands are to be used (through the use of a script file or multiple is processed individually and without regard to adjacent lines. If multiple editing commands are to be used (through the use of a script file or multiple -e -e options), they are all applied in order to each line before moving on to the next line. options), they are all applied in order to each line before moving on to the next line.

Addressing Addresses in sed sed locate lines of text to which commands will be applied. The addresses can be: locate lines of text to which commands will be applied. The addresses can be: A line number (note that sed sed counts lines continuously across multiple input files). The symbol counts lines continuously across multiple input files). The symbol $ $ can be used to indicate the last line of input. A range of line numbers can be given by separating the starting and ending lines with a comma ( can be used to indicate the last line of input. A range of line numbers can be given by separating the starting and ending lines with a comma (start,end). So, for example, the address for all input would be 1,$ 1,$.

A regular expression delimited by forward slashes (/regex/).



A line number with an interval. The form is n n~s, where n n is the starting line number and is the starting line number and s s is the step, or interval, to apply. For example, to match every odd line in the input, the address specification would be is the step, or interval, to apply. For example, to match every odd line in the input, the address specification would be 1~2 1~2 (start at line 1 and match every two lines thereafter). This feature is a GNU extension to (start at line 1 and match every two lines thereafter). This feature is a GNU extension to sed sed.

If no address is given, commands are applied to all input lines by default. Any address may be followed by the ! ! character, applying commands to lines that character, applying commands to lines that do not match do not match the address. the address.

Commands The sed sed command immediately follows the address specification if present. Commands generally consist of a single letter or symbol, unless they have arguments. Following are some basic command immediately follows the address specification if present. Commands generally consist of a single letter or symbol, unless they have arguments. Following are some basic sed sed editing commands to get you started. editing commands to get you started.

d Delete lines.

s Make subst.i.tutions. This is a very popular sed sed command. The syntax is as follows: command. The syntax is as follows:s/pattern/replacement/[flags]The following flags flags can be specified for the can be specified for the s s command: command:gReplace all instances of pattern pattern, not just the first.nReplace n nth instance of pattern pattern; the default is 1.pPrint the line if a successful subst.i.tution is done. Generally used with the -n -n command-line option. command-line option.w file filePrint the line to file file if a successful subst.i.tution is done. if a successful subst.i.tution is done.yTranslate characters. This command works in a fas.h.i.+on similar to the tr tr command, described earlier. command, described earlier.

Example 1 Delete lines 3 through 5 of file1 file1: $sed'3,5d'file1 Example 2 Delete lines of file1 file1 that contain a that contain a # # at the beginning of the line: at the beginning of the line: $sed'/^#/d'file1 Example 3 Translate characters: y/abc/xyz/ Every instance of a a is translated to is translated to x x, b b to to y y, and c c to to z z.

Example 4 Write the @ @ symbol for all empty lines in symbol for all empty lines in file1 file1 (that is, lines with only a newline character but nothing more): (that is, lines with only a newline character but nothing more): $sed's/^$/@/'file1 Example 5 Remove all double quotation marks from all lines in file1 file1: $sed's/"//g'file1 Example 6 Using sed sed commands from external file commands from external file sedcmds sedcmds, replace the third and fourth double quotation marks with ( ( and and ) ) on lines 1 through 10 in on lines 1 through 10 in file1 file1. Make no changes from line 11 to the end of the file. Script file sedcmds sedcmds contains: contains: 1,10{ s/"/(/3 s/"/)/4 } The command is executed using the -f -f option: option: $sed-fsedcmdsfile1 This example employs the positional flag for the s s (subst.i.tute) command. The first of the two commands subst.i.tutes (subst.i.tute) command. The first of the two commands subst.i.tutes ( ( for the third double-quote character. The next command subst.i.tutes for the third double-quote character. The next command subst.i.tutes ) ) for the fourth double-quote character. Note, however, that the position count is interpreted for the fourth double-quote character. Note, however, that the position count is interpreted independently independently for each subsequent command in the script. This is important because each command operates on the results of the commands preceding it. In this example, since the third double quote has been replaced with for each subsequent command in the script. This is important because each command operates on the results of the commands preceding it. In this example, since the third double quote has been replaced with ( (, it is no longer counted as a double quote by the second command. Thus, the second command will operate on the fifth fifth double quote character in the original double quote character in the original file1 file1. If the input line starts out with the following: """"""

after the first command, which operates on the third double quote, the result is this: ""("""

At this point, the numbering of the double-quote characters has changed, and the fourth double quote in the line is now the fifth character. Thus, after the second command executes, the output is as follows: ""(")"

As you can see, creating scripts with sed sed requires that the sequential nature of the command execution be kept in mind. requires that the sequential nature of the command execution be kept in mind.

If you find yourself making repet.i.tive changes to many files on a regular basis, a sed sed script is probably warranted. Many more commands are available in script is probably warranted. Many more commands are available in sed sed than are listed here. than are listed here.

Name Anchors Description Anchors are used to describe position information. Table6-8 Table6-8, shown earlier, lists anchor characters.

Example 1 Display all lines from file1 file1 where the string where the string Linux Linux appears at the start of the line: appears at the start of the line: $grep'^Linux'file1 Example 2 Display lines in file1 file1 where the last character is an where the last character is an x x: $grep'x$'file1 Display the number of empty lines in file1 file1 by finding lines with nothing between the beginning and the end: by finding lines with nothing between the beginning and the end: $grep-c'^$'file1 Display all lines from file1 file1 containing only the word containing only the word null null by itself: by itself: $grep'^null$'file1

Name Groups and ranges Description Characters can be placed into groups and ranges to make regular expressions more efficient, as shown in Table6-10 Table6-10, previously.

Example 1 Display all lines from file1 file1 containing containing Linux Linux, linux linux, TurboLinux TurboLinux, and so on: $grep'[Ll]inux'file1 Example 2 Display all lines from file1 file1 that contain three adjacent digits: that contain three adjacent digits: $grep'[0-9][0-9][0-9]'file1 Example 3 Display all lines from file1 file1 beginning with any single character other than a digit: beginning with any single character other than a digit: $grep'^[^0-9]'file1 Example 4 Display all lines from file1 file1 that contain the whole word that contain the whole word Linux Linux or or linux linux, but not LinuxOS LinuxOS or or TurboLinux TurboLinux: $grep'<[ll]inux>'file1 Example 5 Display all lines from file1 file1 with five or more characters on a line (excluding the newline character): with five or more characters on a line (excluding the newline character): $grep'.....'file1 Example 6 Display all nonblank lines from file1 file1 (i.e., that have at least one character): (i.e., that have at least one character): $grep'.'file1 Example 7 Display all lines from file1 file1 that contain a period (normally a metacharacter) using an escape: that contain a period (normally a metacharacter) using an escape: $grep'.'file1

Name Modifiers Description Modifiers change the meaning of other characters in a regular expression. Table6-11 Table6-11, shown previously, lists these modifiers.

Example 1 Display all lines from file1 file1 that contain that contain ab ab, abc abc, abcc abcc, abccc abccc, and so on: $grep'abc*'file1 Example 2 Display all lines from file1 file1 that contain that contain abc abc, abcc abcc, abccc abccc, and so on, but not ab ab: $grep'abcc*'file1 Example 3 Display all lines from file1 file1 that contain two or more adjacent digits: that contain two or more adjacent digits: $grep'[0-9][0-9][0-9]*'file1 or: $grep'[0-9]{2,}'file1 Example 4 Display lines from file1 file1 that contain that contain file file (because (because ? ? can match zero occurrences), can match zero occurrences), file1 file1, or file2 file2: $grep'file[12]?'file1 Example 5 Display all lines from file1 file1 containing at least one digit: containing at least one digit: $grep'[0-9]+'file1 Example 6 Display all lines from file1 file1 that contain that contain 111 111, 1111 1111, or 11111 11111 on a line by itself: on a line by itself: $grep'^1{3,5}$'file1 Example 7 Display all lines from file1 file1 that contain any three-, four-, or five-digit number: that contain any three-, four-, or five-digit number: $grep'<[0-9]{3,5}>'file1 Example 8 Display all lines from file1 file1 that contain that contain Happy Happy, happy happy, Sad Sad, sad sad, Angry Angry, or angry angry: $grep-E'[Hh]appy [Ss]ad [Aa]ngry'file1 Example 9 Display all lines of file file that contain any repeated sequence of that contain any repeated sequence of abc abc ( (abcabc, abcabcabc abcabcabc, and so on): $grep'(abc){2,}'file You may find it useful to employ the GNU option --color --color to to grep grep when working with regular expressions. It prints the section of the string that matched your regular expression in a different color, so you can see exactly what when working with regular expressions. It prints the section of the string that matched your regular expression in a different color, so you can see exactly what grep grep was looking for. was looking for.

Name Basic regular expression patterns Example 1 Match any letter: [A-Za-z]

Example 2 Match any symbol (not a letter or digit): [^0-9A-Za-z]

Example 3 Match an uppercase letter, followed by zero or more lowercase letters: [A-Z][a-z]*

Example 4 Match a U.S. Social Security Number (123-45-6789) by specifying groups of three, two, and four digits separated by dashes: [0-9]{3}-[0-9]{2}-[0-9]{4} Example 5 Match a dollar amount, using an escaped dollar sign, zero or more s.p.a.ces or digits, an escaped period, and two more digits: $[0-9]*.[0-9]{2} Example 6 Match the month of June and its abbreviation, Jun Jun. The question mark matches zero or one instance of the e e: June?On the ExamMake certain you are clear about the difference between file globbing file globbing and the use of regular expressions. and the use of regular expressions.

Name Using regular expressions as addresses in sed These examples are commands you would issue to sed sed. For example, the commands could take the place of command1 command1 in this usage: in this usage: $sed[options]'command1'[files]

These commands could also appear in a standalone sed sed script. script.

Example 1 Delete blank lines: /^$/d Example 2 Delete any line that doesn't contain #keepme #keepme: /#keepme/!d Example 3 Delete lines containing only whites.p.a.ce (s.p.a.ces or Tabs). In this example, Tab Tab means the single Tab character and is preceded by a single s.p.a.ce: means the single Tab character and is preceded by a single s.p.a.ce: /^[Tab]*$/d Because GNU sed sed also supports character cla.s.ses, this example could be written as follows: also supports character cla.s.ses, this example could be written as follows: /^[[:blank:]]*$/d Example 4 Delete lines beginning with periods or pound signs: /^[.#]/d Example 5 Subst.i.tute a single s.p.a.ce for any number of s.p.a.ces wherever they occur on the line: s/*//g or: s/{2,}//g Example 6 Subst.i.tute def def for for abc abc from line 11 to 20, wherever it occurs on the line: from line 11 to 20, wherever it occurs on the line: 11,20s/abc/def/g Example 7 Translate the characters a a, b b, and c c to the to the @ @ character from line 11 to 20, wherever they occur on the line: character from line 11 to 20, wherever they occur on the line: 11,20y/abc/@@@/

Objective 8: Perform Basic File Editing Operations Using vi vi is perhaps the most ubiquitous text editor available on Linux systems. Since most system administration tasks eventually require editing text files, being able to work effectively in is perhaps the most ubiquitous text editor available on Linux systems. Since most system administration tasks eventually require editing text files, being able to work effectively in vi vi is essential. is essential.

This Objective concentrates on a subset of vi vi functionality. functionality. Learning the vi and Vim Editors (O'Reilly) is an indispensable reference for anyone interested in learning more about (O'Reilly) is an indispensable reference for anyone interested in learning more about vi vi and the enhancements available in its various implementations. There is also a large amount of doc.u.mentation available at and the enhancements available in its various implementations. There is also a large amount of doc.u.mentation available at http://vimdoc.sourceforge.net and and http://www.vim.org for the popular for the popular vi vi implementation Vim, most of which is applicable to any version of implementation Vim, most of which is applicable to any version of vi vi.

Invoking vi To start vi vi, simply execute it. You will be editing a temporary file. To directly edit one or more files, give the names of the files on the command line: $vifile1.txtfile2.txt You are presented with a main window showing the contents of file1.txt file1.txt, or if the specified files don't already exist, a blank screen with tilde (~) characters running the length of the left column (they indicate areas of the screen containing no text, not even blank lines).

vi Basics The vi vi editor has two modes of operation: editor has two modes of operation: command command or or insert insert. In command mode, vi vi allows you to navigate around your file and enter commands. To enter new text, put allows you to navigate around your file and enter commands. To enter new text, put vi vi into insert mode. In command mode, the keyboard keys are interpreted as into insert mode. In command mode, the keyboard keys are interpreted as vi vi commands instead of text. The convenience of being able to manipulate the editor without moving your hands from the keyboard is considered one of commands instead of text. The convenience of being able to manipulate the editor without moving your hands from the keyboard is considered one of vi vi's strengths.

Commands are brief, case-sensitive combinations of one or more letters. For example, to switch from command to insert mode, press the "i" key. To terminate insert mode, press the Escape key (Esc), which puts you back in command mode.

Almost any command can be prefixed with a number to repeat the command that number of times. For example, r r will replace the character at the current cursor position. To replace exactly 10 characters, use will replace the character at the current cursor position. To replace exactly 10 characters, use 10r 10r. Commonly used vi vi commands are listed in commands are listed in Table6-12 Table6-12.

Table6-12.vi commands

Key command Description h or left arrow or left arrow Move left one character.

j or down arrow or down arrow Move down one line.

k or up arrow or up arrow Move up one line.

l or right arrow or right arrow Move right one character.

H Move to the top of the screen.

L Move to the bottom of the screen.

G Move to the end of the file.

w Move forward one word.

b Move backward one word.

0 (zero) (zero) Move to the beginning of the current line.

^ Move to the first nonwhites.p.a.ce character on the current line.

$ Move to the end of the current line.

Ctrl-B Move up (back) one screen.

Ctrl-F Move down (forward) one screen.

i Insert at the current cursor position.

I Insert at the beginning of the current line.

a Append after the current cursor position.

A Append to the end of the current line.

o Start a new line after the current line.

O Start a new line before the current line.

r Replace the character at the current cursor position.

R Start replacing (overwriting) at the current cursor position.

x Delete the character at the current cursor position.

X Delete the character immediately before (to the left) of the current cursor position.

s Delete the character at the current cursor position and go into insert mode. (This is the equivalent of the combination xi.) S Delete the contents of the current line and go into insert mode.

dX Given a movement command X, cut (delete) the appropriate number of characters, words, or lines from the current cursor position.

dd Cut the entire current line.

D Cut from the current cursor position to the end of the line. (This is equivalent to d$.) cX Given a movement command X, cut the appropriate number of characters, words, or lines from the current cursor position and go into insert mode.

cc Cut the entire current line and go into insert mode.

C Cut from the current cursor position to the end of the line and enter insert mode. (This is equivalent to c$.) yX Given a movement command X, copy (yank[a]) the appropriate number of characters, words, or lines from the current cursor position.

yy or or Y Y Copy the entire current line.

p Paste after the current cursor position.

P Paste before the current cursor position.

Repeat the last command.

u Undo the last command.[b]

/regex Search forward for regex.

?regex Search backward for regex.

n Find the next match.

N Find the previous match. (In other words, repeat the last search in the opposite direction.) :n Next file; when multiple files are specified for editing, this command loads the next file. Force this action (if the current file has unsaved changes) with :n!.

:e file file Load file in place of the current file. Force this action with in place of the current file. Force this action with :e! :e! file file.

:r file file Insert the contents of file after the current cursor position. after the current cursor position.

:q Quit without saving changes. Force this action with :q!.

:w file file Write the current buffer to file. To append to an existing file, use :w >> :w >>file. Force the write (when possible, such as when running as root root) with :w! :w! file file.

:wq Write the file contents and quit. Force this action with :wq!.

:x Write the file contents (if changed) and quit (the ex equivalent of equivalent of ZZ ZZ).

ZZ Write the file contents (if changed) and quit.

:! command command Execute command in a subsh.e.l.l. in a subsh.e.l.l.

[a] Emacs users should be careful not to confuse the Emacs users should be careful not to confuse the vi vi definition of yank (copy) with that of Emacs (paste). definition of yank (copy) with that of Emacs (paste).

[b] Many of the popular Many of the popular vi vi implementations support multilevel undo. Vim breaks compatibility with traditional implementations support multilevel undo. Vim breaks compatibility with traditional vi vi by making a repeated by making a repeated u u perform another level of undo. Nvi uses perform another level of undo. Nvi uses . . after after u u to do multilevel undo and, like traditional to do multilevel undo and, like traditional vi vi, uses a repeated u u to redo (undo the undo, so to speak). This can cause some confusion when moving between Linux distributions that have different default implementations of to redo (undo the undo, so to speak). This can cause some confusion when moving between Linux distributions that have different default implementations of vi vi.

NoteKeep in mind that this is not not a complete list, but it is not necessary to know every a complete list, but it is not necessary to know every vi vi command to use it effectively. In fact, even after using command to use it effectively. In fact, even after using vi vi as your only editor for years, you may find yourself using only a small subset of the available commands. as your only editor for years, you may find yourself using only a small subset of the available commands.

There is a pattern in vi vi's keyboard commands that makes them easier to remember. For every lowercase character that has some action a.s.signed to it, the same uppercase character usually usually has some related action a.s.signed to it. As an example, has some related action a.s.signed to it. As an example, i i and and I I both put both put vi vi in insert mode, at the current cursor position and at the beginning of the line, respectively. in insert mode, at the current cursor position and at the beginning of the line, respectively.

On the ExamYou'll need to be familiar with vi vi's command and insert modes, how to switch between them, and how to perform basic navigation and editing tasks.

LPI Linux Certification in a Nutshell Part 11

You're reading novel LPI Linux Certification in a Nutshell Part 11 online at LightNovelFree.com. You can use the follow function to bookmark your favorite novel ( Only for registered users ). If you find any errors ( broken links, can't load photos, etc.. ), Please let us know so we can fix it as soon as possible. And when you start a conversation or debate about a certain topic with other people, please do not offend them just because you don't like their opinions.


LPI Linux Certification in a Nutshell Part 11 summary

You're reading LPI Linux Certification in a Nutshell Part 11. This novel has been translated by Updating. Author: Adam Haeder already has 826 views.

It's great if you read and follow any novel on our website. We promise you that we'll bring you the latest, hottest novel everyday and FREE.

LightNovelFree.com is a most smartest website for reading novel online, it can automatic resize images to fit your pc screen, even on your mobile. Experience now by using your smartphone and access to LightNovelFree.com

RECENTLY UPDATED NOVEL