Sie sind auf Seite 1von 18

frep & grep commands

in UNIX

Anisha (31)

Jaee (32)

Amber (33)

Nihar (34)

Shanawaz (35)
Introduction
• Basically when we create a file in
UNIX or in any OS, we always need a
way to search whatever we want in
that file.
• In UNIX basically grep, fgrep, sed, sh
are different types of commands
which are used for searching a
pattern in an input file.
• Depending upon user needs this
command gives output/search result
from given file.
grep command
• Used for searching & printing the
specified pattern from input.
• It is the abbreviation for “Global
search for regular expression &
print”.
• Where “Global” means entire file or
standard input.
• “regular expression” means
metachar pattern.
• “print” means to print ‘em all .
• Syntax for grep: grep [options] pattern
file/s

• Here ‘pattern’ plays an important role.


• Consider a file named as demo.txt
• After Concatenate we have:
• $ cat demo.txt
Anisha | 19 | F | 31
Jaee | 18 | F | 32
Amber | 18 | M | 33
Nihar | 18 | M | 34
Shanawaz | 18 | M | 35
• $ cat demo.txt
Anisha | 19 | F | 31
Jaee | 18 | F | 32
Amber | 18 | M | 33
Nihar | 18 | M | 34
Shanawaz | 18 | M | 35

• Input : $ grep Nihar demo.txt


• Output : Nihar | 18 | M | 34

• List of controls:
l -i : Ignore case distinctions in both the
PATTERN and the input files.
Input : $ grep –i 19 demo.txt
output : Anisha | 19 | F | 31
 $ cat demo.txt
Anisha | 19 | F | 31
Jaee | 18 | F | 32
Amber | 18 | M | 33
Nihar | 18 | M | 34
Shanawaz | 18 | M | 35

2. -n: Prefix each line of output with the line


number within Its input file.
Input : $ grep -n F demo.txt
output : 1:Anisha | 19 | F | 31
2:Jaee | 18 | F | 32
• $ cat demo.txt
Anisha | 19 | F | 31
Jaee | 18 | F | 32
Amber | 18 | M | 33
Nihar | 18 | M | 34
Shanawaz | 18 | M | 35
3.-v: Invert the sense of matching, to select non-
matching lines
Input : $ grep -v Amber demo.txt
output : Anisha | 19 | F | 31
Jaee | 18 | F | 32
Nihar | 18 | M | 34
Shanawaz | 18 | M | 35
• $ cat demo.txt
Anisha | 19 | F | 31
Jaee | 18 | F | 32
Amber | 18 | M | 33
Nihar | 18 | M | 34
Shanawaz | 18 | M | 35

4. -c: Suppress normal output; instead print a


count of matching lines for each input
file.
Input : $ grep -c M demo.txt
Output : 3

5. -o: only-matching Show only the part of a


matching line that matches PATTERN.
Input : $ grep –o Amber demo.txt
Output : Amber
• $ cat demo.txt
Anisha | 19 | F | 31
Jaee | 18 | F | 32
Amber | 18 | M | 33
Nihar | 18 | M | 34
Shanawaz | 18 | M | 35

• Using Regular Expressions:


2. Display results which starts in range which are
given in pattern.
Input : $ grep -i ‘^[a-j]’ demo.txt
Output : Anisha | 19 | F | 31
Jaee | 18 | F | 32
Amber | 18 | M | 33
• $ cat demo.txt
Anisha | 19 | F | 31
Jaee | 18 | F | 32
Amber | 18 | M | 33
Nihar | 18 | M | 34
Shanawaz | 18 | M | 35

2. Display the results which starts with the given


pattern.
Input : $ grep -i “[n,s]” demo.txt
Output : Nihar | 18 | M | 34
Shanawaz | 18 | M | 35
3. Display result which starts & ends with the
given pattern
Input : $ grep ‘A*r’ demo.txt
Output : Amber | 18 | M | 33
• $ cat demo.txt
Anisha | 19 | F | 31
Jaee | 18 | F | 32
Amber | 18 | M | 33
Nihar | 18 | M | 34
Shanawaz | 18 | M | 35

4. Display result which ends with the given pattern


Input : $ grep ‘35$’ demo.txt
Output : Shanawaz | 18 | M | 35
grep command with PIPES:
3. Search for the directories in given location.
Input : $ ls –l | grep ‘^d’
Output :drwxrw-rw- 2 user group 1234 Mar 19 13:01
Desktop
drwxrw-rw- 4 user group 1234 Mar 19 13:01 Music
drwxrw-rw- 2 user group 1234 Mar 19 13:01 Docs

2. Search for an extension of file. That is (.). But in


grep treats dot as special character. Hence we
have to put ‘ \ ’ before dot to take command
literally.
Input : $ ls –l | grep ‘\.txt’
Output: -rw-rw-rw- 2 user group 23 Mar 19 13:01
demo.txt
-rwxr—r-- 2 user group 12 Mar 19 13:01 demo1.txt
Fgrep Command
 Fgrep command stands for ‘fixed
grep’.
 Means, you can only search for fixed
strings without use of any regular
expression.
 Like grep also you can simply add
pattern while search.
 Syntax for fgrep: fgrep [option] pattern
file

 List of Options in fgrep:


4. -l: Print the names of files with matching lines
once, separated by new-lines. Does not repeat
the names of files when the pattern is found
more than once
Input: $ grep Amber demo.txt
output: demo.txt
• $ cat demo.txt
Anisha | 19 | F | 31
Jaee | 18 | F | 32
Amber | 18 | M | 33
Nihar | 18 | M | 34
Shanawaz | 18 | M | 35

l -x: Print only lines matched entirely.


input: grep –x Jaee | 18 | F | 32 demo.txt
output: Jaee | 18 | F | 32
Summary

 grep & fgrep are two most important


& vast commands in Unix, when it
comes about searching elements,
files or directories present in your
computer hard disk.
 Both commands are similar, they
differ by fraction of margin.
Bibliography
 Working with ‘Unix’- vijay mukhi
 Shell scripts- y. kanetkar
 www.computerhope.com
 www.wikipedia.com
 www.unix.com
 www.google.com
Thank You!!

Das könnte Ihnen auch gefallen