Open menu HomeLinksHolidaysWeddingsGalleryPlaces and CompanysTrip to KenyaLinux
23.2°c in Melksham, UK

Chapter 5

Introduction

Linux contains powerful utility programs. You can use these utility to

Following section introduce you to some of the essential utilities as well as expression. While programming shell you need to use these essential utilities. Some of these utilities (especially sed & awk) requires understanding of expression. After the quick introduction to utilities, you will learn the expression.

 

Prepering for Quick Tour of essential utilities

For this part of tutorial create sname and smark data files as follows (Using text editor of your choice)
Note Each data block is separated from the other by TAB character i.e. while creating the file if you type 11 then press "tab" key, and then write Vivek (as shown in following files):

sname

Sr.No     Name
11          Vivek
12          Renuka
13          Prakash
14         Ashish
15         Rani

smark

Sr.No    Mark
11          67
12          55
13          96
14          36
15          67

 

Selecting portion of a file using cut utility

Suppose from sname file you wish to print name of student on-screen, then from shell (Your command prompt i.e. $) issue command as follows:
$cut -f2 sname
Vivek
Renuka
Prakash
Ashish
Rani


cut utility cuts out selected data from sname file. To select Sr.no. field from sname give command as follows:
$cut -f1 sname
11
12
13
14
15

Command Explanation
cut    Name of cut utility
-f1  Using (-f) option, you are specifying the extraction field number. (In this example its 1 i.e. first field)
sname File which is used by cut utility and which is use as input for cut utility.

You can redirect output of cut utility as follows
$cut -f2 sname > /tmp/sn.tmp.$$
$cut -f2 smark > /tmp/sm.tmp.$$
$cat /tmp/sn.tmp.$$

Vivek
Renuka
Prakash
Ashish
Rani

$cat /tmp/sm.tmp.$$
67
55
96
36
67

General Syntax of cut utility:
Syntax:
cut -f[field number] [file-name]

Use of Cut utility:
Selecting portion of a file.

 

Putting lines together using paste utility

Now enter following command at shell prompt
$ paste sname smark
11    Vivek      11    67
12    Renuka   12    55
13    Prakash 13    96
14    Ashish   14     36
15    Rani      15     67

Paste utility join textual information together. To clear your idea try following command at shell prompt:

$ paste /tmp/sn.tmp.$$ /tmp/sm.tmp.$$
Vivek      67
Renuka    55
Prakash   96
Ashish     36
Rani        67

Paste utility is useful to put textual information together located in various files.

General Syntax of paste utility:
Syntax:
paste [file1] [file2]

Use of paste utility:
Putting lines together.

Can you note down basic difference between cut and paste utility?

 

The join utility

Now enter following command at shell prompt:
$join sname smark
11   Vivek       67
12   Renuka    55
13   Prakash  96
14   Ashish     36
15   Rani        67

Here students names are matched with their appropriate marks. How ? join utility uses the Sr.No. field to join to files. Notice that Sr.No. is the first field in both sname and smark file.

General Syntax of join utility:
Syntax:
join [file1] [file2]

Use of join utility:
The join utility joins, lines from separate files.

Note that join will only work, if there is common field in both file and if values are identical to each other.

 

Translateing range of characters using tr utility

Type the following command at shell prompt:
$ tr "h2" "3x" < sname
11   Vivek
1x   Renuka
13   Prakas3
14   As3is3
15   Rani

You can clearly see that each occurrence of character 'h' is replace with '3' and '2' with 'x'. tr utility translate specific characters into other specific characters or range of characters into other ranges.
h -> 3
2 -> x

Consider following example: (after executing command type text in lower case)
$ tr "[a-z]" "[A-Z]"
hi i am Vivek
HI I AM VIVEK
what a magic
WHAT A MAGIC

[Press CTRL + C to terminate.]

Here tr translate range of characters (i.e. small a to z) into other (i.e. to Capital A to Z) ranges.

General Syntax & use of tr utility:
Syntax:
tr [pattern-1] [pattern-2]

Use of tr utility:
To translate range of characters into other range of characters.

After typing following paragraph, I came to know my mistake that entire paragraph must be in lowercase characters, how to correct this mistake? (Hint - Use tr utility)

$ cat > lcommunity.txt
THIS IS SAMPLE PARAGRAPH
WRITTEN FOR LINUX COMMUNITY,
BY VIVEK G GITE (WHO ELSE?)
OKAY THAT IS OLD STORY.