Pre-Assessment Questions Consider the following statements:

28
Lesson 2B / Slide 1 of 28 ©NIIT Pipes and Filters Introduction to Linux Pre-Assessment Questions 1. Consider the following statements: Statement A: A text editor can be used to create a new file. Statement B: A text editor can be used for searching text in an existing file. Which of the following is correct with respect to the above statements? a. Statement A is True and Statement B is False b. Statement A is False and Statement B is True c. Both, Statement A and Statement B, are True d. Both, Statement A and Statement B, are False

description

Pre-Assessment Questions Consider the following statements: Statement A: A text editor can be used to create a new file. Statement B: A text editor can be used for searching text in an existing file. Which of the following is correct with respect to the above statements? - PowerPoint PPT Presentation

Transcript of Pre-Assessment Questions Consider the following statements:

Lesson 2B / Slide 1 of 28©NIIT

Pipes and Filters

Introduction to Linux

Pre-Assessment Questions 1. Consider the following statements:

• Statement A: A text editor can be used to create a new file.

• Statement B: A text editor can be used for searching text in an existing file.

Which of the following is correct with respect to the above statements? a. Statement A is True and Statement B is Falseb. Statement A is False and Statement B is Truec. Both, Statement A and Statement B, are Trued. Both, Statement A and Statement B, are False

Lesson 2B / Slide 2 of 28©NIIT

Pipes and Filters

Introduction to Linux

Pre-Assessment Questions (Contd.)2. Consider the following statements:

• Statement A: In vi editor, the <Ctrl>d command is used to scroll down half a page at a time.

• Statement B: In vi editor, the <Ctrl>F command is used to scroll down two pages at a time.

Which of the following is correct with respect to the above statements? a. Statement A is True and Statement B is Falseb. Statement A is False and Statement B is Truec. Both, Statement A and Statement B, are Trued. Both, Statement A and Statement B, are False

Lesson 2B / Slide 3 of 28©NIIT

Pipes and Filters

Introduction to Linux

Pre-Assessment Questions (Contd.) 3. Alice is a novice for the emacs editor. She wants to delete the current line in

a file. Which of the following commands should she use? a. <ctrl>@b. <ctrl>kc. <ctrl>yd. <ctrl>d

4. Jane is working on the vi editor. Which of the following command sequence should she follow to copy first 3 lines above the line number 10? a. Keep the cursor at first line, press yy, keep the cursor at the line

number 10, press p

b. Keep the cursor at first line, press 3yy, keep the cursor at the line number 10, press p

c. Keep the cursor at first line, press 3yy, keep the cursor at the line number 10, press P

d. Keep the cursor at first line, press 3yy, keep the cursor at the line number 10, press O

Lesson 2B / Slide 4 of 28©NIIT

Pipes and Filters

Introduction to Linux

Pre-Assessment Questions (Contd.) 5. While writing a C++ program in vi editor, Smith needs to append a keyword

at the end of a line. Which key combination should he use? a.     When the cursor is on the target line, press <Esc> i.b.     When the cursor is on the target line, press <Esc> O.c.     When the cursor is on the target line, press <Esc> o.d. When the cursor is on the target line, press <Esc> A.

Lesson 2B / Slide 5 of 28©NIIT

Pipes and Filters

Introduction to Linux

Solutions to Pre-Assessment Questions 1. c. Both, Statement A and Statement B, are True2. a. Statement A is True and Statement B is False3. b. <ctrl>k4. c. Keep the cursor at first line, press 3yy, keep the cursor at the line number

10, press P5. d. When the cursor is on the target line, press <Esc> A

Lesson 2B / Slide 6 of 28©NIIT

Pipes and Filters

Introduction to Linux

Objectives

In this lesson, you will learn to:

• Identify standard input, output, and error files

• Give input to a command from a file

• Redirect output of a command or the error to a disk file

• Use grep, wc, cut, tr, and sort filters

• Use pipes to combine commands and filters

• Use tee command to display output on the standard output and store it in a file

Lesson 2B / Slide 7 of 28©NIIT

Pipes and Filters

Introduction to Linux

Standard Files• A computer system consists of mainly three parts:

• The input devices, such as keyboard and mouse, are used to accept data from the user.

• The output devices, such as monitor and printer, are used to display/print information or any error messages that might occur.

• The processing device, such as CPU, processes the user input according to the specified instructions, to generate the desired output.

Lesson 2B / Slide 8 of 28©NIIT

Pipes and Filters

Introduction to Linux

Standard Files (Contd.)• Standard input file accepts the input from the user for various commands that

need input from the user.

• The keyboard is default source of input and is referred to as the standard input file.

• Standard output file displays the output of a command to the user.

• The monitor is default destination of output and is referred to as the standard output file.

• Standard error file displays the error messages which are generated during a command execution.

• The error messages are displayed on the monitor by default and therefore monitor acts as standard error file.

