Unix commands with examples for beginners

The article about unix commands with examples for beginners. This is my fifth session on unix command. I am sure it will help you to increase your core knowledge about unix.


Unix commands with examples for beginners

Searching the contents of a file

Simple searching using less

Using less, you can search a text file for a keyword (pattern). For example, to search through science.txt for the word ‘science’, type
% less science.txt
then, still in less (i.e. don’t press [q] to quit), then type a forward slash [/] followed by the word to search
/science
As you will see, less finds and highlights keyword. Type [n] to search for the next occurrence of the word.
let’s move to next command

grep

grep is one of standard UNIX utilities. It use to searches files for specified words or patterns. At first clear the screen, then type
% grep science science.txt
You can see, grep has printed out each line contains the word science.
Or has it????
Try the typing
% grep Science science.txt

Also one more important thing, grep command is case sensitive; it distinguishes between Science and science.
To ignore upper/lower case distinctions, use the -i option, i.e. type
% grep -i science science.txt
Search for a phrase or pattern, you must enclose it in single quotes (the apostrophe symbol). Example to search for spinning top, type
% grep -i ‘spinning top’ science.txt

Some of the other options of grep are available as:
-v display those lines that do NOT match
-n precede each matching line with the line number
-c print only the total count of matched lines


Try some of them and verify the different results. You can use more than one option at a time, for example, the number of lines without the words science or Science is
% grep -ivc science science.txt

Now next command is wc

wc (word count)

A handy little utility is the wc command, short for word count. To do a word count on science.txt, type
% wc -w science.txt
To find out how many lines the file has, type
% wc -l science.txt

Share This Post

Post Comment