Tuesday, October 6, 2009

UNIX Commands

Filter
Filters are commands that are used to filter input or output. These commands by default takes input from keyboard and generates output to standard output where the default input and output can be redirected.
Among the most useful filter tools in UNIX System for finding words in files, include grep, fgrep, and egrep.
grep: It lets us search for a target which may be one or more words or patterns containing wildcards and other regular expression elements.
fgrep: Does not allow regular expressions but does allow us to search for multiple targets.
egrep: Takes a richer set of regular expressions, as well as allowing multiple target searches.
grep
The grep command searches through one or more files for lines containing a target and then prints all of the matching lines it finds.
$ grep room mtg_note
The meeting will be at 9:00 in room 1J303.
$
The following command displays all lines containing the word room.
Note: Multiline search should be enclosed within brackets otherwise grep will treat all others words as filename except the first one.
$ grep chicken *
recipes.ch: chicken with black bean sauce
Recipes.mex: chilaquiles with chicken
$
In the following command, the * replaces the current directory listing and grep looks for the word chicken in all the files. It prints the names of the files where it finds the said pattern.
- c Displays count of number of occurrences - l Displays list of filenames only - n Displays line numbers along with lines - v Displays all but lines matching expression - i Ignores case for matching - h Omits filename when handling multiple files - e exp Specifies expression with this option - N n Displays n lines containing pattern (SCO UnixWare only)

No comments:

Post a Comment