0% found this document useful (0 votes)
24 views10 pages

Chapter 7

The document discusses Unix filters and commands that can modify, extract, compare, and search for information in files. It defines filters and describes commands like split, sort, tr, head, tail, cut, paste, comm, cmp, diff, grep, and find. It provides examples of using the options and parameters for each command.

Uploaded by

arabmelissa18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views10 pages

Chapter 7

The document discusses Unix filters and commands that can modify, extract, compare, and search for information in files. It defines filters and describes commands like split, sort, tr, head, tail, cut, paste, comm, cmp, diff, grep, and find. It provides examples of using the options and parameters for each command.

Uploaded by

arabmelissa18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Batna 2 university Mathematics and computer science faculty

Computer science department Computer Engineer 1st year 2023-2024


Introduction of operating systems

Chapter VII: Unix filters

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.

II/ Modify file data:


1/ Split a file to pieces:
The split command is used to split a file into several files, by typing:

split -10 file fich_split

We'll have the files fileaa, fileab, fileac, ... each containing 10 lines. The first fileaa contains the first 10
lines, and so on.

▪ The -l option divides the file by number of lines:

split -l 4 file fich_split

▪ The -verbose option used to display a creation report


▪ The -b option splits the file by size

split -b 16 file fich_split

▪ The -a option changes the suffix length


▪ The -d option changes the suffix to a number
▪ The -n option divides the file into n files

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

Chapter VII: Unix filters 1 Dr. Saadi Leila


Batna 2 university Mathematics and computer science faculty
Computer science department Computer Engineer 1st year 2023-2024
Introduction of operating systems
the -k option that the sorting concerns one or more particular fields.

▪ -r (reverse) to sort in reverse order;


▪ -f to ignore case (upper/lower case);
▪ -b (blank) to ignore leading blanks;
▪ -n (numeric) to sort in numeric order;
▪ -u (unique) to keep only the first copy of lines for sorting purposes (not necessarily identical);
▪ -kbegin , end (key) sort by numbered fields between begin and end inclusive;
▪ -tdelim allows you to select the delim field separator (by default, this is the [:blank:] category, which
includes spaces and tabs).

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.

Either a file notebook-address


Amine:29:0298334432:Batna
Ahmed:13:0466342233:Algiers
Ridha:75:0144234452:Annaba
Saleh:92:013344433:Algiers
To replace the : with a #, type :

tr " : " "#" < notebook-address

To do the same thing, you can also edit the file with cat and pipe to tr, by typing :

cat notebook-address | tr " : " "#"

Metacharacters can be used, by typing :

cat notebook-address | tr "[a-f] " "[A-F]"

AminE:29:0298334432:BAtnA

Chapter VII: Unix filters 2 Dr. Saadi Leila


Batna 2 university Mathematics and computer science faculty
Computer science department Computer Engineer 1st year 2023-2024
Introduction of operating systems
AhmED:13:0466342233:AlgiErs
RiDhA:75:0144234452:AnnABA
SalEh:92:013344433:AlgiErs

III/ File editing with criteria:


1/ Editing a file at the end:
If you have a very long file, and you only want to see the end, you can use the tail command. The syntax
is as follows: if you type :

tail +10 file

We obtain all the lines of the file from the 10th to the end.

tail -10 file

We get the last 10 lines from the end.

We can specify whether our unit is the line (default), the block or the character with the -t option.

tail -10 -c file

2/ Editing a file from the beginning:


If we have a very long file, and we only want to see the beginning, we can use the head command. The
syntax is as follows:

head +10 -c file

We obtain all the lines of the file from the 10th to the beginning.

3/ Extracting information from a file:


The cut command extracts certain fields from a file. The options are as follows:

▪ -c extracts by number of characters


▪ -f extracts by number of fields
▪ -dx the x character is the field separator

With the cut command, unlike sort, the first field is numbered 1, the second 2 and so on.

We use the example notebook-address


Amine:29:0298334432:Batna
Ahmed:13:0466342233:Algiers
Ridha:75:0144234452:Annaba

Chapter VII: Unix filters 3 Dr. Saadi Leila


Batna 2 university Mathematics and computer science faculty
Computer science department Computer Engineer 1st year 2023-2024
Introduction of operating systems
Saleh:92:013344433:Algiers
cut -c-10 notebook-address

To extract the first 10 characters of each line, we obtain :


Amine:29:0
Ahmed:13:0
Ridha:75:0
Saleh:92:0
cut -c2-5 notebook-address

we will extract the second to fifth character of each line.


mine
hmed
idha
aleh
we will extract from the 25th character to the end of each line.

cut -d: -f1,4 notebook-address

We can extract the first and fourth fields, the : is setting the field separator

cut -d : -f3- notebook-address

3/ File merge:
The paste command is used to merge file lines. The options are as follows:

▪ -dx the x character defines the field separator


▪ -s rows are replaced by columns

We use the example notebook-address


Amine:29:0298334432:Batna
Ahmed:13:0466342233:Algiers
Ridha:75:0144234452:Annaba
Saleh:92:013344433:Algiers
And the file work
engineer
baker
letter carrier
saleswoman
paste -d : notebook-address work

Amine:29:0298334432:Batna : engineer

Chapter VII: Unix filters 4 Dr. Saadi Leila


Batna 2 university Mathematics and computer science faculty
Computer science department Computer Engineer 1st year 2023-2024
Introduction of operating systems
Ahmed:13:0466342233:Algiers: baker
Ridha:75:0144234452:Annaba : letter carrier
Saleh:92:013344433:Algiers: saleswoman
4/ Extract common lines from two files:
The comm command is used to extract lines common to two files, i.e. the file notebook-address and
the file notebook-address2
Amine:29:0298334432:Batna
Ahmed:13:0466342233:Algiers
Marwa:59:0144234452:Adrar
Saleh:92:013344433:Oran
comm notebook-address notebook-addres2

We have:
Amine:29:0298334432:Batna
Ahmed:13:0466342233:Algiers

IV/ File comparison:


1/ Compare two files:
The cmp command indicates whether two files are identical by typing :
cmp file1 file2

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

diff notebook-address notebook-addres2

it generates the result:


2c2
< Saleh:92:013344433:Algiers

< Saleh:92:013344433:Oran

Chapter VII: Unix filters 5 Dr. Saadi Leila


Batna 2 university Mathematics and computer science faculty
Computer science department Computer Engineer 1st year 2023-2024
Introduction of operating systems

V/ Grep and Find commands:


