What Is Unix ?: Grep Command
What Is Unix ?: Grep Command
The UNIX operating system is a set of programs that act as a link between the computer and the
user.The computer programs that allocate the system resources and coordinate all the details of
the computer's internals is called the operating system or kernel.
Users communicate with the kernel through a program known as the shell. The shell is a
command line interpreter; it translates commands entered by the user and converts them into a
language that is understood by the kernel.
Unix was originally developed in 1969 by a group of AT&T employees at Bell Labs,
including Ken Thompson, Dennis Ritchie, Douglas McIlroy, and Joe Ossanna.
There are various Unix variants available in the market. Solaris Unix, AIX, UP Unix and
BSD are few examples. Linux is also a flavour of Unix which is freely available.
Several people can use a UNIX computer at the same time; hence UNIX is called a
multiuser system.
A user can also run multiple programs at the same time; hence UNIX is called
multitasking.
Unix Architecture:
The main concept that unites all versions of UNIX is the following four basics:
Kernel: The kernel is the heart of the operating system. It interacts with hardware and
most of the tasks like memory management, tash scheduling and file management.
Shell: The shell is the utility that processes your requests. When you type in a command
at your terminal, the shell interprets the command and calls the program that you want.
The shell uses standard syntax for all commands. C Shell, Bourne Shell and Korn Shell
are most famous shells which are available with most of the Unix variants.
Commands and Utilities: There are various command and utilities which you would use
in your day to day activities. cp, mv, cat and grep etc. are few examples of commands
and utilities. There are over 250 standard commands plus numerous others provided
through 3rd party software. All the commands come along with various optional options.
Files and Directories: All data in UNIX is organized into files. All files are organized
into directories. These directories are organized into a tree-like structure called the
filesystem.
Grep command
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:
l The caret (^) matches the beginning of a line.
l The dollar sign ($) matches the end of a line.
l The period (.) matches any single character.
l The asterisk (*) matches zero or more occurrences of the previous character.
l The expression [a-b] matches any characters that are lexically between a and b.
Note that some of the pattern matching characters are also shell meta characters. If
you use one of those characters in a grep command, make sure to enclose the
pattern in single quote marks, to prevent the shell from trying to interpret them.
Killing processes
Occasionally, you will find a need to terminate a process. The Unix shell provides
a utility called kill to terminate processes. You may only terminate processes that
you own (i.e., processes that you started).
To kill a process, you must first find its process ID number using the ps command.
Some processes
refuse to die easily, and you can use the "-9" option to force termination of the job.
When the child process finishes, the shell regains access to the terminal, redisplays
the shell prompt, and waits for your next command.
Any task that requires you to actively participate (like word processing) must be in
the foreground to run. Such jobs, termed "interactive," must periodically update the
display, and accept input from you, and so require access to the terminal interface.
Other jobs do not require you to participate once they are started. For example, a
job that sorts the contents of one file and places the results in another file, would
not have to accept user commands, or write information to the screen while it runs.
Some Unix shells allow such non interactive jobs to be disconnected from the
terminal, freeing the terminal for interactive use.
Note that it is even possible to log out, and leave a background process running.
Unfortunately, there is no way to reconnect a background job to the terminal after
you've logged out.
Jobs that are disconnected from the terminal for input and output are called
"background" jobs. You can have a large number of background jobs running at
the same time, but you can only have one foreground job. That is because you only
have one screen, and one keyboard at your terminal.
What Is ed?
ed is a line editor. There are two major types of text editors in the Unix world: line
editors and full-screen editors. (We discuss full-screen editors in Chapter 17: “The
vi Editor.”) When you edit a file with a line editor like ed, you must do so one
line at a time. In contrast, when you use a full-screen editor, you can work on
multiple lines at once. Although line editors are extremely cumbersome, they are
better than nothing. There is enough functionality in a line editor to get your
work done; it’s simply not as easy as it would be with more advanced tools.
ed operates in two modes: input mode and edit mode. (ed also has a command
mode, which is used to manipulate the program until you are ready to work
with text.) As their names imply, input mode is used to insert text into a file, and
edit mode is used to alter the entered text. In later chapters of this part of the
book, you’ll see other editors that use the input and edit mode concepts, even
though they are not line editors.