Books
in black and white
Main menu
Home About us Share a book
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

Linux for dummies - Klimas M.

Klimas M. Linux for dummies - Wiley publishing , 2002. - 169 p.
Download (direct link): linuxfordummies2002.pdf
Previous << 1 .. 100 101 102 103 .. 104105 < 106 > 107 108 109 110 111 112

Thus, "\\" searches for a backslash, "\." searches for a dot, "\*" searches for the asterisk, "\[" searches for the bracket, "\a" searches for the caret even at the begining of the string, "\$" searches for the dollar sign even at the end of the string.
Backslash followed by a regular (non-special) character may gain a special meaning. Thus, the symbols \< and \> match an empty string at the beginning and the end of a word, respectively. The symbol \b matches the empty string at the edge of a word, and \B matches the empty string provided it's not at the edge of a word.
The dot, ".", matches any single character. [The dir command uses "?" in this place.] Thus, "m.a" matches "mpa" and "mea" but not "ma" or "mppa".
Any string is matched by ".*" (dot and asterisk). [The dir command uses "*" instead.] In general, any pattern followed by "*" matches zero or more occurences of this pattern. Thus, "m*" matches zero or more occurances of "m". To search for one or more "m", I could use "mm*".
The * is a repetition operator. Other repetition operators are used less often--here is the full list: * the proceding item is to be matched zero or more times;
\+ the preceding item is to be matched one or more times;
\? the preceding item is optional and matched at most once;
\{n} the preceding item is to be matched exactly n times;
\{n,} the preceding item is to be matched n or more times;
\{n,m} the preceding item is to be matched at least n times, but not more than m times.
The caret, "a", means "the beginning of the line". So "Aa" means "find a line starting with an "a".
The dollar sign, "$", means "the end of the line". So "a$" means "find a line ending with an "a".
Example. This command searches the file myfile for lines starting with an "s" and ending with an "n", and prints them to the standard output (screen):
cat myfile | grep 'As.*n$'
Any character terminating the pattern string is special, precede it with a backslash if you want to use it within this string.
Part 7: Learning with Linux
143
Linux Newbie Guide by Stan, Peter and Marie Klimas 01/08/2003
The bracket, "[" introduces a set. Thus [abD] means: either a or b or D. [a-zA-C] means any character from a to z or from A to C.
Attention with some characters inside sets. Within a set, the only special characters are "[", "]",
"-", and "A", and the combinations "[:", "[=", and "[.". The backslash is not special within a set.
Useful categories of characters are (as definded by the POSIX standard): [:upper:]
=upper-case letters, [:lower:] =lower-case letters, [:alpha:] =alphabetic (letters) meaning upper+lower, [:digit:] =0 to 9, [:alnum:] =alphanumeric meaning alpha+digits, [:space:]
=whitespace meaning <Space>+<Tab>+<Newline> and similar, [:graph:] =graphically printable characters except space, [:print:] =printable characters including space, [:punct:]
=punctuation characters meaning graphical characters minus alpha and digits, [:cntrl:] =control characters meaning non-printable characters, [:xdigit:] = characters that are hexadecimal digits.
Example. This command scans the output of the dir command, and prints lines containing a capital letter followed by a digit:
dir -l | grep '[[:upper:]][[:digit:]]'
tr
(=translation). A filter useful to replace all instances of characters in a text file or "squeeze" the white space.
Example :
cat my_file | tr 1 2 > new_file
This command takes the content of the file my_file, pipes it to the translation utility tr, the tr utility replaces all instances of the character "1" with "2", the output from the process is directed to the file new_file.
sed
(=stream editor) I use sed to filter text files. The pattern to match is typically included between a pair of slashes // and quoted.
For example, to print lines containing the string "1024", I may use: cat filename | sed -n '/1024/p'
Here, sed filters the output from the cat command. The option "-n" tells sed to block all the incoming lines but those explicitly matching
my expression. The sed action on a match is "p"= print.
Another example, this time for deleting selected lines:
cat filename | sed '/.*o$/d' > new_file
In this example, lines ending the an "o" will be deleted. I used a regular expression for matching any string followed by an "o" and the end of the line. The output (i.e., all lines but those ending with "d") is directed to new_file.
Another example. To search and replace, I use the sed 's' action, which comes in front of two expressions:
cat filename | sed 's/string_old/string_new/' > newfile
A shorter form for the last command is:
sed 's/string_old/string_new/' filename > newfile To insert a text from a text file into an html file, I may use a script containing:
sed '/text_which_is_a_placeholder_in my_html_file/r text_file_to_insert.txt'
index_master_file.html > index.htmll
gawk
(=GNU awk. The awk command is a traditional UNIX tool.) A tool for processing text files, in many respects similar to sed, but more powerful. Perl can do all that gawk can, and more, so I don't bother with gawk too much. For simple tasks, I use sed, for more complicated tasks, I use perl. In some instances, however, awk scripts can be much shorter, easier to understand and maintain, and faster than an equivalent perl program.
Previous << 1 .. 100 101 102 103 .. 104105 < 106 > 107 108 109 110 111 112