SEESenv/web/html/chn10session4.html
author amit@thunder
Thu, 25 Feb 2010 00:45:20 +0530
changeset 28 514098969b11
parent 27 cb14131583c6
permissions -rw-r--r--
Testing
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
     1
<html>
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     2
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Chapter. session</title><link rel="stylesheet" href="/review/support/styles.css" type="text/css" /><meta name="generator" content="DocBook XSL Stylesheets V1.74.3" /><link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" /><script type="text/javascript" src="/review/support/jquery-min.js"></script><script type="text/javascript" src="/review/support/form.js"></script><script type="text/javascript" src="/review/support/hsbook.js"></script></head>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     3
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" id="ch10session4">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     4
<div class="titlepage"></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     5
<div class="toc">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     6
<p><b>Table of Contents</b></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     7
<dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     8
<dt><span class="article"><a href="#id2773576"></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     9
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    10
<dt><span class="section"><a href="#id2583667">1. More text processing</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    11
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    12
<dt><span class="section"><a href="#id2771691">1.1. <code class="literal">sort</code></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    13
<dt><span class="section"><a href="#id2824598">1.2. <code class="literal">uniq</code></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    14
<dt><span class="section"><a href="#id2824700">1.3. <code class="literal">join</code></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    15
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    16
<dt><span class="section"><a href="#id2824800">2. Generating a word frequency list</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    17
<dt><span class="section"><a href="#id2824947">3. Basic editing and editors</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    18
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    19
<dt><span class="section"><a href="#id2824956">3.1. vim</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    20
<dt><span class="section"><a href="#id2825522">3.2. SciTE</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    21
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    22
<dt><span class="section"><a href="#id2825567">4. Personalizing your Environment</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    23
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    24
<dt><span class="section"><a href="#id2825576">4.1. .bashrc</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    25
<dt><span class="section"><a href="#id2825876">4.2. .vimrc</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    26
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    27
<dt><span class="section"><a href="#id2826095">5. Subshells and <code class="literal">source</code></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    28
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    29
</dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    30
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    31
<div class="article">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    32
<div class="titlepage">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    33
<div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    34
<div><h2 class="title" id="id2773576"></h2></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    35
<div><h2 class="title" id="id2773576"></h2></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    36
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    37
<hr />
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    38
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    39
<div class="toc">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    40
<p><b>Table of Contents</b></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    41
<dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    42
<dt><span class="section"><a href="#id2583667">1. More text processing</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    43
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    44
<dt><span class="section"><a href="#id2771691">1.1. <code class="literal">sort</code></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    45
<dt><span class="section"><a href="#id2824598">1.2. <code class="literal">uniq</code></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    46
<dt><span class="section"><a href="#id2824700">1.3. <code class="literal">join</code></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    47
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    48
<dt><span class="section"><a href="#id2824800">2. Generating a word frequency list</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    49
<dt><span class="section"><a href="#id2824947">3. Basic editing and editors</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    50
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    51
<dt><span class="section"><a href="#id2824956">3.1. vim</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    52
<dt><span class="section"><a href="#id2825522">3.2. SciTE</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    53
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    54
<dt><span class="section"><a href="#id2825567">4. Personalizing your Environment</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    55
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    56
<dt><span class="section"><a href="#id2825576">4.1. .bashrc</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    57
<dt><span class="section"><a href="#id2825876">4.2. .vimrc</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    58
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    59
<dt><span class="section"><a href="#id2826095">5. Subshells and <code class="literal">source</code></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    60
</dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    61
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    62
<div class="section" title="1.More text processing">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    63
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    64
<a name="id2583667"></a>1.More text processing</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    65
<div class="section" title="1.1.sort">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    66
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    67
<a name="id2771691"></a>1.1.<code class="literal">sort</code>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    68
</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    69
<p id="ch10session4_1">Let's say we have a file which lists a few of the stalwarts of the open source community and a few details about them, like their "other" name, their homepage address, and what they are well known for or their claim to fame.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    70
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    71
Richard Stallman%rms%GNU Project
27
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
    72
Eric Raymond%ESR%Jargon File
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
    73
Ian Murdock% %Debian
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
    74
