Connecting simple processes in a pipeline allows you to perform complex tasks without writing complex programs.
EXAMPLE: Using the more command, and a pipe, send the contents of your .profile and .shrc files to the screen by typing
- cat .profile .shrc | more
EXERCISE: How could you use head and tail in a pipeline to display lines 25 through 75 of a file?
ANSWER: The command
- cat file | head -75 | tail -50
It is easy for beginners to confuse the usage of the input/output redirection symbols < and >, with the usage of the pipe. Remember that input/output redirection connects processes with files, while the pipe connects processes with other processes.
Grep
The grep utility is one of the most useful filters in UNIX. Grep searches line-by-line for a specified pattern, and outputs any line that matches the pattern. The basic syntax for the grep command is grep [-options] pattern [file]. If the file argument is omitted, grep will read from standard input. It is always best to enclose the pattern within single quotes, to prevent the shell from misinterpreting the command.
The grep utility recognizes a variety of patterns, and the pattern specification syntax was taken from the vi editor. Here are some of the characters you can use to build grep expressions:
- The carat (^) matches the beginning of a line.
- The dollar sign ($) matches the end of a line.
- The period (.) matches any single character.
- The asterisk (*) matches zero or more occurrences of the previous character.
- The expression [a-b] matches any characters that are lexically between a and b.
EXAMPLE: Type the command
- grep 'jon' /etc/passwd
EXAMPLE: Type the command
- grep '^jon' /etc/passwd
EXERCISE:List all the files in the /tmp directory owned by the user root.
EXPLANATION: The command
- ls -l /tmp | grep 'root'
浙公网安备 33010602011771号