Filter Command
Filter Command
UMRAKH
SUB: PROGRAMMING LANGUAGE - I
SHORT EXTERNAL QUESTIONS
Q
Define Filters
SYBCA (SEM:2)
Page 1
Here
CODE
DESCRIPTION
+N
-N
Count by lines
-l
Count by line.
Count by characters
-c
Counts by character.
Count by block
-b
Reverse order
-r
SYBCA (SEM:2)
Page 2
SYBCA (SEM:2)
Page 3
SYBCA (SEM:2)
Page 4
$ cat file2
Dedicated server
Virtual server
$ cat file3
Hosting
Machine
SYBCA (SEM:2)
Page 5
File 2
$ cat file
$ cat file2
20
25
19
18
49
48
200
200
$ sort n file
200
19
49
SYBCA (SEM:2)
Page 6
20
49
19
200
18
18
19
19
20
20
25
25
48
48
49
49
200
200
200
File with multiple fields and delimiter: Let us consider a file with delimiter ,.
By default delimiter is space.
File
$ cat file
OM,20,SURAT,BCA,258307
SAI,19,BARDOLI,BBA,245678
RAM,5,NAVSARI,BBA,222434
VATSHAL,49,BARODA,BCA,258783
AADI,200,SURAT,BCA,264416
SYBCA (SEM:2)
Page 7
$ sort file
$ sort -r file
OR
$ sort -t"," -k1,1 file
AADI,200,SURAT,BCA,264416
OM,20,SURAT,BCA,258307
RAM,5,NAVSARI,BBA,222434
VATSHAL,49,BARODA,BCA,258783
SAI,19,BARDOLI,BBA,245678
RAM,5,NAVSARI,BBA,222434
OM,20,SURAT,BCA,258307
AADI,200,SURAT,BCA,264416
RAM,5,NAVSARI,BBA,222434
SAI,19,BARDOLI,BBA,245678
OM,20,SURAT,BCA,258307
VATSHAL,49,BARODA,BCA,258783
AADI,200,SURAT,BCA,264416
To reverser order.
SAI,19,BARDOLI,BBA,245678
VATSHAL,49,BARODA,BCA,258783
'-t' option is used to provide the delimiter in case of files with delimiter.
'-k' is used to specify the keys on the basis of which the sorting has to be done.
The format of '-k' is : '-km,n'
where m is the starting key and n is the ending key.
In our case, since the sorting is on the 1st field alone, we speciy '1,1'.
Similarly, if the sorting is to be done on the basis of first 3 fields, it will be: '-k 1,3'.
if the sorting is to be done 2 and 4 field, it will be: '-k 2, -k4'.
-k2,2 specifies sorting on the key starting and ending with column 2.
If -k2 is used instead, the sort key would begin at column 2 and extend to the end of the line,
spanning all the fields in between.
Sorting on Secondary filed using (-k) key option:
Sorting in ASCENDING order
$ sort -t"," k 3,3 k 2n,2 file
SAI,19,BARDOLI,BBA,245678
VATSHAL,49,BARODA,BCA,258783
RAM,5,NAVSARI,BBA,222434
OM,20,SURAT,BCA,258307
AADI,200,SURAT,BCA,264416
NOTE: Write answer as per marks. List out all option and give 2 to 3 example.
SYBCA (SEM:2)
Page 8
uniq
[option]
filename
OPTION
MEANING
$ cat file
Unix operating system
$ uniq file
unix operating system
$ cat file
Unix operating system
$ uniq c file
2 unix operating system
SYBCA (SEM:2)
Page 9
$ uniq d file
unix operating system
$ cat file
Unix operating system
$ uniq f2 file
unix operating system
set
of
characters
with
SYBCA (SEM:2)
Page 10
File
$ cat file
OM|20|SURAT|BCA|258307|11/08/1997
SAI|19|BARDOLI|BBA|245678|10/12/1994
RAM|5|NAVSARI|BBA|222434|10/11/1991
VATSHAL|49|BARODA|BCA|258783|01/04/1992
AADI|200|SURAT|BCA|264416|07/02/1990
SYBCA (SEM:2)
Page 11
linux server
SYBCA (SEM:2)
Page 12
SYBCA (SEM:2)
Page 13