Chapter 10 Advanced File Processing
Regular Expressions A compact notation for representing patterns in strings Used by many common Linux utilities such as: – less/more – ed – vi – emacs – egrep – grep – fgrep – awk – sed – lex – perl – python
Table 10.1 Regular Expression Operators
Table 10.1 More Regular Expression Operators
Regular Expression Examples cat|dog matches "cat" or "dog“..at matches any three-character string ending with "at", including "hat", "cat", and "bat". [hc]at matches "hat" and "cat". [^b]atmatches all strings matched by.at except "bat". ^[hc]at matches "hat" and "cat", but only at the beginning of the string or line. [hc]at$matches "hat" and "cat", but only at the end of the string or line.
Regular Expression Examples (0|1)+ 1+0* a*ba*ba* [A-Za-z][A-Za-z0-9]* (yaba|daba)+doo
Table 10.2 Examples of Regular Expressions for vi and Their Meaning
Table 10.3 Some Commonly Used vi Commands Illustrating the Use of Regular Expressions
File Compression compress uncompress gzip gunzip gzexe zcat zmore uuencode uudecode
Figure 10.1 The process of uuencoding and uudecoding
The Sort Utility Sorts lines of ASCII text Options for: – Ascending/descending order – Case folding – Ignoring leading whitespace – Alpha/numeric – Multiple keys
sort - sort lines of text files Syntax sort [OPTION]... [FILE]... Options -b, --ignore-leading-blanks ignore leading blanks -d, --dictionary-order consider only blanks and alphanumeric characters -f, --ignore-case fold lower case to upper case characters -i, --ignore-nonprinting consider only printable characters -n, --numeric-sort compare according to string numerical value -r, --reverse reverse the result of comparisons Other options: -k, --key=POS1[,POS2] start a key at POS1, end it at POS2 (origin 1) -t, --field-separator=SEP use SEP instead of non-blank to blank transition
Sort examples File donors.dat Bay Ching China Jack Arta Indonesia Cruella Lumper Malaysia sort donors.dat Bay Ching China Cruella Lumper Malaysia Jack Arta Indonesia
Sort Example – by last name sort +1 donors.dat Jack Arta Indonesia Bay Ching China Cruella Lumper Malaysia
Sort Example – by donation amount sort +2 donors.dat Jack Arta Indonesia Bay Ching China Cruella Lumper Malaysia sort +2 -n donors.dat Jack Arta Indonesia Bay Ching China Cruella Lumper Malaysia sort +2 -n -r donors.dat Cruella Lumper Malaysia Bay Ching China Jack Arta Indonesia
Locating Files find whereis which
Find command Syntax Find directory-list expression Searches directory-list for files that match conditions in expression Can find files by name, owner, permission, size, age, links
Find Command Examples find ~ -name *.gif -print find / -name socket.h -print find / -size c -print find ~ -newer filetocompare
Searching File Contents grep egrep fgrep
Grep – Get Regular Expression & Print Syntax grep [options] pattern [file-list] Searches files in file-list, prints lines which match the given pattern Options to fold case, print line #’s, filenames, non- matching lines fgrep – faster but dumb egrep – slower but more powerful
Command History A list of the recently used commands is maintained and controlled by the variables: – HISTSIZE – HISTFILE – HISTFILESIZE
Table 10.4 Commonly Used Event Designators for History Expansion
Table 10.5 Commonly Used Event Designators for History Expansion
Table 10.6 Commonly Used Modifiers for History Expansion