1/ Regular expressions:
A "regular expression" is a character string made up of normal characters and special characters known
as "regular expression metacharacters" (not to be confused with the shell metacharacters described
above!). Like all strings, a regular expression can be written between apostrophes, in quotation marks
or on its own, although the latter should be avoided and the use of apostrophes is recommended. The
main metacharacters used in regular expressions are:
▪ . designates any character;
▪ * designates any repetition of the preceding character;
▪ ^ designates the beginning of a line;
▪ $ designates the end of a line;
▪ \ protects the next character, whether normal or metacharacter (regular expressions);
▪ {nbre} designates the repetition of the preceding character, whether normal or metacharacter
(regular expressions);
▪ [...] denotes characters in square brackets, defined by enumeration or interval (^ after the [
denotes the complement of characters in square brackets).
Examples:
▪ '^$' designates an empty line;
▪ '.*' designates any line;
▪ '^début' designates a line beginning with début
▪ "pwd`$" designates a line ending with the name of the working directory;
▪ '\*.\\' designates a line containing a * character, followed by any character, followed by a \
character.
▪ 'A-Z][a-z]{9}' designates a line containing at least ten successive letters, the first of which is
uppercase and the other nine lowercase;
▪ '^[A-Z]' designates a line beginning with an uppercase letter.
2/ Grep command:
The grep filter displays all lines in a file containing a given pattern. This pattern can be defined explicitly
or in the form of a regular expression representing a generic pattern (grep = global regular expression
print).
▪ -i (ignore case) makes the search case insensitive (no distinction between upper and lower
case);
▪ -v (inverse) displays lines not containing the specified pattern;
▪ -l (list) displays the list of files containing the pattern instead of the lines containing it;
▪ -n (number) displays lines containing the pattern preceded by their number;
▪ -c (count) displays file names and, for each file, the number of lines containing the pattern.
Chapter VII: Unix filters 6 Dr. Saadi Leila
Batna 2 university Mathematics and computer science faculty
Computer science department Computer Engineer 1st year 2023-2024
Introduction of operating systems

grep "^ ! " file

displays lines beginning with !

grep "^* ! " file

displays lines whose first non-blank character is !

grep -v "^* ! " file

displays lines that are not comments


3/ find command:
It is used to search and locate the list of files and directories. Find can be used under a variety of
conditions, such as searching for files by permissions, users, groups, file type, date, size and other
possible criteria.
You'll see that you can combine the results of find searches to run other Linux commands, making it a
very powerful tool.
▪ -name searches for file name,
▪ -perm searches for file access rights,
▪ -links finds the number of links in the file,
▪ -user finds the owner of the file,
▪ -group finds the group to which the file belongs,
▪ -type searches for file type (d=directory, c=character, f=normal file),
▪ -size searches for file size in number of blocks (1 block=512bytes),
▪ -atime searches by date of last read access to file,
▪ -mtime searches by file modification date,
▪ -ctime searches by file creation date.
The criteria can be combined with logical operators:
▪ criterion1 criterion2 or criterion1 -a criterion2 corresponds to and logical,
▪ non-logical criterion,
▪ \ (criterion1 -o criterion2) or logical,
The find command must be used with the -print option. Without this option, even if the search is
successful, find displays nothing at standard output (the screen, or more precisely, the shell).
The find command is recursive, i.e. wherever you type, it will scan the directories and sub-directories it
contains, and so on.
To search for a file containing the string toto in the /usr directory,

Chapter VII: Unix filters 7 Dr. Saadi Leila


Batna 2 university Mathematics and computer science faculty
Computer science department Computer Engineer 1st year 2023-2024
Introduction of operating systems

find /usr -name tot -print

To search for all files ending in .c in the /usr directory,

find /usr -name "*.c " -print


find /usr -mtime 3 -print
find /usr -size 2000 -print
find /usr f -user leila -perm 755 -print

VI/ Process management:


1/ Process characteristics:
In the system, several processes can be in progress at any given time. The system must be able to
identify them. To do this, it assigns each of them a PID (Process Identification) number.
A process can itself create another process, so it becomes a parent process, and the new process a child
process. The child process is identified by its PID, and the parent process by its PPID (Parent Process
Identification) number.
All processes are identified by their PID, but also by the PPID of the process that created them, since all
processes were created by another process. But who created the first process? The only process that
doesn't follow this rule is the first process launched on the system, the init process, which has no parent
and whose PID is 1.
2/ Visualize a process:
You can view the processes running on a machine with the command: ps (options). The most interesting
options under HP-UX are -e (display all processes) and -f (detailed display). The ps -ef command
produces something like this:
UID PID PPID C STIME TTY TIME COMMAND
root 1 0 0 Dec 6 ? 1:02 init
...
leila 319 300 0
10:30:30 ? 0:02 /usr/dt/bin/dtsession
Ing 321 319 0 10:30:34 ttyp1 0:02 csh
Ing 324 321 0 10:32:12 ttyp1 0:00 ps -ef
The meaning of the different columns is as follows:
▪ UID name of the user who started the process
▪ PID corresponds to the process number
▪ PPID corresponds to the number of the parent process
▪ C is the priority factor: the higher the value, the higher the priority of the process
▪ STIME corresponds to process start time
Chapter VII: Unix filters 8 Dr. Saadi Leila
Batna 2 university Mathematics and computer science faculty
Computer science department Computer Engineer 1st year 2023-2024
Introduction of operating systems
▪ TTY is the terminal name
▪ TIME is the process processing time
▪ COMMAND is the process name.
In the example given, the ps -ef command has been run from a shell. The first process has a PID of 321,
the second 324. Note that the PPID of the "ps -ef" process is 321, which corresponds to the shell, so
the shell is the parent process of the command just typed.
Some processes are permanent, i.e. they are started at system startup and stopped only when the
system is shut down. These processes are called daemons. The term daemon is an abbreviation of
daemon.
To view the processes of a single user,

ps -u leila

VII/ Process management command:


1/ Change process priority:
Processes run with a certain degree of priority, and a higher-priority process will tend to monopolize
system resources more often, to get to the end of its run as quickly as possible. It's the role of the
operating system to manage these priorities.
The nice command can be used to change the priority of a process.

nice -valeur commande


nice -5 ps -ef
nice -5 cc monprogram.c

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.

Chapter VII: Unix filters 9 Dr. Saadi Leila


Batna 2 university Mathematics and computer science faculty
Computer science department Computer Engineer 1st year 2023-2024
Introduction of operating systems

VIII/ Launching a task process at the bottom :


To run any command, you enter its name after the shell prompt. Until the command is finished, you no
longer have control of the shell, and the prompt is no longer available to you. If the command takes a
long time, your shell won't give you control until it's finished, so you have to launch another shell to
type another command.
There's a simple technique you can use to launch a command from a shell, and then immediately take
over again. Simply add a & at the end of the command. The command will run in the background, and
you'll be taken straight back to the shell prompt.

> ps ef & [321] >

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

to switch netscape to background mode, type CTRL+Z, it will display

311 stopped +

311 being the PID of the netscape process. Then type bg (for background), and you will see
[311]

Chapter VII: Unix filters 10 Dr. Saadi Leila

You might also like