Unix Filter Commands


1) grep

2) sort

3) more

4) cut

5) wc

6) uniq

1) grep: (Global Regular Expression Patern)

This command is used for searching a required patern in a file.

Syntax:

$ grep [- optopn] "search patern" Filename [redirection symbol newfilename]

Options:

-i   Ignores case sensitiveness in searching patern

-n   displays line numbers for those lines which gets matched and un matched with the patern

-c   counts number of times a searching patern exists and does not exists

-v (verbose)   Displays those lines that does not match with the patern

Example:

$ cat > paypal.txt
welcome to unix
paypal welcomes you
unix multi user os
WELCOME to the world of unix


$ grep "welcome" paypal.txt

$ grep -i "welcome" paypal.txt

$ grep -i -n "welcome" paypal.txt

$ grep -i -c "welcome" paypal.txt

$ grep -i -v "welcome" paypal.txt

$ grep -i -v -n "welcome" paypal.txt > funpal.txt


Sort : Used to arrange numbers/text in ascending/descending order.

* by default it arrages ascending order.

Syntax:

$ sort [-option] [redirection synbol ] filename [redirection synbol] [new filename]

options:

-r   Arrage data in reverse or descending order

-n   Arrage data in ascending or descending order by considering whole number.

* if n is not used then numbers gets arranged in order based on 1st digit.

Ex1:

$ sort > paypal.txt
6
2
9
1
5
3

$ cat paypal.txt      Ascending order

$ sort -r paypal.txt > funpal.txt     Descending order

$cat funpal.txt

Ex2:

$ sort >google.txt
176
2165
8
93
-----
------ [ctrl+d]

$cat google.txt     It considered first digit

$sort -n google.txt > yahoo.txt    It considered whole number.

more: This filter command used to display information from multiple files based n page wise.

It gives an identification of end of file for first file and begining of next file.

Syn:

$more [-option] file1,file2,file3, etc...

options:

-p   clears the screen and displays next file in the list of files

*note:

Enterkey retrives next file based on %s
spacebar retrieves complete data from next file

Ex: $more - p paypal.txt   funpal.txt

cut: Used to cut the required text from a file.It can cut the data on the columns and feilds.

Syntax:

$ cut [-option] filename [redirection symbol new filename]

-c   To cut the data in columns

-f    To cut the data in feilds that is that data which is separated by tab.

Ex1:

$ cat>paypal.txt
Hyderabad
Secunderabad
Andhra [ctrl+d]

$ cut -c1 paypal.txt    [Enter]
H
S
A

$cut -c3 paypal.txt     [Enter]

d
c
d

$cut -c1 -3 paypal.txt [Enter]

Hyd
Sec
And

Ex2:

$cat > funpal.txt [Enter]
India    Delhi
Andhra    Hyderabad
Peers    Net      
[ctrl+d]

$ cut   -f1   funpal.txt     [Enter]
India
Andhra
Peers

$ cut   -f2   funpal.txt    [Enter]

Delhi
Hyderabad
Net

wc: It will count number of lines,worlds,characters in a file.

Syntax:

$ wc [-option] filename

options:

-l    count number of lines

-w   count number of words

-c   count number of characters

Ex:

$ wc -l paypal.txt

$ wc -w paypal.txt

$ wc -c paypal.txt

uniq: This filter is used to get the uniq or duplicate lines from a file.Data should be in order.

Syntax: $ unix [-option]   filename

options:

-d   Display duplicate lines

-u   Display uniq lines

-c   Counts number of times each word has occured in a file

Ex:

$ cat > city.txt
ameerpet
ameerpet
peers
bhel
hyderabad
ameerpet
peers
secunderabad   [ctrl+d]

$ sort city.txt> city1.txt

$cat city1.txt      Ascending order of data displaying

$ uniq -u city1.txt

$ uniq -d city1.txt

$ uniq -c city1.txt

Followers