Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Objectives
Use regular expressions in a variety of circumstances Manipulate text files in complex ways using multiple command-line utilities Use advanced features of the vi editor Use the sed and awk text processing utilities
Regular Expressions
Flexible way to encode many types of complex patterns Use to define pattern in many situations
Parameter to most Linux commands Within vi editor Within programming languages
Including shell scripts
Examples:
[Rr]eunion[0-9][0-9].jpg [Rr]eunion[0-9]{2}.jpg Reunion-[^d].jpg
Manipulating Files
Command-line utilities useful for:
Searching Sorting Reorganizing Otherwise working with text files
10
wc command
Count number of characters, words, and lines
11
12
13
14
sort command
Sort all of lines in text file
uniq command
Remove duplicate lines in file
The Complete Guide to Linux System Administration 15
cmp command
Gives quick check of whether two files are identical
16
17
18
19
20
awk command
Generally used for formatting output
21
22
23
24
Format output based on fields within line of text Often can perform same functions with sed or awk
25
First field is referred to by $1 second by $2, etc. Basic format: /pattern/ { actions } Example: ls -l | awk '{ print $3 $9 }'
26
27
28
File Operations in vi
:w command
Write file you are editing
:r file name
Insert another file into file you are editing
:q command
Exit from vi
:wq
Save and exit
The Complete Guide to Linux System Administration 29
Screen Repositioning
Line number and cursor position on line
Shown at bottom right
30
Mark
Like bookmark m command followed by name (a-z and 0-9)
Place mark
Shift+J
Joins two lines
32
Ctrl+]
Navigate to hyperlinks in help files
Ctrl+t
Navigate back from links in help files
33
n key
Move to next occurrence of search pattern
?
Search backwards
N key
Move to previous occurrence of pattern
The Complete Guide to Linux System Administration 34
Example
:1$ s/^configure/configure/
35
36
Setting vi Options
:set all
View all options currently set in vi Press spacebar multiple times to see all screens of settings
37
38
39
Summary
Regular expressions used in many places to define patterns of information grep command used to search for lines of text containing pattern defined using regular expression sed and awk commands support complex scripting language that includes regular expressions
40
Summary (continued)
vi
Uses complex combinations of commands to reposition cursor within text Supports search-and-replace operations set command defines editor settings
41