290 likes | 547 Views
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?
E N D
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? • Statement A is True and Statement B is False • Statement A is False and Statement B is True • Both, Statement A and Statement B, are True • Both, Statement A and Statement B, are False Introduction to Linux
Pre-Assessment Questions (Contd.) • 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? • Statement A is True and Statement B is False • Statement A is False and Statement B is True • Both, Statement A and Statement B, are True • Both, Statement A and Statement B, are False Introduction to Linux
Pre-Assessment Questions (Contd.) • 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? • <ctrl>@ • <ctrl>k • <ctrl>y • <ctrl>d • 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? • Keep the cursor at first line, press yy, keep the cursor at the line number 10, press p • Keep the cursor at first line, press 3yy, keep the cursor at the line number 10, press p • Keep the cursor at first line, press 3yy, keep the cursor at the line number 10, press P • Keep the cursor at first line, press 3yy, keep the cursor at the line number 10, press O Introduction to Linux
Pre-Assessment Questions (Contd.) • 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. Introduction to Linux
Solutions to Pre-Assessment Questions • c. Both, Statement A and Statement B, are True • a.Statement A is True and Statement B is False • b. <ctrl>k • c. Keep the cursor at first line, press 3yy, keep the cursor at the line number 10, press P • d. When the cursor is on the target line, press <Esc> A 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 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. 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. 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 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. 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 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. 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 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] Introduction to Linux
Regular Expression Pattern ‘A’ The character A ‘F’ The character F ‘New' The word New • 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). Introduction to Linux
Option Function –l Displays the number of lines –w Displays the number of words –c Displays the number of characters • 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: Introduction to Linux
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 • 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: 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 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: 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: 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. 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" 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. 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: • Identify the commands to be used. • Execute the identified command in the correct sequence. • Verify whether or not the command has displayed the required information. 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. 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: • Identify the commands to be used. • Execute the identified command in the correct sequence. • Verify whether or not the command has displayed the required information. Introduction to Linux
Summary • In 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. • Thewc filter counts the number of lines, words, and characters in a disk file or in the standard input. 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). Introduction to Linux