Lawrence Lessig% %Creative Commons
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
    75
Linus Torvalds% %Linux Kernel
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
    76
Guido van Rossum%BDFL%Python
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    77
Larry Wall% %Perl</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    78
<p id="ch10session4_2">The sort command enables us to do this in a flash! Just running the sort command with the file name as a parameter sorts the lines of the file alphabetically and prints the output on the terminal.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    79
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    80
$ sort stalwarts.txt
27
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
    81
Eric Raymond%ESR%Jargon File
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
    82
Guido van Rossum%BDFL%Python
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
    83
Ian Murdock% %Debian
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
    84
Larry Wall% %Perl
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
    85
Lawrence Lessig% %Creative Commons
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
    86
Linus Torvalds% %Linux Kernel
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    87
Richard Stallman%rms%GNU Project</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    88
<p id="ch10session4_3">If you wish to sort them reverse alphabetically, you just need to pass the <code class="literal">-r</code> option. Now, you might want to sort the lines, based on each person's claim to fame or their "other" name. What do we do in that case?</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    89
<p id="ch10session4_4">Below is an example that sorts the file based on "other" names.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    90
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    91
$ sort -t % -k 2,2  stalwarts.txt
27
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
    92
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
    93
Ian Murdock% %Debian
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
    94
Larry Wall% %Perl
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
    95
Lawrence Lessig% %Creative Commons
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
    96
Linus Torvalds% %Linux Kernel
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
    97
Guido van Rossum%BDFL%Python
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
    98
