9/14/2008

More UNIX usage tips!

!$ operator returns the file_system_access.c argument, and the !:1 operator returns the kxp12.c argument, which is the first argument of the previous command.

$ mv kxp12.c file_system_access.c
$ ln –s !$ !:1

df command shows you the total number of blocks used on each available volume and the percentage of free space.

Use the find command with the -size parameter. Listing 7 shows how to use the find command to find files larger than 10MB. Note that the -size parameter takes a size in kilobytes.
$ find / -size +10000k –xdev –exec ls –lh {}\;

Make the most of regular expressions

Many UNIX commands use regular expressions as arguments. Technically speaking, a regular expression is a string (that is, a sequence of characters composed of letters, numbers, and symbols) that represents a pattern defining zero or more strings. A regular expression uses meta-characters (for example, the asterisk [*] and question mark [?] symbols) to match parts of or whole other strings. A regular expression doesn't have to contain wildcards, but wildcards can make regular expressions useful for searching for patterns and manipulating files. Table 1 shows some basic regular expression sequences.


Table 1. Regular expression sequences
SequenceDescription
Caret (^)Matches the expression at the start of a line, as in ^A
Question mark (?)Matches the expression at the end of a line, as in A?
Backslash (\)Turns off the special meaning of the next character, as in \^
Brackets ([])Matches any one of the enclosed characters, as in [aeiou] (Use a hyphen [-] for a range, as in [0-9].)
[^ ] Matches any one character except those enclosed in brackets, as in [^0-9]
Period (.)Matches a single character of any value except end of line
Asterisk (*)Matches zero or more of the preceding characters or expressions
\{x,y\} Matches x to y occurrences of the preceding
\{x\} Matches exactly x occurrences of the preceding
\{x,\} Matches x or more occurrences of the preceding

Listing 12 shows some of the basic regular expressions used with the grep command.


Listing 12. Using regular expressions with grep
               
$ # Lists your mail
$ grep '^From: ' /usr/mail/$USER
$ # Any line with at least one letter
$ grep '[a-zA-Z]' search-file.txt
$ # Anything not a letter or number
$ grep '[^a-zA-Z0-9] search-file.txt
$ # Find phone numbers in the form 999-9999
$ grep '[0-9]\{3\}-[0-9]\{4\}' search-file.txt
$ # Find lines with exactly one character
$ grep '^.$' search-file.txt
$ # Find any line that starts with a period "."
$ grep '^\.' search-file.txt
$ # Find lines that start with a "." and 2 lowercase letters
$ grep '^\.[a-z][a-z]' search-file.txt

Process data with awk

The awk command always seems to live in the shadows of Perl, but it can be a quick, useful tool for simple command-line-based data manipulation. Listing 15 shows how to get started with the awk command. To get the length of each line in the file text, use the length() function. To see if the string ing is present in the file text, use the index() function, which returns the location of the first occurrence of ing so that you can use it for further string processing. To tokenize (that is, split a line into word-length pieces) a string, use the split() function.


Listing 15. Basic awk processing
               
$ cat text
testing the awk command
$ awk '{ i = length($0); print i }' text
23
$ awk '{ i = index($0,”ing”); print i}' text
5
$ awk 'BEGIN { i = 1 } { n = split($0,a," "); while (i <= n) {print a[i]; i++;} }' text
testing
the
awk
command

Printing specified fields of text file is a simple awk task. In Listing 16, the sales file consists of each salesperson's name followed by a monthly sales figure. You can use the awk command to quickly total the sales for each month. By default, awk treats each comma-separated value as a different field. You use the $n operators to access each individual field.


Listing 16. Using awk for data summarization
               
$cat sales
Gene,12,23,7
Dawn,10,25,15
Renee,15,13,18
David,8,21,17
$ awk -F, '{print $1,$2+$3+$4}' sales
Gene 42
Dawn 50
Renee 46
David 46

No comments: