summaryrefslogtreecommitdiff
path: root/ult/ult_5/script.rst
blob: 293154c1c40b66f916470d4a8ebdaeacd85b65c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
.. Objectives
.. ----------
   
   .. At the end of this tutorial, you will be able to:
   
   ..   1. 
   ..   2.

.. Prerequisites
.. -------------

..   1. Using Linux tools - Part 1
..   2. Using Linux tools - Part 2
..   3. Using Linux tools - Part 3
..   4. Using Linux tools - Part 4

 
Script
------

.. L1

{{{ Show the  first slide containing title, name of the production
team along with the logo of MHRD }}}

.. R1

Hello friends and Welcome to the tutorial on 
'Using linux tools - Part 5'.

.. L2

{{{ Show slide with objectives }}} 

.. R2

At the end of this tutorial, you will be able to,

 1. Learn text editing tools of linux. 

.. L3

{{{ Switch to the pre-requisite slide }}}

.. R3

Before beginning this tutorial,we would suggest you to complete the 
tutorial on "Using Linux tools from Part 1 to Part 4".

.. R4

In this tutorial, we shall learn about text processing.
Consider data kept in two files, namely marks1.txt and students.txt
Let us see what data they contain,

.. L4

{{{ Open the terminal }}}
::

    cat marks1.txt
    cat students.txt

.. R5

Let's say we wish to sort the output in the alphabetical order
of the names of the files. We can use the ``sort`` command for this
purpose.

We just pipe the previous output to the ``sort`` command as,

.. L5
::

    cut -d " " -f 2- marks1.txt | paste -d " " students.txt -| sort

.. R6

Let's say we wish to sort the names, based on the marks in the first
subject i.e. the first column after the name. ``sort`` command also allows us to
specify the delimiter between the fields and sort the data on a particular
field. ``-t`` option is used to specify the delimiter and ``-k`` option
is used to specify the field. 

.. L6
::

    cut -d " " -f 2- marks1.txt | paste -d " " students.txt -| sort -t " " -k 2

.. L7

{{{ Show slide with, Sort... }}}

.. R7

This command give us a sorted output as required. But, it would be
nicer to have the output sorted in the reverse order.

.. R8

 ``-r`` option allows the output to be sorted in the reverse order and 
the ``-n`` option is used to choose a numerical sorting. 

.. L8

{{ Switch to the terminal }}}
::

    cut -d " " -f 2- marks1.txt | paste -d " " students.txt -| sort -t " " -k 2 -rn

.. R9

Suppose, While you are compiling the student marklist, Anne walks up to you and
wants to know her marks. You, being a kind person that you are, oblige.
But you do not wish to her to see the marks that others have scored. What
do you do? The ``grep`` command comes to your rescue. 

``grep`` is a command line text search utility. You can use it to search
for Anne and show her, what she scored. ``grep`` allows you to search for a
search string in files. But you could, like any other command, pipe the
output of other commands to it. So, we shall use the previous combination
of cut and paste that we had, to get the marks of students along with their
names and search for Anne in that. 

.. L9
::

    cut -d " " -f 2- marks1.txt | paste -d " " students.txt - | grep Anne 

.. R10

This will give you only the line containing the word Anne as the output.
The grep command is by default case-sensitive. So, you wouldn't have got
the result if you had searched for anne, with a small a, instead of 
Anne, with a capital a. But, what if you didn't know, whether the name was 
capitalized or not? ``grep`` allows you to do case-insensitive searches 
by using the ``-i`` option. 

.. L10
::

    cut -d " " -f 2- marks1.txt | paste -d " " students.txt - | grep -i Anne 

.. R11

Now, in another scenario, if you wished to print all the lines, which do
not contain the word Anne, you could use the ``-v`` option. 

.. L11
::

    cut -d " " -f 2- marks1.txt | paste -d " " students.txt - | grep -iv Anne

.. R12

grep allows you to do more complex searches, for instance, searching for
sentences starting or ending with a particular pattern and regular
expression based searches. 

{{{ Show slide with, tr }}}

``tr`` is a command that takes two sets of characters as parameters, and
replaces occurrences of the characters in the first set with the
corresponding elements from the other set. It reads from the standard
output and writes to the standard output. 

For instance, if you wish to replace all the lower case letters in the
students file with upper case, you can do it as, 

.. L12

{{{ Switch to the terminal }}}
::

    cat students.txt | tr a-z A-Z

.. R13

A common task is to remove empty newlines from a file. The ``-s`` flag
causes ``tr`` to compress sequences of identical adjacent characters in its
output to a single token. For example,

.. L13
::

    tr -s '\n' '\n'

.. R14

Hit enter 2-3 times and see that every time we hit enter we get a newline.

.. L14
::

    <Enter>
    <Enter> 

.. R15

It replaces sequences of one or more newline characters with a single newline.

The ``-d`` flag causes ``tr`` to delete all tokens of the specified set of
characters from its input. In this case, only a single character set
argument is used. The following command removes carriage return characters,
thereby converting a file in DOS/Windows format to the Unix format. 

.. L15
::

    cat foo.txt | tr -d '\r' > bar.txt

.. R16

The ``-c`` flag complements the first set of characters.

.. L16
::

    tr -cd '[:alnum:]' 

.. R17

therefore removes all non-alphanumeric characters.

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 ``uniq`` command to achieve this. Let us first have a look at our file

.. L17
::

    cat items.txt

.. R18

Now, let us try and get rid of the duplicate lines from this file using 
the ``uniq`` command.

.. L18
::

    uniq items.txt

.. R19

Nothing happens! Why? The ``uniq`` command removes duplicate lines only when they 
are next to each other. So, henceforth, we get a sorted file from the original file and work 
with that file. 

.. L19
::

    sort items.txt | uniq

.. R20

``uniq -u`` command gives the lines which are unique and do not have any 
duplicates in the file. ``uniq -d`` outputs only those lines which have duplicates. 

.. L20 
::

    uniq -u items-sorted.txt 

.. R21

The ``-c`` option displays the number of times each line occurs in the file.

.. L21
::

    uniq -dc items-sorted.txt

.. L22

{{{ Show summary slide }}}

.. R22

This brings us to the end of the end of this tutorial.
In this tutorial, we have learnt to, 
 
  1. Use the ``sort`` command to sort lines of text files.
  #. Use the ``grep`` command to search text pattern.
  #. Use the ``tr`` command to translate and/or delete characters.
  #. Use the ``uniq`` command to omit repeated lines in a text. 

.. L23

{{{ Show self assessment questions slide }}}

.. R23

Here are some self assessment questions for you to solve

.. L24

{{{ Solution of self assessment questions on slide }}}

.. R24

And the answers,

.. L25

{{{ Show the Thank you slide }}}

.. R25

Hope you have enjoyed this tutorial and found it useful.
Thank you!