Eric Raymond%ESR%Jargon File
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    99
Richard Stallman%rms%GNU Project</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   100
<p id="ch10session4_5">Sort command assumes white space to be the default delimiter for columns in each line. The <code class="literal">-t</code> option specifies the delimiting character, which is <code class="literal">%</code> in this case.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   101
<p id="ch10session4_6">The <code class="literal">-k</code> option starts a key at position 2 and ends it at 2, essentially telling the sort command that it should sort based on the 2nd column, which is the other name. <code class="literal">sort</code> also supports conflict resolution using multiple columns for sorting. You can see that the first three lines have nothing in the "other" names column. We could resolve the conflict by sorting based on the project names (the 3rd column).</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   102
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   103
$ sort -t % -k 2,2 -k 3,3  stalwarts.txt
27
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   104
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   105
Lawrence Lessig% %Creative Commons
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   106
Ian Murdock% %Debian
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   107
Linus Torvalds% %Linux Kernel
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   108
Larry Wall% %Perl
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   109
Guido van Rossum%BDFL%Python
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   110
Eric Raymond%ESR%Jargon File
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   111
Richard Stallman%rms%GNU Project</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   112
<p id="ch10session4_7"><code class="literal">sort</code> also has a lot of other options like ignoring case differences, month sort(JAN&lt;FEB&lt;...), merging already sorted files. <code class="literal">man sort</code> would give you a lot of information.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   113
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   114
<div class="section" title="1.2.uniq">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   115
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   116
<a name="id2824598"></a>1.2.<code class="literal">uniq</code>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   117
</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   118
<p id="ch10session4_8">Suppose we have a list of items, say books, and we wish to obtain a list which names of all the books only once, without any duplicates. We use the <code class="literal">uniq</code> command to achieve this.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   119
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   120
Programming Pearls
27
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   121
The C Programming Language
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   122
The Mythical Man Month: Essays on Software Engineering
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   123
Programming Pearls
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   124
The C Programming Language
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   125
Structure and Interpretation of Computer Programs
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   126
Programming Pearls
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   127
Compilers: Principles, Techniques, and Tools
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   128
The C Programming Language
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   129
The Art of UNIX Programming
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   130
Programming Pearls
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   131
The Art of Computer Programming
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   132
Introduction to Algorithms
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   133
The Art of UNIX Programming
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   134
The Pragmatic Programmer: From Journeyman to Master
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   135
Programming Pearls
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   136
Unix Power Tools
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   137
The Art of UNIX Programming</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   138
<p id="ch10session4_9">Let us try and get rid of the duplicate lines from this file using the <code class="literal">uniq</code> command.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   139
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   140
$ uniq items.txt
27
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   141
Programming Pearls
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   142
The C Programming Language
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   143
The Mythical Man Month: Essays on Software Engineering
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   144
Programming Pearls
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   145
The C Programming Language
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   146
Structure and Interpretation of Computer Programs
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   147
Programming Pearls
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   148
Compilers: Principles, Techniques, and Tools
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   149
The C Programming Language
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   150
The Art of UNIX Programming
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   151
Programming Pearls
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   152
The Art of Computer Programming
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   153
Introduction to Algorithms
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   154
The Art of UNIX Programming
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   155
The Pragmatic Programmer: From Journeyman to Master
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   156
Programming Pearls
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   157
Unix Power Tools
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   158
The Art of UNIX Programming</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   159
<p id="ch10session4_a">Nothing happens! Why? The <code class="literal">uniq</code> command removes duplicate lines only when they are next to each other. So, we get a sorted file from the original file and work with that file, henceforth.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   160
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   161
$ sort items.txt &gt; items-sorted.txt
27
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   162
$ uniq items-sorted.txt
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   163
Compilers: Principles, Techniques, and Tools
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   164
Introduction to Algorithms
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   165
Programming Pearls
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   166
Structure and Interpretation of Computer Programs
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   167
The Art of Computer Programming
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   168
The Art of UNIX Programming
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   169
The C Programming Language
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   170
The Mythical Man Month: Essays on Software Engineering
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   171
The Pragmatic Programmer: From Journeyman to Master
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   172
Unix Power Tools</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   173
<p id="ch10session4_b"><code class="literal">uniq -u</code> command gives the lines which are unique and do not have any duplicates in the file. <code class="literal">uniq -d</code> outputs only those lines which have duplicates. The <code class="literal">-c</code> option displays the number of times each line occurs in the file.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   174
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   175
$ uniq -u items-sorted.txt
27
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   176
Compilers: Principles, Techniques, and Tools
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   177
Introduction to Algorithms
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   178
Structure and Interpretation of Computer Programs
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   179
The Art of Computer Programming
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   180
The Mythical Man Month: Essays on Software Engineering
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   181
The Pragmatic Programmer: From Journeyman to Master
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   182
Unix Power Tools
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   183
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   184
$ uniq -dc items-sorted.txt
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   185
5 Programming Pearls
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   186
3 The Art of UNIX Programming
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   187
3 The C Programming Language</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   188
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   189
<div class="section" title="1.3.join">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   190
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   191
<a name="id2824700"></a>1.3.<code class="literal">join</code>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   192
</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   193
<p id="ch10session4_c">Now suppose we had the file <code class="literal">stalwarts1.txt</code>, which lists the home pages of all the people listed in <code class="literal">stalwarts.txt</code>.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   194
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   195
Richard Stallman%http://www.stallman.org
27
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   196
Eric Raymond%http://www.catb.org/~esr/
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   197
Ian Murdock%http://ianmurdock.com/
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   198
Lawrence Lessig%http://lessig.org
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   199
Linus Torvalds%http://torvalds-family.blogspot.com/
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   200
Guido van Rossum%http://www.python.org/~guido/
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   201
Larry Wall%http://www.wall.org/~larry/</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   202
<p id="ch10session4_d">It would be nice to have a single file with the information in both the files. To achieve this we use the <code class="literal">join</code> command.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   203
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   204
$ join stalwarts.txt stalwarts1.txt -t %
27
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   205
Richard Stallman%rms%GNU Project%http://www.stallman.org
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   206
Eric Raymond%ESR%Jargon File%http://www.catb.org/~esr/
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   207
Ian Murdock% %Debian%http://ianmurdock.com/
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   208
Lawrence Lessig% %Creative Commons%http://lessig.org
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   209
Linus Torvalds% %Linux Kernel%http://torvalds-family.blogspot.com/
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   210
Guido van Rossum%BDFL%Python%http://www.python.org/~guido/
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   211
Larry Wall% %Perl%http://www.wall.org/~larry/</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   212
<p id="ch10session4_e">The <code class="literal">join</code> command joins the two files, based on the common field present in both the files, which is the name, in this case.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   213
<p id="ch10session4_f">The <code class="literal">-t</code> option again specifies the delimiting character. Unless that is specified, join assumes that the fields are separated by spaces.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   214
<p id="ch10session4_10">Note that, for <code class="literal">join</code> to work, the common field should be in the same order in both the files. If this is not so, you could use <code class="literal">sort</code>, to sort the files on the common field and then join the files. In the above example, we have the common field to be the first column in both the files. If this is not the case we could use the <code class="literal">-1</code> and <code class="literal">-2</code> options to specify the field to be used for joining the files.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   215
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   216
$ join -2 2 stalwarts.txt stalwarts2.txt -t %
27
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   217
Richard Stallman%rms%GNU Project%http://www.stallman.org
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   218
Eric Raymond%ESR%Jargon File%http://www.catb.org/~esr/
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   219
Ian Murdock% %Debian%http://ianmurdock.com/
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   220
Lawrence Lessig% %Creative Commons%http://lessig.org
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   221
Linus Torvalds% %Linux Kernel%http://torvalds-family.blogspot.com/
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   222
Guido van Rossum%BDFL%Python%http://www.python.org/~guido/
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   223
Larry Wall% %Perl%http://www.wall.org/~larry/</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   224
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   225
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   226
<div class="section" title="2.Generating a word frequency list">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   227
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   228
<a name="id2824800"></a>2.Generating a word frequency list</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   229
<p id="ch10session4_11">Now, let us use the tools we have learnt to use, to generate a word frequency list of a text file. We shall use the free text of Alice in Wonderland.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   230
<p id="ch10session4_12">The basic steps to achieve this task would be -</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   231
<div class="orderedlist"><ol class="orderedlist" type="1">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   232
<li class="listitem"><p id="ch10session4_13">Eliminate the punctuation and spaces from the document.</p></li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   233
<li class="listitem"><p id="ch10session4_14">Generate a list of words.</p></li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   234
<li class="listitem"><p id="ch10session4_15">Count the words.</p></li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   235
</ol></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   236
<p id="ch10session4_16">We first use <code class="literal">grep</code> and some elementary <code class="literal">regex</code> to eliminate the non-alpha-characters.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   237
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   238
$ grep "[A-Za-z]*" alice-in-wonderland.txt</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   239
<p id="ch10session4_17">This outputs all the lines which has any alphabetic characters on it. This isn't of much use, since we haven't done anything with the code. We only require the alphabetic characters, without any of the other junk. <code class="literal">man grep</code> shows us the <code class="literal">-o</code> option for outputting only the text which matches the regular expression.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   240
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   241
$ grep "[A-Za-z]*" -o alice-in-wonderland.txt</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   242
<p id="ch10session4_18">Not very surprisingly, we have all the words, spit out in the form of a list! Now that we have a list of words, it is quite simple to count the occurrences of the words. You would've realized that we can make use of <code class="literal">sort</code> and <code class="literal">uniq</code> commands. We pipe the output from the <code class="literal">grep</code> to the <code class="literal">sort</code> and then pipe it's output to <code class="literal">uniq</code>.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   243
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   244
$ grep "[A-Za-z]*" -o alice-in-wonderland.txt | sort | uniq -c</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   245
<p id="ch10session4_19">Notice that you get the list of all words in the document in the alphabetical order, with it's frequency written next to it. But, you might have observed that Capitalized words and lower case words are being counted as different words. We therefore, replace all the Upper case characters with lower case ones, using the <code class="literal">tr</code> command.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   246
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   247
$ grep  "[A-Za-z]*" -o alice-in-wonderland.txt | tr 'A-Z' 'a-z' | sort | uniq -c</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   248
<p id="ch10session4_1a">Now, it would also be nice to have the list ordered in the decreasing order of the frequency of the appearance of the words. We sort the output of the <code class="literal">uniq</code> command with <code class="literal">-n</code> and <code class="literal">-r</code> options, to get the desired output.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   249
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   250
$ grep  "[A-Za-z]*" -o alice-in-wonderland.txt | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   251
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   252
<div class="section" title="3.Basic editing and editors">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   253
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   254
<a name="id2824947"></a>3.Basic editing and editors</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   255
<div class="section" title="3.1.vim">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   256
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   257
<a name="id2824956"></a>3.1.vim</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   258
<p id="ch10session4_1b">Vim is a very powerful editor. It has a lot of commands, and all of them cannot be explained here. We shall try and look at a few, so that you can find your way around in vim.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   259
<p id="ch10session4_1c">To open a file in vim, we pass the filename as a parameter to the <code class="literal">vim</code> command. If a file with that filename does not exist, a new file is created.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   260
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   261
$ vim first.txt</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   262
<p id="ch10session4_1d">To start inserting text into the new file that we have opened, we need to press the <code class="literal">i</code> key. This will take us into the <span class="emphasis"><em>insert</em></span> mode from the <span class="emphasis"><em>command</em></span> mode. Hitting the <code class="literal">esc</code> key, will bring us back to the <span class="emphasis"><em>command</em></span> mode. There is also another mode of vim, called the <span class="emphasis"><em>visual</em></span> mode which will be discussed later in the course.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   263
<p id="ch10session4_1e">In general, it is good to spend as little time as possible in the insert mode and extensively use the command mode to achieve various tasks.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   264
<p id="ch10session4_1f">To save the file, use <code class="literal">:w</code> in the command mode. From here on, it is understood that we are in the command mode, whenever we are issuing any command to vim.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   265
<p id="ch10session4_20">To save a file and continue editing, use <code class="literal">:w FILENAME</code>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   266
The file name is optional. If you do not specify a filename, it is saved in the same file that you opened. If a file name different from the one you opened is specified, the text is saved with the new name, but you continue editing the file that you opened. The next time you save it without specifying a name, it gets saved with the name of the file that you initially opened.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   267
<p id="ch10session4_21">To save file with a new name and continue editing the new file, use <code class="literal">:saveas FILENAME</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   268
<p id="ch10session4_22">To save and quit, use <code class="literal">:wq</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   269
<p id="ch10session4_23">To quit, use <code class="literal">:q</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   270
<p id="ch10session4_24">To quit without saving, use <code class="literal">:q!</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   271
<div class="section" title="3.1.1.Moving around">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   272
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   273
<a name="id2825077"></a>3.1.1.Moving around</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   274
<p id="ch10session4_25">While you are typing in a file, it is in-convenient to keep moving your fingers from the standard position for typing to the arrow keys. Vim, therefore, provides alternate keys for moving in the document. Note again that, you should be in the command mode, when issuing any commands to vim.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   275
<p id="ch10session4_26">The basic cursor movement can be achieved using the keys, <code class="literal">h</code> (left), <code class="literal">l</code> (right), <code class="literal">k</code> (up) and <code class="literal">j</code> (down).</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   276
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   277
      ^
