Bresnahan Ch. 4 Pt 2
A practical example of redirecting STDOUT and STDIN involves the diff utility, covered in Chapter 3. The diff utility allows you to discover any disparities between two text fi les and change the differing text fi le so that the two fi les are identical. It involves a few steps. The fi rst ones are shown in Listing 4.19 along with extra explanatory commands: (show commands)
"pr -mtl 15 numbers.txt random.txt" "cp numbers.txt n.txt" "diff -e n.txt random.txt > switch.sh" "ed n.txt < switch.sh"
Commands to sort input until the text "EOF" is entered:
"sort <<EOF" "dog" "cat" "fish" "EOF" (a sorted list is outputted from the command).
vim command mode keystroke with description: (Move cursor to end of line.)
$
redirection operator with description: (Redirect STDOUT and STDERR to specified file. If file exists, overwrite it. If it does not exist, create it.)
&>
The vim editor has three standard modes as follows: - Insert Mode Insert mode is also called edit or entry mode. This is the mode where you can perform simple editing. There are not many commands or special mode keystrokes. You enter this mode from command mode, by pressing the I key. At this point, the message __ will display in the message area.
--Insert--
The vim editor has three standard modes as follows: (describe)
-Command Mode: This is the mode vim uses when you fi rst enter the buffer area; this is sometimes called normal mode. Here you enter keystrokes to enact commands. For example, pressing the J key will move your cursor down one line. This is the best mode to use for quickly moving around the buffer area. - Insert Mode Insert mode is also called edit or entry mode. This is the mode where you can perform simple editing. There are not many commands or special mode keystrokes. You enter this mode from command mode, by pressing the I key. At this point, the message --Insert-- will display in the message area. You leave this mode by pressing the Esc key. -Ex Mode: This mode is sometimes also called colon commands because every command entered here is preceded with a colon ( : ). For example, to leave the vim editor and not save any changes you type :q and press the Enter key.
The gawk utility is also a stream editor, but it provides a more powerful editing process via its programming language. With the gawk programming language, you can do the following:(4)
-Define variables to store data. -Use arithmetic and string operators to work on data. -Use programming structures, such as loops, to add logic to your processing. -Create formatted reports from data.
long and short gawk options with description: (Specify the delimiter that separates the data file's fields.)
-F <x> ; --field-separator <x>
The sed editor changes data based on commands either entered into the command line or stored in a text fi le. The process the editor goes through is as follows: (4 steps)
-Reads one text line at a time from the input stream - Matches that text with the supplied editor commands -Modifies the text as specified in the commands -Outputs the modified text to STDOUT (After the sed editor matches all the prespecifi ed commands against a text line, it reads the next text line and repeats the editorial process. Once sed reaches the end of the text lines, it stops.)
Similar to sed , you can provide the program on the same command line as the gawk command. It also employs the use of single quotation marks to enclose the script. However, unlike sed , the gawk utility requires you to put your programming language commands between two curly braces. An example is shown in Listing 4.38. The print command displays text to STDOUT, but notice that different parts of STDIN are shown, as shown in Listing 4.38. This is accomplished via the gawk utility's defi ned data fi eld variables. They are defi ned as follows: (4) (describe)
-The $0 variable represents the entire text line. -The $1 variable represents the text line's first data field. -The $2 variable represents the text line's second data field. -The $ n variable represents the text line's n th data field.
In Figure 4.1 you can see the four main sections of the nano text editor. They are as follows: (4) (describe)
-Title Bar: The title bar is at the nano text editor window's top line. It shows the current editor version as well as the name of the fi le you are presently editing. In Figure 4.1, the fi le being edited is the numbers.txt fi le. If you simply typed in nano and did not include a fi le name, you would see New Buffer in the title bar. -Main Body: The nano text editor's main body is where you perform the editing. If the fi le already contains text, its fi rst lines are shown in this area. If you need to view text that is not in the main body, you can use either arrow keys, Page Up or Down key, and/or the page movement shortcut key combinations to move through the text. -Status Bar: The status bar does not always contain information. It only displays status information for certain events. For example, in Figure 4.1 , the text fi le has just been opened in nano , so the status bar area displays [ Read 5 lines ] to indicate that fi ve text lines were read into the editor. -Shortcut List: The shortcut list is one of the editor's most useful features. By glancing at this list at the window's bottom, you can see the most common commands and their associated shortcut keys. The caret (^) symbol in this list indicates that the Ctrl key must be used. For example, to move down a page, you press and hold the Ctrl key and then press the V key. To see additional commands, press the Ctrl+G key combination for help.
long and short sed options with description: ( Add commands in <<sccript>> to text processing. The <<sccript>> is written as part of the sed command.)
-e <<sccript>> ; --expression=<<sccript>>
long and short sed options with description: (Add commands in <<sccript>> to text processing. The <<sccript>> is a file.)
-f <<sccript>> ; --file=<<sccript>>
long and short gawk options with description: (Use program in file for text processing.)
-f <file> ; --file=<file>
long and short sed options with description: ( Use extended regular expressions in <<sccript>>.)
-r ; --regexp-extended
long and short gawk options with description: (Execute gawk program in sandbox mode.)
-s ; --sandbox
Command to find files inside the "tmp" directory that are empty and list them: (3 ways)
1. "find tmp -size 0 | xargs /usr/bin/ls" 2. "ls $(find tmp -size 0)" 3. ls `find tmp -size 0`
Lines to put in a gawk script to replace fourth field of "cake.txt" with word "donuts" if it is currently "cake." and replace fifth field with "donuts" if it is currently "cake." and print the lines if either of these options takes place: (numbered lines)
1. {if ($4=="cake.") 2. {$4="donuts"; print $0} 3. else if ($5=="cake.") 4. {$5="donuts"; print $0}}
vim Ex mode command keystrokes with description: (Execute shell command and display results, but don't quit editor.)
:! <command>
vim Ex mode command keystrokes with description: (Quit editor without writing buffer to file.)
:q
vim Ex mode command keystrokes with description: (Quit editor without writing buffer to file (overrides protection).)
:q!
vim Ex mode command keystrokes with description: ( Read < file> contents and include them in editor buffer area.)
:r <file>
vim Ex mode command keystrokes with description: (Execute shell <command> and include the results in editor buffer area.)
:r! <command>
vim Ex mode command keystrokes with description: (Write buffer to file and stay in editor.)
:w
vim Ex mode command keystrokes with description: (Write buffer to file and stay in editor (overrides protection).)
:w!
vim Ex mode command keystrokes with description: (Write buffer to file and quit editor (overrides protection).)
:wq!
vim Ex mode command keystrokes with description: (Write buffer to file and quit editor.) (2)
:x ; :wq
As with STDOUT and STDERR, you can redirect STDIN. The basic redirection operator is the __ symbol.
<
Another form of STDIN redirection can be accomplished using a here document , which is sometimes called here text or heredoc. A here document allows you to redirect multiple items into a command. It can also modify a fi le using a script, create a script, keep data in a script, and so on. A here document redirection operator is
<< followed by a keyword. This keyword can be anything, and it signals the beginning of the data as well as the data's end. Listing 4.25 shows an example of using the sort command along with a here document.
redirection operator with description: (Redirect STDIN from specified file into command and redirect STDOUT to specified file.)
<>
vim command mode keystroke with description: (Move cursor to file line number <n> .)
<n>G
In Listing 4.25, the sort command is entered followed by the << redirection operator and a keyword, EOF . The Enter key is pressed, and a secondary prompt, __ , appears indicating that more data can be entered.
>
Which of the following is a BRE pattern that could be used with the grep command? (Choose all that apply.) A. Sp?ce B. "Space, the .*(ast)frontier" C. ^Space D. ( lasting | final) E. frontier$
A, B, C, E. A BRE is a basic regular expression that describes certain patterns you can use with the grep command. An ERE is an extended regular expression and it requires the use of grep -e or the egrep command. Options A, B, C, and E are all BRE patterns that can be used with the grep command, so they are correct choices. The only ERE is in option D, and therefore, it is an incorrect choice.
A file data.txt needs to be sorted numerically and its output saved to a new file newdata .txt . Which of the following commands can accomplish this task? (Choose all that apply.) A. sort -n -o newdata.txt data.txt B. sort -n data.txt > newdata.txt C. sort -n -o data.txt newdata.txt D. sort -o newdata.txt data.txt E. sort data.txt > newdata.txt
A, B. To sort the data.txt fi le numerically and save its output the new fi le, newdata .txt , you can either use the -o switch to save the fi le or employ standard output redirection via the > symbol. In both cases, however, you need to use the -n switch to properly enact a numerical sort. Therefore, both options A and B are correct. Option C is a wrong answer because the command has the newdata.txt and data.txt fl ipped in the command's syntax. Options D and E do not employ the -n switch, so they are incorrect answers as well.
You want to edit the file SpaceOpera.txt and decide to use the vim editor to complete this task. Which of the following are vim modes you might employ? (Choose all that apply.) A. Insert B. Change C. Command D. Ex E. Edit
A, C, D, E. The three modes of the vim editor are command (also called normal mode), insert (also called edit or entry mode), and ex (sometimes called colon commands) mode. Therefore, options A, C, D, and E are correct answers. The only incorrect choice for this question is option B.
A Unicode-encoded text file, MyUCode.txt , needs to be perused. Before you decide what utility to use in order view the file's contents, you employ the wc command on it. This utility displays 2020 6786 11328 to STDOUT. What of the following is true? (Choose all that apply.) A. The file has 2,020 lines in it. B. The file has 2,020 characters in it. C. The file has 6,786 words in it. D. The file has 11,328 characters in it. E. The file has 11,328 lines in it.
AC
Description of sed command option: --file=<<sccript>>
Add commands in <<sccript>> to text processing. The <<sccript>> is a file.
Description of sed command option: -f <<sccript>>
Add commands in <<sccript>> to text processing. The <<sccript>> is a file.
Description of sed command option: --expression=<<scccript>>
Add commands in <<sccript>> to text processing. The <<sccript>> is written as part of the sed command.
Description of sed command option: -e <<sccccript>>
Add commands in <<sccript>> to text processing. The <<sccript>> is written as part of the sed command.
What to write to a sed script file to replace "cake" with "donuts" and "like" with "love" (not globally) and command to invoke this script on "cake.txt":
Add these two lines to file script.sed: "(1) s/cake/donuts/ (2) s/like/love/". Execute this command: "sed -f script.sed cake.txt"
Which of the following best defines a file descriptor? A. A letter that represents the file's type B. A number that represents a process's open files C. Another term for the file's name D. A six-character name that represents standard output E. A symbol that indicates the file's classification
B
You want to find any file named 42.tmp , which exists somewhere in your current directory's tree structure and display its contents to STDOUT. Which of the following will allow you to build a command to do this? (Choose all that apply.) A. xargs (find . -name 42.tmp) cat B. cat `find . -name 42.tmp` C. cat $(find . -name 42.tmp) D. cat {find . -name 42.tmp} E. find . -name 42.tmp | xargs cat
B, C, E. The xargs command, $() method, backticks ( ` ), and brace expansion all allow you to build a command-line command on the fl y. In this case, only options B, C, and E are using the correct command syntax to fi nd any fi le named 42.tmp , which exists somewhere in your current directory's tree structure and display its contents to STDOUT. Therefore, options B, C, and E are correct answers. Option A is using the wrong syntax for the xargs command, and this command will generate an error message. Therefore, option A is a wrong answer. Option D is using the wrong syntax for brace expansion, and thus, it is an incorrect choice as well.
ou are a system administrator on a CentOS Linux server. You need to view records in the /var/log/messages file that start with the date May 30 and end with the IPv4 address 192.168.10.42. Which of the following is the best grep command to use? A. grep "May 30?192.168.10.42" /var/log/messages B. grep "May 30.*(ast)192.168.10.42" /var/log/messages C. grep -i "May 30.*(ast)192.168.10.42" /var/log/messages D. grep -i "May 30?192.168.10.42" /var/log/messages E. grep -v "May 30.*(ast)192.168.10.42" /var/log/messages
B. Option B is the best command because this grep command employs the correct syntax. It uses the quotation marks around the pattern to avoid unexpected results and uses the .*(ast) regular expression characters to indicate that anything can be between May 30 and the IPv4 address. No additional switches are necessary. Option A is not the best grep command because it uses the wrong regular expression of ? , which only allows one character to exist between May 30 and the IPv4 address. Options C and D are not the best grep commands because they employ the use of the -i switch to ignore case, which is not needed in this case. The grep command in option E is an incorrect choice, because it uses the -v switch, which will display text records that do not match the PATTERN .
Which of the following commands can display the data.txt and datatoo.txt files' content one after the other to STDOUT? (Choose all that apply.) A. ls data.txt datatoo.txt B. sort -n data.txt > datatoo.txt C. cat -n data.txt datatoo.txt D. ls -l data.txt datatoo.txt E. sort data.txt datatoo.txt
C, E. The commands in both options C and E will display the data.txt and datatoo.txt f i les' content one after the other to STDOUT. The cat -n command will also append line numbers to it, but it will still concatenate the fi les' content to standard output. Therefore, options C and E are correct. Option A will just display the fi les' names to STDOUT, so it is a wrong answer. Option B will numerically sort the data.txt , wipe out the datatoo.txt f i le's contents, and replace it with the numerically sorted contents from the data.txt fi le. Therefore, option B is an incorrect answer. Option D will show the two fi les' metadata to STDOUT instead of their contents, so it also is a wrong choice.
A text file, StarGateAttacks.txt , needs to be specially formatted for review. Which of the following commands is the best command to accomplish this task quickly? A. printf B. wc C. pr D. paste E. nano
C. The pr command's primary purpose in life is to specially format a text fi le for printing, and it can accomplish the required task fairly quickly. Therefore, option C is the best choice. While the pr utility can handle formatting entire text fi les, the printf command is geared toward formatting the output of a single text line. While you could write a shell script to read and format each text fi le's line via the printf command, it would not be the quickest method to employ. Therefore, option A is a wrong answer. Option B's wc command will perform counts on a text fi le and does not format text fi le contents, so it is also an incorrect answer. The paste command will "sloppily" put together two or more text fi les side by side. Thus, option D is a wrong answer. Option E is an incorrect choice because the nano text editor would force you to manually format the text fi le, which is not the desired action.
Quickly moving around in the vim editor buffer is useful. However, there are also several editing commands that help to speed up your modifi cation process. For example, by moving your cursor to a word's fi rst letter and pressing __, the word is deleted, and you are thrown into insert mode. You can then type in the new word and press Esc to leave insert mode.
CW
vim command mode keystroke with description: (Scroll up almost one full screen.)
Ctrl+B
vim command mode keystroke with description: (Scroll down half of a screen.)
Ctrl+D
vim command mode keystroke with description: (Scroll down one line.)
Ctrl+E
vim command mode keystroke with description: (Scroll down almost one full screen.)
Ctrl+F
In Figure 4.1 you can see the four main sections of the nano text editor. They are as follows: -Shortcut List: The shortcut list is one of the editor's most useful features. By glancing at this list at the window's bottom, you can see the most common commands and their associated shortcut keys. The caret (^) symbol in this list indicates that the Ctrl key must be used. For example, to move down a page, you press and hold the Ctrl key and then press the V key. To see additional commands, press the __ key combination for help.
Ctrl+G
vim command mode keystroke with description: (Scroll up half of a screen.)
Ctrl+U
vim command mode keystroke with description: (Scroll up one line.)
Ctrl+Y
By default, STDOUT goes to what item? A. /dev/tty n , where n is a number. B. /dev/null C. > D. /dev/tty E. pwd
D. D. By default, STDOUT goes to your current terminal, which is represented by the /dev/ tty fi le. Therefore, option D is the correct answer. The /dev/tty n fi le, such as /dev/ tty2 , may be your current terminal at a particular point in time, but /dev/tty always represents your current terminal, so option A is a wrong answer. Option C is incorrect because it is the symbol used at the command line to redirect STDOUT away from its default behavior. The pwd command displays your present working directory, so option E is a wrong choice.
Explain this command: rm -i tmp/EmptyFile{1,3}.txt
Delete EmptyFile1.txt and EmptyFile3.txt
Description of gawk option: --sandbox
Execute gawk program in sandbox mode.
Description of gawk option: -s
Execute gawk program in sandbox mode.
Description of vim Ex mode command keystrokes: :! <command>
Execute shell <command> and display results, but don't quit editor.
Description of vim Ex mode command keystrokes: :r! <command>
Execute shell <command> and include the results in editor buffer area.
vim command mode keystroke with description: (Move cursor to the file's last line.)
G
Description of vim command mode keystroke: b
Move cursor backward one word.
Description of vim command mode keystroke: j
Move cursor down one line (the next line in the text).
Description of vim command mode keystroke: w
Move cursor forward one word to front of next word.
Description of vim command mode keystroke: h
Move cursor left one character.
Description of vim command mode keystroke: l (like lion)
Move cursor right one character.
Description of vim command mode keystroke: ^
Move cursor to beginning of line.
Description of vim command mode keystroke: e
Move cursor to end of current word.
Description of vim command mode keystroke: $
Move cursor to end of line.
Description of vim command mode keystroke: <n>G
Move cursor to file line number <n> .
Description of vim command mode keystroke: gg
Move cursor to the file's first line.
Description of vim command mode keystroke: G
Move cursor to the file's last line.
Description of vim command mode keystroke: k
Move cursor up one line (the previous line in the text).
In Figure 4.1 you can see the four main sections of the nano text editor. They are as follows: -Title Bar: The title bar is at the nano text editor window's top line. It shows the current editor version as well as the name of the fi le you are presently editing. In Figure 4.1, the fi le being edited is the numbers.txt fi le. If you simply typed in nano and did not include a fi le name, you would see __ in the title bar.
New Buffer
Description of vim Ex mode command keystrokes: :q!
Quit editor without writing buffer to file (overrides protection).
Description of vim Ex mode command keystrokes: :q
Quit editor without writing buffer to file.
Description of vim Ex mode command keystrokes: :r <file>
Read < file> contents and include them in editor buffer area.
Description of redirection operator: <>
Redirect STDIN from specified file into command and redirect STDOUT to specified file.
By default, sed will use the text from __ to modify according to the prespecifi ed commands
STDIN
If you really want to enact powerful and quick results at the Linux command line, you need to explore pipes. The pipe is a simple redirection operator represented by the ASCII character 124 ( | ), which is called the vertical bar, vertical slash, or vertical line. With the pipe, you can redirect
STDOUT, STDIN, and STDERR between multiple commands all on one command line.
Description of vim command mode keystroke: Ctrl+F
Scroll down almost one full screen.
Description of vim command mode keystroke: Ctrl+D
Scroll down half of a screen.
Description of vim command mode keystroke: Ctrl+E
Scroll down one line.
Description of vim command mode keystroke: Ctrl+B
Scroll up almost one full screen.
Description of vim command mode keystroke: Ctrl+U
Scroll up half of a screen.
Description of vim command mode keystroke: Ctrl+Y
Scroll up one line.
Description of gawk option: --field-separator <x>
Specify the delimiter that separates the data file's fields.
Description of gawk option: -F <x>
Specify the delimiter that separates the data file's fields.
The basic syntax for redirection with the pipe symbol is as follows: command 1 | command 2 [| command n ]... (explain)
The syntax for pipe redirection shows that the fi rst command, command1 , is executed. Its STDOUT is redirected as STDIN into the second command, command2 . Also, you can pipe more commands together than just two. Keep in mind that any command in the pipeline has its STDOUT redirected as STDIN to the next command in the pipeline.
WARNING: Some distributions, such as Ubuntu, do not have the vim editor installed by default. Instead, they use an alternative, called vim.tiny , which will not allow you to try all the various vim commands discussed here. You can check your distribution to see if vim is installed by obtaining the vim program file name. (describe process)
Type in type vi and press Enter, and if you get an error or an alias, then enter type vim . Once you receive the program's directory and file name, type in the command readlink -f and follow it up with the directory and file name. For example: readlink -f /usr/bin/ vi . If you see /usr/bin/vi.tiny , you need to either switch to a different distribution to practice the vim commands or install the vim package (see Chapter 13).
WARNING: Some distributions, such as __, do not have the vim editor installed by default. Instead, they use an alternative, called vim.tiny , which will not allow you to try all the various vim commands discussed here.
Ubuntu
Description of sed command option: --regexp-extended
Use extended regular expressions in scrcipt.
Description of sed command option: -r
Use extended regular expressions in scrcipt.
Description of gawk option: --file=<file>
Use program in file for text processing.
Description of gawk option: -f <file>
Use program in file for text processing.
Description of vim Ex mode command keystrokes: :wq!
Write buffer to file and quit editor (overrides protection).
Description of vim Ex mode command keystrokes: :wq
Write buffer to file and quit editor.
Description of vim Ex mode command keystrokes: :x
Write buffer to file and quit editor.
Description of vim Ex mode command keystrokes: :w!
Write buffer to file and stay in editor (overrides protection).
Description of vim Ex mode command keystrokes: :w
Write buffer to file and stay in editor.
Once you have made any needed text changes in the vim buffer area, it's time to save your work. You can type __ in command mode to write the buffer to disk and exit your process from the vim editor.
ZZ
The vim editor has a message area near the bottom line. If you have just opened an already created fi le, it will display the fi le name along with the number of lines and characters read into the buffer area. If you are creating a new fi le, you will see __ in the message area.
[New File]
vim command mode keystroke with description: (Move cursor to beginning of line.)
^
vim command mode keystroke with description: (Move cursor backward one word.)
b
Another method for creating commands is __ . This handy approach allows you to cut down on typing at the command line.
brace expansion
The vim editor has three standard modes as follows: -Ex Mode: This mode is sometimes also called
colon commands because every command entered here is preceded with a colon ( : ).
The basic syntax for redirection with the pipe symbol is as follows:
command 1 | command 2 [| command n ]...
The third vim mode, Ex mode, has additional handy commands. You must be in
command mode to enter into Ex mode.
Similar to sed , you can provide the program on the same command line as the gawk command. It also employs the use of single quotation marks to enclose the script. However, unlike sed , the gawk utility requires you to put your programming language commands between two curly braces. An example is shown in Listing 4.38. The print command...
displays text to STDOUT, but notice that different parts of STDIN are shown, as shown in Listing 4.38. This is accomplished via the gawk utility's defi ned data fi eld variables.
vim command mode keystroke with description: (Move cursor to end of current word.)
e
command to send "Hello World" as stdin to gawk and print this string:
echo "Hello World" | gawk '{print $0}'
command to send "Hello World" as stdin to gawk and print the first word of the string:
echo "Hello World" | gawk '{print $1}' (output is the word Hello)
Change word "cake" to "donuts" in line "I like cake." and send to STDOUT: (not globally)
echo "I like cake." | sed 's/cake/donuts/'
Change word "cake" to "donuts" in line "I like cake and more cake." and send to STDOUT: (globally)
echo "I love cake and more cake." | sed 's/cake/donuts/g'
The vim editor has three standard modes as follows: - Insert Mode Insert mode is also called __ or __ mode. This is the mode where you can perform simple editing.
edit; entry
sed: A handy option to use is the -e option. This allows you to
employ multiple scripts in the sed command.
The sed editor changes data based on commands either
entered into the command line or stored in a text fi le.
command to find all empty files in "tmp" directory and delete them but ask permission first
find tmp -size 0 | xargs -p /usr/bin/rm
The gawk programming language is popular for creating
formatted reports from large data sets. You can create gawk programs and store them in fi les for repeated use.
command to replace fourth field of each line in file "cake.txt" with the word "donuts" and then print the entire line:
gawk '{$4="donuts"; print $0}' cake.txt
command to, if fourth field of a line in "cake.txt" is the string "cake.", replace that field with the string "donuts" and print the entire line:
gawk '{if ($4 == "cake.") {$4="donuts"; print $0}}' cake.txt
gawk command to print the first word from each line in cake.txt to STDOUT:
gawk '{print $1}' cake.txt
gawk command to print first field of /etc/passwd
gawk -F : '{print $1}' /etc/passwd
gawk command to use gawk script "script.gawk" on file "cake.txt":
gawk -f script.gawk cake.txt
Before looking as some gawk examples, it is important to understand the command's basic syntax. It is as follows:
gawk [ OPTIONS ] [ PROGRAM ]... [ FILENAME ]
vim command mode keystroke with description: (Move cursor to the file's first line.)
gg
vim command mode keystroke with description: (Move cursor left one character.)
h
Another form of STDIN redirection can be accomplished using a here document , which is sometimes called __ or __.
here text; heredoc
Whether you need to modify a confi guration fi le or create a shell script, being able to use an __ is an important skill.
interactive text fi le editor
The fi rst stream editor we'll explore is called the stream editor . The command to invoke it is sed . The sed utility edits a stream of text data based on a set of commands you supply ahead of time. It is a quick editor because
it makes only one pass through the text to apply the modifi cations.
vim command mode keystroke with description: (Move cursor down one line (the next line in the text).)
j
vim command mode keystroke with description: (Move cursor up one line (the previous line in the text).)
k
vim command mode keystroke with description: (Move cursor right one character.)
l (like lion)
There are times where you will want to edit text fi les without having to pull out a fullf l edged text editor. In these cases, learning about two very popular stream editors is worthwhile. A stream editor...
modifi es text that is passed to it via a fi le or output from a pipeline. The editor uses special commands to make text changes as the text "streams" through the editor utility.
Another form of STDIN redirection can be accomplished using a here document , which is sometimes called here text or heredoc. A here document allows you to redirect multiple items into a command. It can also (3)
modify a fi le using a script, create a script, keep data in a script, and so on.
It's tempting to learn only one text editor and ignore the other. This, of course, won't help you pass the CompTIA Linux+ certifi cation exam. But, in addition, knowing at least two text editors is useful in your day-to-day Linux work. For simple modifi cations, the __ text editor shines. For more complex editing, the __ editor is king. Both are worth your time to master.
nano; vim
In Listing 4.42, a more complex if structure statement is written using the gawk programming language and saved to a fi le, script.gawk . This script not only employs an if statement, it also incorporates an else if structure. Notice also that
no single quotation marks are needed when the gawk program is stored in a fi le.
The vim editor has three standard modes as follows: -Command Mode: This is the mode vim uses when you fi rst enter the buffer area; this is sometimes called __ mode. Here you enter keystrokes to enact commands. For example, pressing the J key will move your cursor down one line. This is the best mode to use for quickly moving around the buffer area.
normal
In Figure 4.1 you can see the four main sections of the nano text editor. They are as follows: -Shortcut List: The shortcut list is one of the editor's most useful features. By glancing at this list at the window's bottom, you can see the most common commands and their associated shortcut keys. The caret (^) symbol in this list indicates that the Ctrl key must be used. For example, to move down a page, you
press and hold the Ctrl key and then press the V key. To see additional commands, press the Ctrl+G key combination for help.
The vim editor has three standard modes as follows: - Insert Mode Insert mode is also called edit or entry mode. This is the mode where you can perform simple editing. There are not many commands or special mode keystrokes. You enter this mode from command mode, by pressing the I key. At this point, the message --Insert-- will display in the message area. You leave this mode by
pressing the Esc key.
The gawk programming language is popular for creating formatted reports from large data sets. You can create gawk
programs and store them in fi les for repeated use.
The gawk utility is also a stream editor, but it
provides a more powerful editing process via its programming language.
Before looking as some gawk examples, it is important to understand the command's basic syntax. It is as follows: gawk [ OPTIONS ] [ PROGRAM ]... [ FILENAME ] Similar to sed , you can provide the program on the same command line as the gawk command. It also employs the use of single quotation marks to enclose the script. However, unlike sed , the gawk utility requires you to
put your programming language commands between two curly braces.
use brace expansion to delete files EmptyFile1.txt and EmptyFile3.txt from the tmp folder using interactive mode:
rm -i tmp/EmptyFile{1,3).txt
The fi rst stream editor we'll explore is called the stream editor . The command to invoke it is
sed
Command to replace word "cake" with "donuts" in file cake.txt (not globally)
sed 's/cake/donuts/' cake.txt
command to replace word "cake" with "donuts" and word "like" with "love" in cake.txt and send results to STDOUT (not globally):
sed -e 's/cake/donuts ; s/like/love' cake.txt
Before looking at some sed examples, it is important to understand the command's basic syntax. It is as follows:
sed [ OPTIONS ] [ SCRIPT ]... [ FILENAME ]
Before looking as some gawk examples, it is important to understand the command's basic syntax. It is as follows: gawk [ OPTIONS ] [ PROGRAM ]... [ FILENAME ] Similar to sed , you can provide the program on the same command line as the gawk command. It also employs the use of __ to enclose the script.
single quotation marks
If you have a lot of sed script commands, you can
store them in a fi le. This is convenient because you can use the script fi le over and over again.
The fi rst stream editor we'll explore is called the __ . The command to invoke it is sed .
stream editor
There are times where you will want to edit text fi les without having to pull out a fullf l edged text editor. In these cases, learning about two very popular __ is worthwhile.
stream editors
In cases where you want to keep a copy of the command pipeline's output as well as view it, the (general command)__ command will help.
tee
In Figure 4.1 you can see the four main sections of the nano text editor. They are as follows: -Status Bar: The status bar does not always contain information. It only displays status information for certain events. For example, in Figure 4.1 , the text fi le has just been opened in nano , so the status bar area displays [ Read 5 lines ] to indicate
that fi ve text lines were read into the editor.
In Figure 4.1 you can see the four main sections of the nano text editor. They are as follows: -Shortcut List: The shortcut list is one of the editor's most useful features. By glancing at this list at the window's bottom, you can see the most common commands and their associated shortcut keys. The caret (^) symbol in this list indicates
that the Ctrl key must be used. For example, to move down a page, you press and hold the Ctrl key and then press the V key. To see additional commands, press the Ctrl+G key combination for help.
TIP: Within the nano text editor's help subsystem, you'll see some key combinations denoted by M- <k> . An example is M-W for repeating a search. These are metacharacter key combinations, and the M represents
the Esc, Alt, or Meta key, depending on your keyboard's setup. The <k> simply represents a keyboard key, such as W.
Change word "cake" to "donuts" in line "I like cake." and send to STDOUT: " echo "I like cake." | sed 's/cake/donuts/'" Notice in Listing 4.32 that the text output from the echo command is piped as input into the stream editor. The sed utility's s command (substitute) specifi es that if the fi rst text string, cake , is found, it is changed to donuts in the output. Note that the entire command after sed is considered to be
the SCRIPT , and it is encased in single quotation marks. Also notice that the text words are delimited from the s command, the quotation marks, and each other via the forward slashes ( / ).
Another form of STDIN redirection can be accomplished using a here document , which is sometimes called here text or heredoc. A here document allows you to redirect multiple items into a command. It can also modify a fi le using a script, create a script, keep data in a script, and so on. A here document redirection operator is << followed by a keyword. This keyword can be anything, and it signals
the beginning of the data as well as the data's end.
In Figure 4.1 you can see the four main sections of the nano text editor. They are as follows: -Title Bar: The title bar is at the nano text editor window's top line. It shows (2)
the current editor version as well as the name of the fi le you are presently editing. In Figure 4.1, the fi le being edited is the numbers.txt fi le. If you simply typed in nano and did not include a fi le name, you would see New Buffer in the title bar.
The vim editor has a message area near the bottom line. If you have just opened an already created fi le, it will display
the fi le name along with the number of lines and characters read into the buffer area. If you are creating a new fi le, you will see [New File] in the message area.
The nano text editor is wonderful to use for simple text fi le modifi cations. However, if you need a more powerful text editor for creating programs or shell scripts,
the vim editor is a popular choice.
Quickly moving around in the vim editor buffer is useful. However, there are also several editing commands that help to speed up your modifi cation process. For example, by moving your cursor to a word's fi rst letter and pressing CW,
the word is deleted, and you are thrown into insert mode. You can then type in the new word and press Esc to leave insert mode.
TIP: Backticks are not very popular anymore. While they perform the same duty as do the $() symbols for creating commands,
they are harder to see and are often confused with single quotation marks.
As with STDOUT and STDERR, you can redirect STDIN. The basic redirection operator is the < symbol. The (general command)__ command is one of the few utilities that require you to redirect standard input.
tr
command to replace " " with "," in file "Grades.txt" and send it to output:
tr " " "," < Grades.txt
To start using the nano text editor,
type nano followed by the fi le's name you wish to edit or create.
If you open vim without a fi le name or the fi le name you entered doesn't yet exist,
vim starts a new buffer area for editing.
WARNING: Some distributions, such as Ubuntu, do not have the vim editor installed by default. Instead, they use an alternative, called __ , which will not allow you to try all the various vim commands discussed here.
vim.tiny
TIP: Some distributions have a vim tutorial installed by default. This is a handy way to learn to use the vim editor. To get started, just type __ at the command line.
vimtutor
vim command mode keystroke with description: (Move cursor forward one word to front of next word.)
w
Change word "cake" to "donuts" in line "I like cake." and send to STDOUT: " echo "I like cake." | sed 's/cake/donuts/'" Notice in Listing 4.32 that the text output from the echo command is piped as input into the stream editor. The sed utility's s command (substitute) specifi es that if the fi rst text string, cake , is found, it is changed to donuts in the output. Note that the entire command after sed is considered to be the SCRIPT , and it is encased in single quotation marks. Also notice that the text words are delimited from the s command, the quotation marks, and each other via the forward slashes ( / ). Keep in mind that just using the s command
will not change all instances of a word within a text stream.
Creating command-line commands is a useful skill. There are several different methods you can use. One such method is using the __ utility.
xargs