summaryrefslogtreecommitdiff
path: root/ult/ult_3/script.rst
blob: c9740b9aac81ff927a06b6bcc13f1b12d0e44829 (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
.. Objectives
.. ----------
   
   .. At the end of this tutorial, you will be able to:
   
   ..   1. Display the contents of files.
   ..   2. Read only parts of a file.
   ..   3. Look at the statistical information ao a file.

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

..   1. Using Linux tools - Part 1
..   2. Using Linux tools - Part 2
 
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 3'.

.. L2

{{{ Show slide with objectives }}} 

.. R2

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

 1. Display the contents of files.
 #. Read only parts of a file.
 #. Look at the statistical information of a file.

.. L3

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

.. R3

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

.. R4

Let us begin with how to read a while as a whole.
The ``cat`` command is the most commonly used command to display the
contents of files. To view the contents of a file, say, ``foo.txt``, we
simply say, 

.. L4
::

    cat foo.txt

.. R5

You can see the contents of the file on the terminal. 

The cat command could also be used to concatenate the text of multiple
files. Say, we have two files,``foo.txt`` and ``bar.txt``, 

.. L5
::

    cat foo.txt bar.txt

.. R6

It shows the output of both the files concatenated on the standard output. 
But if we had a long file,the output of ``cat`` command is not convenient 
to read.
Let's look at the ``less`` command which turns out to be more useful in 
such a case. 

``less `` allows you to view the contents of a text file one screen at a
time. 

.. L6
::

    less wonderland.txt

.. R7

This shows us the file, one screen at a time.

.. L7

.. L8

{{{ Show slide with, less }}}

.. R8

``less`` has a list of commands that it allows you to use, once you have
started viewing a file. A few of the common ones have been listed below. 

    * q: Quit.

    * [Arrows]/[Page Up]/[Page Down]/[Home]/[End]: Navigation.

    * ng: Jump to line number n. Default is the start of the file.

    * /pattern: Search for pattern. Regular expressions can be used.

    * h: Help. 

.. R9

Let us move ahead with the topic. Often we just would like to get some 
statistical information about the file, rather than viewing the contents 
of the file. The ``wc`` command prints these details for a file. 

.. L9
::

    wc wonderland.txt

.. L10

{{{ Highlight the required portions accordingly while narrating }}}

.. R10

As you can, we get some information about the file.
The first number is the number of lines, the second is the number of words
and the third is the number of characters in the file. 

.. R11

Let us now look at a couple of commands that let you see parts of files,
instead of the whole file. The ``head`` and ``tail`` commands let you see 
parts of files, as their names suggest, the start and the end of a file,
respectively. 

.. L11
::

    head wonderland.txt

.. R12

It prints only the first 10 lines of the file. Similarly tail will print the
last 10 lines of the file. If we wish to change the number of lines that we
wish to view, we use the option ``-n``. 

.. L12
::

    head -n 1 wonderland.txt

.. R13

It print only the first line of the file. Similarly, we could print only
the last line of the file.

The most common use of the tail command is to monitor a continuously
changing file, for instance a log file. Say you have a process running,
which is continuously logging it's information to a file, for instance the
logs of the system messages. 

.. L13
::

    tail -f /var/log/dmesg

.. R14

This will show the last 10 lines of the file as expected, but along with
that, it starts monitoring the file. Any new lines added at the end of the
file, will be shown. To interrupt tail, while it is monitoring, hit
``Ctrl-C``. which will stop any process that is running from your
current shell. 

We looked at a couple of functions that allowed us to view a part of a file,
line-wise. We shall now look at a couple of commands that will allow us to look
at only certain sections of each line of a file and merge those parts.
Let's take the ``/etc/passwd`` file as our example file. It contains
information about each user of the system.

.. L14
::

    cat /etc/passwd

.. R15

In the output, let us look at only the first, fifth, sixth and the last 
columns.The first column is the user name, the fifth column is the user info, 
the sixth column is the home folder and the last column is the path of the 
shell program that the user uses. 
Let's say we wish to look at only the user names of all the users in the
file, how do we do it?

.. L15
::
    
    cut -d : -f 1 /etc/passwd

.. R16

It gives us the required output. Let us understand this operation in detail.
The first option ``-d`` specifies the delimiter between the various fields in
the file, in this case it is the semicolon. If no delimiter is specified,
the TAB character is assumed to be the delimiter. The ``-f`` option specifies,
the field number that we want to choose. 
You can print multiple fields, by separating the field numbers with a
comma. 

Pause the video here, try out the following exercise and resume the video.

.. L16

.. L17

{{{ Show slide with exercise 3 }}}

.. R17

Print only the first, fifth and the seventh fields of the file ``/etc/passwd``.

.. R18

Switch to the terminal for solution

.. L18

{{{ continue from paused state }}}
{{{ Switch to the terminal }}}

::
    
    cut -d : -f 1,5,7 /etc/passwd

.. R19

We get the correct output.
Instead of choosing by fields, ``cut`` also allows us to choose on the
basis of characters or bytes. For instance, we could get the first 4
characters of all the entries of the file, ``/etc/passwd`` by saying,

.. L19
::

    cut -c 1-4 /etc/passwd 

.. R20

The end limits of the ranges can take sensible default values, if they are
left out. For example, 

.. L20
::

    cut -c -4 /etc/passwd 

.. R21

It gives the same output as before. If the start position has not been
specified, it is assumed to be the start of the line. Similarly if the end
position is not specified, it is assumed to be the end of the line. 

.. L21
::

    cut -c 10- /etc/passwd 

.. R22

It prints all the characters from the 10th character up to the end of the
line. 
Let us now solve an inverse problem. Let's say we have two columns of data
in two different files, and we wish to view them side by side. 

.. L22

.. L23

{{{ Show slide with, paste }}}

.. R23

For instance, given a file containing the names of students in a file,
students.txt, and another file with the marks of the students,marks.txt,

.. R24

we wish to view the contents, side by side. The ``paste`` command allows 
us to do that. 

.. L24
::

    paste students.txt marks.txt
    paste -s students.txt marks.txt

.. R25

The first command gives us the output of the two files, next to each other
and the second command gives us the output one below the other. 

Now, this problem is a bit unrealistic because, we wouldn't have the marks
of students in a file, without any information about the student to which
they belong. Let's say our marks file had the first column as the roll
number of the student, followed by the marks of the students. What would we
then do, to get the same output that we got before? 

Essentially we need to use both, the ``cut`` and ``paste`` commands, but
how do we do that? That brings us to the concept of Redirection and Piping
which is covered in the next spoken tutorial. 

.. L25

.. L26

{{{ Switch to summary slide }}}

.. R26

This brings us to the end of the end of this tutorial.
In this tutorial, we have learnt to, 

 1. Display the contents of files using the ``cat`` command.
 #. View the contents of a file one screen at a time using the 
    ``less`` command.
 #. Display specific contents of file using the ``head`` and 
    ``tail`` commands.
 #. Use the ``cut``, ``paste`` and ``wc`` commands.
  
.. L27

{{{ Show self assessment questions slide }}}

.. R27

Here are some self assessment questions for you to solve

1. How to view lines from 1 to 15 in wonderland.txt?

2. In ``cut`` command, how to specify space as the delimiter? 

.. L28

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

.. R28

And the answers,

1. We can use the head command as,
::

    head -15 wonderland.txt

2. We use the -d option with the command as,
::

    cut -d " " <filename>

.. L29

{{{ Show the Thank you slide }}}

.. R29

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