Linux

  1. Home
  2. Computing & Technology
  3. Linux

From Authors, for About.com

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, "^", means "the beginning of the line". So "^a" 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 '^s.*n$'

Any character terminating the pattern string is special, precede it with a backslash if you want to use it within this string.

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 "^", 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 "o") 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

Explore Linux

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Linux

  1. Home
  2. Computing & Technology
  3. Linux

©2009 About.com, a part of The New York Times Company.

All rights reserved.