Lesson 2B / Slide 9 of 28©NIIT

Pipes and Filters

Introduction to Linux

Redirection• Redirection:

• Changes the assignments for the standard input, standard output, and the standard error

• Enables you to take input to a command from a file other than the keyboard

• Enables you to write the output of a command or the error to a disk file or printed, instead of the monitor

• Is of the following three types:

• Input redirection

• Output redirection

• Error redirection

Lesson 2B / Slide 10 of 28©NIIT

Pipes and Filters

Introduction to Linux

Input Redirection• The following example illustrates the usage of input redirection:

$ cat < test1 • The less than symbol, <, implies input redirection from the file, test1.

• The above command can also be written using the file descriptor as: $ cat 0< test1

• In the preceding code, 0 indicates input redirection.

Lesson 2B / Slide 11 of 28©NIIT

Pipes and Filters

Introduction to Linux

Output Redirection

• The following example illustrates the usage of output redirection:$ cat test1 > out_test

• The greater than symbol, >, implies redirection of output to the file, out_test.

• In output redirection, the file to which the output is redirected is first created on the disk as an empty file and then the output is sent to this file.

• If the file already exists, its content is deleted before the output is written to it.

• If you want to append the output to the file, out_test, the command is:$ cat test1 >> out_test

• The previous commands can also be written using the file descriptor as:$ cat test1 1> out_test $ cat test1 1>>out_test

Lesson 2B / Slide 12 of 28©NIIT

Pipes and Filters

Introduction to Linux

Error Redirection• Error redirection first creates the file to which the error messages are

redirected and then writes the error output to the file.

• The following example illustrates the usage of error redirection:$ cat test_2 2> error-mesg

• In the preceding command, the file, test_2, does not exist in the current directory.

• When a user tries to execute the preceding command, Linux will generate an error message because the execution is unsuccessful. This message, which would otherwise be displayed on the monitor (the standard error file), is written to the file, error‑mesg.

Lesson 2B / Slide 13 of 28©NIIT

Pipes and Filters

Introduction to Linux

Filters • A filter is a program that takes its input from the standard input file, processes

(or filters) it, and sends its output to the standard output file.

• Some examples of filters are:• grep

• cat

• wc

• tr

• cut

Lesson 2B / Slide 14 of 28©NIIT

Pipes and Filters

Introduction to Linux

The grep Filter• The grep filter:

• Stands for Global Regular Expression Print.

• Searches a file for a particular pattern of characters, and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression.

• Cannot be used without specifying a regular expression.

• Has the following syntax:grep [options] pattern [filename]

Lesson 2B / Slide 15 of 28©NIIT

Pipes and Filters

Introduction to Linux

Specifying Regular Expressions

• Regular expressions can be used to specify simple patterns of characters to highly complex ones.

• The following table lists some simple patterns:

• You can also specify complex regular expressions, such as, [], [] with hyphen, ^, ^ within [], $, . (dot), and \ (backslash).

Regular Expression Pattern

‘A’ The character A

‘F’ The character F

‘New' The word New

Lesson 2B / Slide 16 of 28©NIIT

Pipes and Filters

Introduction to Linux

The wc Filter

• The wc filter:

• Is used to count the number of lines, words, and characters in a disk file or in the standard input

• Has the following syntax: wc [option] [filename]

• The following table lists the options of the wc filter:

Option Function

–l Displays the number of lines

–w Displays the number of words

–c Displays the number of characters

Lesson 2B / Slide 17 of 28©NIIT

Pipes and Filters

Introduction to Linux

The cut Filter

• The cut filter:

• Is useful when specific columns from the output of certain commands (such as ls, who) need to be extracted

• Has the following syntax:cut [options] [filename]

• The following table lists the options of the cut filter:

Option Function

-f<column_number(s)> Displays the specified columns

-c<character_number(s)> Displays the specified characters

-d<column_delimiter> Specifies the column delimiter

Lesson 2B / Slide 18 of 28©NIIT

Pipes and Filters

Introduction to Linux

The tr Filter

• The tr filter:

• Can be used to translate one set of characters to another

• Can also be used to squeeze repeated occurrences of a character into one

• Uses the -s option to squeeze several occurrences of a character into one character. For example,

who > temporary $ tr -s " " < temporary root tty1 Sep 28 17:02

steve pts/4 Sep 28 19:36 (172.17.55.167)

• Enables you to perform case-conversion

Lesson 2B / Slide 19 of 28©NIIT

Pipes and Filters

Introduction to Linux

The sort Filter

• The sort filter arranges each line of the standard input in ascending order.

• The following table lists the options of the sort filter:

Option Function

-r Sorts the input in reverse order

-f Arranges the input to the sort filter consisting of digits, alphabets, and other characters in the ASCII sequence

-n Arranges the numbers also in ASCII sequence

<filename> Sorts the content of the specified file

+pos1 –pos2 Sorts a file in the order of a specific column

