Sem 3‎ > ‎OS (Unix) LAB‎ > ‎

D3: Wc, Sort, Head, Tail, Grep

posted Aug 10, 2011, 6:51 AM by Neil Mathew   [ updated Aug 10, 2011, 7:36 AM ]

DAY 3 : Actually, this combines what we've done in about two or three labs.





wc

wc <filename>  : Word Count ( lines, words, characters)

Use following for specific counts,
-l : only lines
-w : only words
-c : only characters
-wl : words & lines
-wc : words * characters
-lc : lines and characters


$ cat > NewFile2
Tommy Boy v/s Green Lantern

$ wc Newfile2   <-- Subtle Reminder how file names are Case Sensitive.
wc: cannot open Newfile2: No such file or directory (error 2)

$ wc NewFile2
      1      5     28 NewFile2
$ wc -wl NewFile2
      1      5 NewFile2

$ cat < PUMA2
Jump & move it.
Jump & move it..
Jump & move it...
Yo!
$ wc PUMA2
      4     13     55 PUMA2
$ wc -w PUMA2
     13 PUMA2
$ wc -c PUMA2
     55 PUMA2
$ wc -l PUMA2
      4 PUMA2




sort

sort <filename>  : Sorts files taking each line as separate entity

Use following for specific results,
sort -r <filename> : reverse order (descending)
sort -o<TOfilename> <FROMfilename> : save sorted file


$ cat > JLA     <-- Creating a file JLA and filling it with their names.
Clark Kent
Bruce Wayne
Diana
Barry Allen
Hal Jordan
Arthur Curry
J'onn J'onzz

$ sort JLA      <-- Default Order.
Arthur Curry
Barry Allen
Bruce Wayne
Clark Kent
Diana
Hal Jordan
J'onn J'onzz

$ sort -r JLA   <-- Reverse Order
J'onn J'onzz
Hal Jordan
Diana
Clark Kent
Bruce Wayne
Barry Allen
Arthur Curry

$ cat > JLA2
Superman
Batman
Wonder Woman
Flash
Green Lantern
Aquaman
Martian Manhunter

$ sort JLA2    <-- Note the file JLA2 appears to be sorted
Aquaman
Batman
Flash
Green Lantern
Martian Manhunter
Superman
Wonder Woman

$ cat < JLA2   <--  But the sorted file is merely shown, not saved.
Superman
Batman
Wonder Woman
Flash
Green Lantern
Aquaman
Martian Manhunter

$ sort -oJLA_SORTED JLA  <--  Command done.

$ cat<JLA                <-- Original file Not Sorted.
Clark Kent
Bruce Wayne
Diana
Barry Allen
Hal Jordan
Arthur Curry
J'onn J'onzz

$ cat < JLA_SORTED       <-- New file Sorted.
Arthur Curry
Barry Allen
Bruce Wayne
Clark Kent
Diana
Hal Jordan
J'onn J'onzz

Now, using multiple files:

sort <filename> <filename2> : Combines both and sorts it all.
sort -o<TOfilename> <FROMfilename1> <FROMfilename2> : Saves the combined and sorted.

Also, use:

-m : treats each file as an entity instead of the lines within it
-u : avoids repetition when common entities from 2 files repeat


$ sort JLA JLA2
Aquaman
Arthur Curry
Barry Allen
Batman
Bruce Wayne
Clark Kent
Diana
Flash
Green Lantern
Hal Jordan
J'onn J'onzz
Martian Manhunter
Superman
Wonder Woman

$ sort -oJLA_together JLA JLA2

$ cat < JLA_together
Aquaman
Arthur Curry
Barry Allen
Batman
Bruce Wayne
Clark Kent
Diana
Flash
Green Lantern
Hal Jordan
J'onn J'onzz
Martian Manhunter
Superman
Wonder Woman

$ cat > FRAGMENT1  
John : Clone 3
$ cat > FRAGMENT2
John : Clone 1
$ cat > FRAGMENT3
John : Clone 4
( Currently Missing In Action )
$ cat > FRAGMENT4
John : Clone 5
( Best So Far )

$ sort FRAGMENT1 FRAGMENT2 FRAGMENT3 FRAGMENT4 
( Best So Far )     <-- Notice how inner contents of file were sorted too
( Currently Missing In Action )
John : Clone 1
John : Clone 3
John : Clone 4
John : Clone 5

$ sort -m FRAGMENT1 FRAGMENT2 FRAGMENT3 FRAGMENT4
John : Clone 1      <-- Using -m each file treated as separate entities.
John : Clone 3
John : Clone 4
( Currently Missing In Action )
John : Clone 5
( Best So Far )

$ cat > DPS
Vayne
Heimer
Warwick
Master Yi

$ cat > Range
Vayne
Ashe
Heimer
Brand

$ sort DPS Range
Ashe
Brand
Heimer                 <-- Common entities repeat 
Heimer
Master Yi
Vayne
Vayne
Warwick

$ sort -u DPS Range    <-- -u avoids repetition. 
Ashe
Brand
Heimer
Master Yi
Vayne
Warwick





head

head -n <filename>  :  displays the first n lines of a file

tail

tail -n <filename>  :  displays the last n lines of a file


$ cat < PUMA2
Jump & move it.
Jump & move it..
Jump & move it...
Yo!

$ head -10 PUMA2
Jump & move it.
Jump & move it..
Jump & move it...
Yo!

$ head -1 PUMA2
Jump & move it.

$ head -2 PUMA2
Jump & move it.
Jump & move it..

$ tail -1 PUMA2
Yo!

$ tail -2 PUMA2
Jump & move it...
Yo!





grep



Comments