Linux Lecture-6 Finale
Linux Lecture-6 Finale
Linux Lecture-6 Finale
24-12-2020 vag@mkbhavuni.edu.in 2
INTRODUCTION
24-12-2020 vag@mkbhavuni.edu.in 3
1. INTRODUCTION(Contd.)
• Filters are UNIX commands that take input from the standard input
(keyboard), perform some utility operation on the data and then send the
result to the standard output (monitor). For example, the commands cat,
sort, cut, more, pg, tail, wc etc. are all filters. In this chapter we will be
learning some commonly used filters.
• Regular Expressions are sequence of characters that can match a set of
strings in a text. These are used in searching for and replacing strings that
match a particular pattern, in a text. The symbol * that matches zero or
more of the preceding character, [] that matches any one of the enclosed
character, [^] that matches any character that is not enclosed and . That
matches any character except new-line are some of the symbols used in
regular expressions. In this chapter, we will learn regular expressions as a
powerful tool that UNIX provides for data manipulation.
24-12-2020 vag@mkbhavuni.edu.in 4
2. USING wc, head, tail AND cut COMMANDS
• In order to learn and test the commands that we will be discussing in this chapter, let
us use the file called book.lst. The file book.lst contains details of a few books. Each
record holds the book-id, subject , publisher name, date of purchase and author’s
name. the contents of the file are as given in Figure 6.1.
• The command wc is used to count the number of lines, words and characters in a
given text or file.
• When given without any options, wc will give an output as shown in Figure 6.2. The
wc command shows an output that indicates that there are “10” lines, “126” words
and “702” characters in the file book.lst. If multiple filenames are given as input , wc
produces a line for each file, as shown in Figure 6.2. you will note that it also
produces a total count of lines, words, and characters in all the given files.
• One can also use wc to list only line count or only word count or only character count
or a combination of them. Table 6.1 lists the different options that wc provides to do
such a specific count.
24-12-2020 vag@mkbhavuni.edu.in 5
2. USING wc, head, tail AND cut
COMMANDS(Contd.)
24-12-2020 vag@mkbhavuni.edu.in 7
2. USING wc, head, tail AND cut
COMMANDS(Contd.)
24-12-2020 vag@mkbhavuni.edu.in 9
2. USING wc, head, tail AND cut
COMMANDS(Contd.)
24-12-2020 vag@mkbhavuni.edu.in 12
3. TRANSLATING CHARACTERS (tr)
• The filters that we learned so far, were useful for handling either entire lines or entire columns in a file.
However, the tr command can work on individual characters in a file. It takes two expressions as input. It
translates each character of the first expression with its counterpart in the second expression. For example,
suppose we wish to translate all | in out book.lst file with ‘,’ and all / with -, then we have to give the following
tr command:
$ tr ‘|/’ ‘,-’ < book.lst
• Note that the two expressions are given in single quotes. The first character (|) in the first expression is
replaced by the first character (,) in the second expression. And the second character (/) in the first expression
is replaced with the second character (-) in the second expression. The output is given in Figure 6.3.
• By default, tr takes its input from the standard input. Therefore, we had to redirect the input (so that tr takes
input from the file) using the left chevron (<) operator in the command line. The output of the tr command
can be stored in a file using right chevron (>) operator.
• tr can also work on ranges in the expression list. For example, to convert all the lowercase letters in book.lst
file to uppercase, we can give a tr command as follows
$ tr ‘[a-z]’ ‘[A-Z]’ < book.lst
• When used with -d option, tr can delete strings that match a particular expression from the file. For example,
to delete all pipes (|) from the file book.lst, give the following command:
$ tr –d ‘|’ < book.lst
24-12-2020 vag@mkbhavuni.edu.in 13
3. TRANSLATING CHARACTERS (tr)(Contd.)
24-12-2020 vag@mkbhavuni.edu.in 15
4. SPECIFYING FILE AND SEARCHING FOR A
PATTERN (grep)(Contd.)
Option Used to
-c Count the number of occurrences of the pattern in the
file(s)
-n Display line numbers along with the lines containing
the pattern
-v Select lines not containing the pattern
-l Display only the names of the files where a pattern
has been found
-i Ignore case for pattern matching
-h Omit filenames when handling multiple files
Table 6.2 Options of grep
24-12-2020 vag@mkbhavuni.edu.in 16
4. SPECIFYING FILE AND SEARCHING FOR A
PATTERN (grep)(Contd.)
• Suppose we need to find out the number of books published by
“SSN” we can use the -c option and give the command as: (See Figure
6.3)
$ grep –c ‘SSN’ book.lst
• Similarly, try out other grep options as well.
24-12-2020 vag@mkbhavuni.edu.in 17
4. SPECIFYING FILE AND SEARCHING FOR A
PATTERN (grep)(Contd.)
24-12-2020 vag@mkbhavuni.edu.in 19
5. PERFORMING SUBSTITUTION (sed)(Contd.)
24-12-2020 vag@mkbhavuni.edu.in 21
5. PERFORMING SUBSTITUTION (sed)(Contd.)
24-12-2020 vag@mkbhavuni.edu.in 24
6. ENHANCING POWER OF grep AND sed
WITH REGULAR EXPRESSIONS(Contd.)
Symbol Description
* Matches zero or more occurrence of the previous
character
. Matches any single character
[xyz] Matches a single character x, y or z
[c1-c2] Matches a single character which is within the ascii
range represented by characters c1 and c2
[^xyz] Matches a single character which is not an x, y or z
^xyz Matches a pattern xyz at the beginning of line
xyz$ Matches a pattern xyz at the end of line
Table 6.3 Regular expression symbols
24-12-2020 vag@mkbhavuni.edu.in 25
6. ENHANCING POWER OF grep AND sed
WITH REGULAR EXPRESSIONS(Contd.)
• Let us take some more examples. Suppose you want to see the list of the books on “Computer
Organization”, but you do not remember the exact spelling, i.e. is it “Organization” or
“Organisation” in the file. This is solved by regular expression as follows:
$ grep “Organi[sz]ation” book.lst
• The * symbol is used when you want to indicate that the previous character can occur many
times. For example, the pattern “s*” indicates that s can occur many times. That is, it can match
“s”, “ss”, “sss”, “ssssssss” and so on.
• For example, the command:
$ grep “S*N” book.lst
• will match the publishers SN, SSN, SSSN or any number of S’s before N.
• The . symbol matches any single character. This is similar to the ? wild-card. For example, the
command:
$ grep “U…” book.lst
• will match a four-character pattern beginning with U.
24-12-2020 vag@mkbhavuni.edu.in 26
6. ENHANCING POWER OF grep AND sed
WITH REGULAR EXPRESSIONS(Contd.)
• The ^ symbol signifies that the pattern should be matched only at the beginning
of a line. This should not be confused with ^ symbol used in regular expressions.
The $ symbol signifies that the pattern should be matched only at the end of the
line. For example, suppose we need to extract the books where the book-id starts
with 2. the command for this is:
$ grep “^2” book.lst
• To select lines where the book-id does not begin with 2, the command is:
$ grep “^[^2”] book.lst
• These regular expressions can also be used with sed command. For example, in
sed, to replace all occurrences of Organization (or Organisation) with
Architecture.
$ sed –n ‘s/Organi[sz]ation/Architecture/’ book.lst
• Similarly, other regular expressions can also be used with sed.
24-12-2020 vag@mkbhavuni.edu.in 27
7. HOMEWORK
• State True or False:
1. We can replace one character by another for all lines in a file using the tr command.
2. The * symbol in the regular expression h* matches one or more occurrences of
character h.
3. The wc command returns the numbers of characters in a word.
4. The tail command displays the last n lines of a file.
5. The grep command searches and prints lines containing a certain pattern.
24-12-2020 vag@mkbhavuni.edu.in 28
8. Answer key for the last HOMEWORK
1. True
2. False
3. True
4. True
5. False
24-12-2020 vag@mkbhavuni.edu.in 29
TEST – 6 Coming Soon…
24-12-2020 vag@mkbhavuni.edu.in 30
LINKS AND REFERENCES
• Book:
• ‘A’ Level Made Simple – Basics of OS, UNIX and Shell Programming by Vineeta
Pillai and Satish Jain
• Images:
• www.google.com
• www.nasa.gov
24-12-2020 vag@mkbhavuni.edu.in 31
Thank You
“I hear, I forget
I see, I remember
I do, I understand”
-CONFUCIUS
24-12-2020 vag@mkbhavuni.edu.in 32