27
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   278
      k
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   279
&lt; h       l &gt;
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   280
      j
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   281
      v</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   282
<p id="ch10session4_27">Note: Most commands can be prefixed with a number, to repeat the command. For instance, <code class="literal">10j</code> will move the cursor down 10 lines.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   283
<div class="section" title="3.1.1.1.Moving within a line">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   284
<div class="titlepage"><div><div><h5 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   285
<a name="id2825129"></a>3.1.1.1.Moving within a line</h5></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   286
<p id="ch10session4_28">Cursor Movement</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   287
<p id="ch10session4_29">Command</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   288
<p id="ch10session4_2a">Forward by a screenful of text</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   289
<p id="ch10session4_2b"><code class="literal">C-f</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   290
<p id="ch10session4_2c">Backward by a screenful of text</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   291
<p id="ch10session4_2d"><code class="literal">C-b</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   292
<p id="ch10session4_2e">Beginning of the screen</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   293
<p id="ch10session4_2f"><code class="literal">H</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   294
<p id="ch10session4_30">Middle of the screen</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   295
<p id="ch10session4_31"><code class="literal">M</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   296
<p id="ch10session4_32">End of the screen</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   297
<p id="ch10session4_33"><code class="literal">L</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   298
<p id="ch10session4_34">End of file</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   299
<p id="ch10session4_35"><code class="literal">G</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   300
<p id="ch10session4_36">Line number <code class="literal">num</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   301
<p id="ch10session4_37"><code class="literal">[num]G</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   302
<p id="ch10session4_38">Beginning of file</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   303
<p id="ch10session4_39"><code class="literal">gg</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   304
<p id="ch10session4_3a">Next occurrence of the text
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   305
under the cursor</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   306
<p id="ch10session4_3b"><code class="literal">*</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   307
<p id="ch10session4_3c">Previous occurrence of the text
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   308
under the cursor</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   309
<p id="ch10session4_3d"><code class="literal">#</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   310
<p id="ch10session4_3e">Note: <code class="literal">C-x</code> is <code class="literal">Ctrl</code> + <code class="literal">x</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   311
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   312
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   313
<div class="section" title="3.1.2.The visual mode">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   314
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   315
<a name="id2825272"></a>3.1.2.The visual mode</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   316
<p id="ch10session4_3f">The visual mode is a special mode that is not present in the original vi editor. It allows us to highlight text and perform actions on it. All the movement commands that have been discussed till now work in the visual mode also. The editing commands that will be discussed in the future work on the visual blocks selected, too.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   317
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   318
<div class="section" title="3.1.3.Editing commands">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   319
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   320
<a name="id2825290"></a>3.1.3.Editing commands</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   321
<p id="ch10session4_40">The editing commands usually take the movements as arguments. A movement is equivalent to a selection in the visual mode. The cursor is assumed to have moved over the text in between the initial and the final points of the movement. The motion or the visual block that's been highlighted can be passed as arguments to the editing commands.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   322
<p id="ch10session4_41">Editing effect</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   323
<p id="ch10session4_42">Command</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   324
<p id="ch10session4_43">Cut the character under the cursor</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   325
<p id="ch10session4_44"><code class="literal">x</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   326
<p id="ch10session4_45">Replace the character under the
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   327
cursor with <code class="literal">a</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   328
<p id="ch10session4_46"><code class="literal">ra</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   329
<p id="ch10session4_47">Cut an entire line</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   330
<p id="ch10session4_48"><code class="literal">dd</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   331
<p id="ch10session4_49">Copy/yank an entire line</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   332
<p id="ch10session4_4a"><code class="literal">yy</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   333
<p id="ch10session4_4b">Note: You can prefix numbers to any of the commands, to repeat them.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   334
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   335
<div class="section" title="3.1.4.Undo and Redo">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   336
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   337
<a name="id2825370"></a>3.1.4.Undo and Redo</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   338
<p id="ch10session4_4c">You can undo almost anything using <code class="literal">u</code>.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   339
<p id="ch10session4_4d">To undo the undo command type <code class="literal">C-r</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   340
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   341
<div class="section" title="3.1.5.Searching and Replacing">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   342
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   343
<a name="id2825395"></a>3.1.5.Searching and Replacing</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   344
<p id="ch10session4_4e">Finding and Replacing</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   345
<p id="ch10session4_4f">Command</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   346
<p id="ch10session4_50">Replace the first instance of <code class="literal">old</code>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   347
with <code class="literal">new</code> in the current line.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   348
<p id="ch10session4_51"><code class="literal">:s/old/new</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   349
<p id="ch10session4_52">Replace all instances of <code class="literal">old</code>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   350
with <code class="literal">new</code> in the current line.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   351
<p id="ch10session4_53"><code class="literal">:s/old/new/g</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   352
<p id="ch10session4_54">Replace all instances of <code class="literal">old</code>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   353
with <code class="literal">new</code> in the current line,
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   354
but ask for confirmation each time.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   355
<p id="ch10session4_55"><code class="literal">:s/old/new/gc</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   356
<p id="ch10session4_56">Replace the first instance of <code class="literal">old</code>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   357
with <code class="literal">new</code> in the entire file.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   358
<p id="ch10session4_57"><code class="literal">:%s/old/new</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   359
<p id="ch10session4_58">Replace all instances of <code class="literal">old</code>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   360
with <code class="literal">new</code> in the entire file.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   361
<p id="ch10session4_59"><code class="literal">:%s/old/new/g</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   362
<p id="ch10session4_5a">Replace all instances of <code class="literal">old</code> with
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   363
<code class="literal">new</code> in the entire file but ask
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   364
for confirmation each time.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   365
<p id="ch10session4_5b"><code class="literal">:%s/old/new/gc</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   366
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   367
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   368
<div class="section" title="3.2.SciTE">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   369
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   370
<a name="id2825522"></a>3.2.SciTE</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   371
<p id="ch10session4_5c">SciTE is a <span class="emphasis"><em>source code</em></span> editor, that has a feel similar to the commonly used GUI text editors. It has a wide range of features that are extremely useful for a programmer, editing code. Also it aims to keep configuration simple, and the user needs to edit a text file to configure SciTE to his/her liking.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   372
<p id="ch10session4_5d">Opening, Saving, Editing files with SciTE is extremely simple and trivial. Knowledge of using a text editor will suffice.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   373
<p id="ch10session4_5e">SciTE can syntax highlight code in various languages. It also has auto-indentation, code-folding and other such features which are useful when editing code.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   374
<p id="ch10session4_5f">SciTE also gives you the option to (compile and) run your code, from within the editor.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   375
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   376
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   377
<div class="section" title="4.Personalizing your Environment">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   378
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   379
<a name="id2825567"></a>4.Personalizing your Environment</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   380
<div class="section" title="4.1..bashrc">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   381
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   382
<a name="id2825576"></a>4.1..bashrc</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   383
<p id="ch10session4_60">What would you do, if you want bash to execute a particular command each time you start it up? For instance, say you want the current directory to be your Desktop instead of your home folder, each time bash starts up. How would you achieve this? Bash reads and executes commands in a whole bunch of files called start-up files, when it starts up.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   384
<p id="ch10session4_61">When bash starts up as an interactive login shell, it reads the files <code class="literal">/etc/profile</code>, <code class="literal">~/.bash_profile</code>, <code class="literal">~/.bash_login</code>, and <code class="literal">~/.profile</code> in that order.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   385
<p id="ch10session4_62">When it is a shell that is not a login shell, <code class="literal">~/.bashrc</code> is read and the commands in it are executed. This can be prevented using the <code class="literal">--norc</code> option. To force bash to use another file, instead of the <code class="literal">~/.bashrc</code> file on start-up, the <code class="literal">--rcfile</code> option may be used.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   386
<p id="ch10session4_63">Now, you know what you should do, to change the current directory to you Desktop. Just put a <code class="literal">cd ~/Desktop</code> into your <code class="literal">~/.bashrc</code> and you are set!</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   387
<p id="ch10session4_64">This example is quite a simple and lame one. The start-up files are used for a lot more complex things than this. You could set (or unset) aliases and a whole bunch of environment variables in the <code class="literal">.bashrc</code>. We shall look at them, in the next section where we look at environment variables and <code class="literal">set</code> command.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   388
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   389
<div class="section" title="4.2..vimrc">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   390
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   391
<a name="id2825876"></a>4.2..vimrc</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   392
<p id="ch10session4_65"><code class="literal">.vimrc</code> is a file similar to <code class="literal">.bashrc</code> for vim. It is a start-up file that vim reads and executes, each time it starts up. The options that you would like to be set every time you use vim, are placed in the <code class="literal">.vimrc</code> file, so that they are automatically set each time vim starts. The recommended place for having your <code class="literal">.vimrc</code> is also your home directory.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   393
<p id="ch10session4_66">The file <code class="literal">/etc/vimrc</code> is the global config file and shouldn't usually be edited. You can instead edit the <code class="literal">~/.vimrc</code> file that is present in your home folder.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   394
<p id="ch10session4_67">There are a whole bunch of variables that you could set in the <code class="literal">.vimrc</code> file. You can look at all the options available, using the <code class="literal">:set all</code> command in vim. You could use the <code class="literal">:help option_name</code> to get more information about the option that you want to set. Once you are comfortable with what you want to set a particular variable to, you could add it to <code class="literal">.vimrc</code>. You should also look at <code class="literal">:help vimrc</code> for more info on the <code class="literal">.vimrc</code> file. If you already have a <code class="literal">.vimrc</code> file, you can edit it from within vim, using <code class="literal">:e $MYVIMRC</code> command. We shall look at some of the most commonly used options.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   395
<p id="ch10session4_68">Command</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   396
<p id="ch10session4_69">Vim action</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   397
<p id="ch10session4_6a"><code class="literal">set nocompatible</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   398
<p id="ch10session4_6b">Explicitly disable compatibility with vi</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   399
<p id="ch10session4_6c"><code class="literal">set backspace=indent,eol,start</code></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   400
<p id="ch10session4_6d">In the insert mode, vim allows the backspace key to delete white spaces at the
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   401
start of line, line breaks and the character before which insert mode started.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   402
<p id="ch10session4_6e">set autoindent</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   403
<p id="ch10session4_6f">Vim indents a new line with the same indentation of the previous line.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   404
<p id="ch10session4_70">set backup</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   405
<p id="ch10session4_71">Vim keeps a backup copy of a file when overwriting it.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   406
<p id="ch10session4_72">set history=50</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   407
<p id="ch10session4_73">Vim keeps 50 commands and 50 search patterns in the history.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   408
<p id="ch10session4_74">set ruler</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   409
<p id="ch10session4_75">Displays the current cursor position in the lower right corner of the vim window.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   410
<p id="ch10session4_76">set showcmd</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   411
<p id="ch10session4_77">Displays the incomplete command in the lower right corner.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   412
<p id="ch10session4_78">set incsearch</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   413
<p id="ch10session4_79">Turns on incremental searching. Displays search results while you type.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   414
<p id="ch10session4_7a">You can see the effect of the changes made to your <code class="literal">.vimrc</code> file by restarting vim. If you want to see the changes that you made to your <code class="literal">.vimrc</code> file immediately, you could source the file from within vim.</p>If the <code class="literal">.vimrc</code> file has been sourced when this instance of vim was started, you could just resource the file again::<p id="ch10session4_7b">:so $MYVIMRC</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   415
<p id="ch10session4_7c">If you just created the <code class="literal">.vimrc</code> file or it was not sourced when you stared this instance of vim, just replace the <code class="literal">$MYVIMRC</code> variable above, with the location of the <code class="literal">.vimrc</code> file that you created/edited.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   416
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   417
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   418
<div class="section" title="5.Subshells and source">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   419
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   420
<a name="id2826095"></a>5.Subshells and <code class="literal">source</code>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   421
</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   422
<p id="ch10session4_7d">A subshell is just a separate instance of the shell which is a child process of the shell that launches it. Bash creates a subshell in various circumstances. Creation of subshells allows the execution of various processes simultaneously.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   423
<div class="itemizedlist"><ul class="itemizedlist" type="*">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   424
<li class="listitem" style="list-style-type: *"><p id="ch10session4_7e">When an external command is executed, a new subshell is created. Any built-in commands of bash are executed with int the same shell, and no new subshell is started. When an external command is run, the bash shell copies itself (along with it's environment) creating a subshell and the process is changed to the external command executed. The subshell is a child process of this shell.</p></li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   425
<li class="listitem" style="list-style-type: *"><p id="ch10session4_7f">Any pipes being used, create a subshell. The commands on the input and output ends of the pipe are run in different subshells.</p></li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   426
<li class="listitem" style="list-style-type: *"><p id="ch10session4_80">You could also, explicitly tell bash to start a subshell by enclosing a list of commands between parentheses. Each of the commands in the list is executed within a single new subshell.</p></li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   427
</ul></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   428
<p id="ch10session4_81">To avoid creating a subshell, when running a shell script, you could use the <code class="literal">source</code> command.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   429
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   430
$ source script.sh</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   431
<p id="ch10session4_82">This will run the <code class="literal">script.sh</code> within the present shell without creating a subshell. The <code class="literal">.</code> command is an alias for the source command. <code class="literal">. script.sh</code> is therefore equivalent to <code class="literal">source script.sh</code>.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   432
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   433
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   434
</div></body>
27
cb14131583c6 Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
diff changeset
   435
</html>
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   436