-t Creates a file with a different column separator

-o Saves the output of the sort filter to a disk file

Lesson 2B / Slide 20 of 28©NIIT

Pipes and Filters

Introduction to Linux

Pipes

• Pipes enable you to combine multiple commands and execute them as a single command. For example,

$ ls | more • Pipes are represented using the vertical bar (|), which indicates to the

shell that the output of the command before the ‘|’ is to be sent as input to the command after the ‘|’.

The following figure shows the working of a pipe:

Lesson 2B / Slide 21 of 28©NIIT

Pipes and Filters

Introduction to Linux

The tee Command

• The tee command:

• Pipes the standard output of a command to another command, and also save it on disk for later use or displays on the standard output

• Creates the file if the file where data is to be written does not exist

• Overwrites the content of the files if the file already exists

• An example of the tee command is: $ cat temp | tee temp1 temp2

• The –a (append) option can be used to append the new content to an existing file.

Lesson 2B / Slide 22 of 28©NIIT

Pipes and Filters

Introduction to Linux

Demonstration-Sorting and Redirecting Output

• Problem Statement

• The details of various customers who have made calls to Deez Telecommunications are stored in the file, Customer. Sample data from the file is as follows:

"10:30","000004","Angela","Smith","16223 Radiance Court","Kansas City","Kansas"

"10:36","000002","Barbara","Johnson","227 Beach Ave.","Alexandria","Virginia"

"10:48","000003","Betty","Williams","1 Tread Road","Dublin","Georgia"

"11:15","000001","Carol","Jones","765 Furling Rd Apt","Norton","Kansas"

"11:23","000005","Catherine","Roberts","5508 Aquiline Court","Norton","Kansas"

"11:44","000038","Joyce","Phillips","535 Darwin Avenue ","Columbus","Georgia"

"12:20","000026","Linda","Lewis","1524 Patagonia Lane ","Columbus","Georgia"

"14:00","000022","Ruth","Green","459 Ridge Road ","Middle Town","New Jersy"

Lesson 2B / Slide 23 of 28©NIIT

Pipes and Filters

Introduction to Linux

Demonstration-Sorting and Redirecting Output (Contd.)

• Problem Statement (Contd.)

• In the file, the field delimiter is ','. The various fields are CustomerCode, FirstName, LastName, Address, City, and State. Angela has been assigned the task to sort the file and display only the CustomerCode, FirstName and LastName of the customers living in Georgia. The output should be displayed in the order of the CustomerCode.

Lesson 2B / Slide 24 of 28©NIIT

Pipes and Filters

Introduction to Linux

Demonstration-Sorting and Redirecting Output (Contd.)

• Solution

• To sort the file and redirect the output as required in the problem statement, Angela needs to perform the following tasks: 1. Identify the commands to be used. 2. Execute the identified command in the correct sequence. 3. Verify whether or not the command has displayed the

required information.

Lesson 2B / Slide 25 of 28©NIIT

Pipes and Filters

Introduction to Linux

Demonstration-Exporting Content of a File

• Problem Statement

• Export the content of the file Customer_Details present in the ~/CustomerCalls directory into a file, New_Details. The exported data needs to be in uppercase. You also need to view the data in the new format on the screen, one screen-full at a time.

Lesson 2B / Slide 26 of 28©NIIT

Pipes and Filters

Introduction to Linux

Demonstration-Exporting Content of a File (Contd.)

• Solution

• To export the data of Customer_Details file to a database, you need to do the following tasks: 1. Identify the commands to be used. 2. Execute the identified command in the correct sequence. 3. Verify whether or not the command has displayed the

required information.

Lesson 2B / Slide 27 of 28©NIIT

Pipes and Filters

Introduction to Linux

SummaryIn this lesson, you learned:

• In Linux, the keyboard is referred to as the standard input file and the monitor is referred to as the standard output and the standard error file.

• The input, output, and errors can be redirected to a file other than the standard files using the file descriptors along with the redirection symbols, > and < .

• The output and error(s) can be redirected, in the append mode, to add the redirected output or error to an existing file using the >> symbol.

• A filter is a command or a user program that takes input from the standard input file, processes the data and gives output on the standard output file. Examples of filters are: grep, wc, tr, and cut.

• The grep filter searches the standard input or a file for a particular pattern of characters, and displays all lines that contain that pattern.

• The wc filter counts the number of lines, words, and characters in a disk file or in the standard input.

Lesson 2B / Slide 28 of 28©NIIT

Pipes and Filters

Introduction to Linux

Summary (Contd.)• The cut filter is used when specific columns from the output of certain

commands (or files) need to be extracted.

• The tr filter is used to translate one set of characters to another.

• The sort filter arranges each line of the standard input or a file in a particular order.

• A pipe is a feature through which the standard output of a command or user program can be sent as the standard input to another command or user program.

• The tee command takes standard input and writes to standard output and to file(s).