Chapter 7
Chapter 7
I/ Definitions:
A filter is a command that reads input data from the keyboard (standard input), transforms it and
displays the result on the screen (standard output).
The commands cat, head, tail, wc, fold, cut, grep, sort and tr are filters. Filters are generally sensitive to
the local environment.
We'll have the files fileaa, fileab, fileac, ... each containing 10 lines. The first fileaa contains the first 10
lines, and so on.
2/ Sort files:
The sort command sorts, groups or compares all lines in files passed as parameters. If no file name
is supplied after the command, the file is read from the standard input, i.e. the keyboard.
By default, sort sorts the input lines according to lexicographical order on all characters on each
line (starting from the left) and displays the result on the screen. However, it is possible to specify more
precise sorting criteria: by dividing each line into fields separated by a space or tab, you can specify with
sort -f file
3/ String conversion :
The tr command converts one character string into another of equal size. The options are as follow:
▪ -c characters not in the original string are converted to characters in the destination string
▪ -d destruction of characters belonging to the original string
▪ -s if the destination string contains a contiguous sequence of identical characters, this sequence
is reduced to a single character
The tr command needs a file as input, with the result of the conversion displayed on standard output.
To do the same thing, you can also edit the file with cat and pipe to tr, by typing :
AminE:29:0298334432:BAtnA
We obtain all the lines of the file from the 10th to the end.
We can specify whether our unit is the line (default), the block or the character with the -t option.
We obtain all the lines of the file from the 10th to the beginning.
With the cut command, unlike sort, the first field is numbered 1, the second 2 and so on.
We can extract the first and fourth fields, the : is setting the field separator
3/ File merge:
The paste command is used to merge file lines. The options are as follows:
Amine:29:0298334432:Batna : engineer
We have:
Amine:29:0298334432:Batna
Ahmed:13:0466342233:Algiers
If the two are identical, the command generates no output; if they are different, the command
indicates the position of the first difference (line and character), with output like :
file1 file2 differ : char 34, line 2
2/ Editing the differences between two files:
The diff command searches for differences between two files, diff gives you indications that file1 is
identical to file2. Let's take the file notebook-address and the file notebook-address2
< Saleh:92:013344433:Oran
ps -u leila
Nice is generally used on time-consuming commands, but its effect on common commands is
imperceptible. For example, it can be used to compile a program.
2/ Stop a process:
The kill command is available to stop a process. To stop a process, you need to know its PID (ps
command),
kill -9 PID
Users can only stop processes that belong to them (that they have started). Only the system
administrator has the right to stop a process that does not belong to him/her.
When you enter a command followed by &, the shell immediately gives you control and displays the
PID number of the process launched.
If you launch a command from the shell without the & at the end, and it takes a long time to give you
back your hand, you can make it switch to background mode, so that you can take over again.
> netscape
311 stopped +
311 being the PID of the netscape process. Then type bg (for background), and you will see
[311]