chapter 10 advanced file processing. regular expressions a compact notation for representing...
TRANSCRIPT
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
Regular Expression Examplescat|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]at matches 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.
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.datBay Ching 500000 ChinaJack Arta 250000 IndonesiaCruella Lumper 725000 Malaysia
sort donors.datBay Ching 500000 ChinaCruella Lumper 725000 MalaysiaJack Arta 250000 Indonesia
Sort Example – by last name
sort +1 donors.dat Jack Arta 250000 Indonesia Bay Ching 500000 China Cruella Lumper 725000 Malaysia
Sort Example – by donation amount
sort +2 donors.dat Jack Arta 250000 Indonesia Bay Ching 500000 China Cruella Lumper 725000 Malaysia
sort +2 -n donors.dat Jack Arta 250000 Indonesia Bay Ching 500000 China Cruella Lumper 725000 Malaysia
sort +2 -n -r donors.dat Cruella Lumper 725000 Malaysia Bay Ching 500000 China Jack Arta 250000 Indonesia
Find command
SyntaxFind 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 -printfind / -name socket.h -printfind / -size+5000000c -printfind ~ -newer filetocompare
Grep – Get Regular Expression & Print
Syntaxgrep [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