Module Linux essentials - Module 8: Pipes, redirection and REGEX

70 141 0
Module Linux essentials - Module 8: Pipes, redirection and REGEX

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Module Linux essentials - Module 8 introduce pipes, redirection and REGEX. After studying this chapter you should be able to understand pipes, redirection and partial POSIX; understand how to search and extrac data from files. Inviting you to refer.

Module Pipes, Redirection and REGEX This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 Exam Objective 3.2 Searching and Extracting Objective Summary Data from Files – – Piping and redirection Partial POSIX This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 Command Line and Redirection This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 Command Line Pipes • The pipe character ( | ) can be used between two commands to send the output of the first as input to the second: – • ls /etc | head The output of ls /etc is sent to head as input This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 Command Line Pipelines • Multiple commands can be combined to form pipelines The order in which commands are added to the pipeline can affect the output: This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 I/O Redirection • Three Input/Output (I/O) streams associated with every command: – – – Standard Input (STDIN) is normally provided by the user via the keyboard Standard Output (STDOUT) is the output produced by the command when operating correctly STDOUT normally appears in the same window as where command executed Standard Error (STERR) is the is the output produced by the command when an error has occurred STDOUT normally appears in the same window as where command executed This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 I/O Redirection Symbols • Summary of redirection possible with the bash shell: – – – – – – – < /path/to/file (Redirect STDIN from file) > /path/to/file (Redirect STDOUT overwriting file) >> /path/to/file (Redirect STDOUT appending file) 2> /path/to/file (Redirect STDERR overwriting file) 2>> /path/to/file (Redirect STDERR appending file) &> /path/to/file (Redirect STDERR and STDOUT overwriting file) &>> /path/to/file (Redirect STDERR and STDOUT appending file) This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 The null device • • • The null device is represented by the /dev/null file (Otherwise known as the “Bit Bucket”) This file is very useful in redirection of input and output This file serves two purposes: – – any output redirected to /dev/null is discarded /dev/null can be used for input to provide a stream of null values This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 STDIN, STDOUT, and STDERR This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 STDIN or • • • • • Standard Input (STDIN) normally is provided by the keyboard but can be redirected with the < symbol STDIN can be read by programs to get data for them to process To signal a program that you wish to stop providing data by the keyboard via STDIN, type CTRL-D The tr command reads its data from STDIN It translates from one set of characters to another If you were the user typing the data to be translated by the tr command, you would type CTRL-D when finished This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 Common grep options Option Purpose color Color the matches found -v Reverse (negate) matches -c Count matches -n Number matching lines -l List matching files -i Match case insensitive -w Match pattern as a word This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 Basic Regular Expressions This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 Basic Regular Expressions • • • Basic Regular Expressions (BRE) are able to be used with the grep command without requiring an option to use them (unlike Extended Regular Expression show later) The simplest regular expressions are just alphabetic or numeric characters that match themselves The backslash \ can be used to escape the meaning of regular expression meta-characters, including the backslash itself This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 BRE: the example • • • The (period) character matches exactly one character The example below shows the grep command matching the 'a' followed by two characters The results show it matched 'abc' This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 BRE: the [ ] example • • • The [ ] (brackets) characters are used to match exactly one character The characters can be listed or given as a range If the first character listed is ^ (caret), then it means not the characters bracketed This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 BRE: the * example • • • The * (asterisk) character will match zero or more of the previous character Matching "a*" is not very useful because it might match zero a's (matches every line) Matching "abcd*" would be more useful, since you would need an "abc" followed by zero or more d's This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 BRE: the ^ example • • The ^ (caret) character, when appearing at the beginning of the pattern, means that pattern must appear at the beginning of the line The ^ not at the beginning of a pattern matches itself This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 BRE: the $ example • • The $ (dollar sign), when appearing at the end of the pattern, means that pattern must appear at the end of the line The $ not at the beginning of a pattern matches itself This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 BRE: Combining ^ and $ • Combining both the ^ and $ characters allows for two special matches: – – '^$' is a blank line match '^pattern$ matches if the whole line contains only the "pattern“ This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 Extended Regular Expressions This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 Extended Regular Expressions • • • The use of Extended Regular Expressions (ERE) requires the -E option when using the grep command Extended Regular Expressions can be combined with Basic Regular Expressions The following are ERE characters: ?, +, and | This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 ERE: the + example • • The + (plus) character will match one or more of the previous character Matching "a+" is useful because it can match one or more a's, ensuring only lines that have at least one “a” are matched This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 ERE: the ? example • • The ? (question mark) character will optionally match one of the previous character The ? character is useful for matching characters that only occasionally appear in a word The following example illustrates this: This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 ERE: the | example • • The | (vertical bar) character will act like an “or” operator between two regular expressions This alternation operator is useful to be able to match multiple patterns: This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 The xargs command • • • The xargs command helps complex piped command sets execute more efficiently It attempts to build the longest command line possible with as many arguments as possible It tries to prevent executing the command each time for every argument This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses ©Copyright Network Development Group 2013 ... example below, the echo Linux command is executed and the output appears on STDOUT Then, the echo Linux > a.txt command redirects the output to the file a.txt Finally, the command cat a.txt sends... example, the tr command translates from lowercase to uppercase after the user typed the command and pressed Enter Then, "alpha" was typed and Enter pressed Finally, the user typed CTRL-D This slide... produced by the command when operating correctly STDOUT normally appears in the same window as where command executed Standard Error (STERR) is the is the output produced by the command when an error

Ngày đăng: 30/01/2020, 03:09

Từ khóa liên quan

Mục lục

  • Slide 1

  • Exam Objective 3.2 Searching and Extracting Data from Files

  • Slide 3

  • Command Line Pipes

  • Command Line Pipelines

  • I/O Redirection

  • I/O Redirection Symbols

  • The null device

  • Slide 9

  • STDIN or 0

  • STDIN from keyboard

  • Redirecting STDIN from file

  • STDOUT or 1

  • Redirecting STDOUT

  • Appending STDOUT redirection

  • STDERR or 2

  • Redirecting STDERR

  • Disposing of STDERR

  • Working with STDERR and STDOUT

  • STDERR and STDOUT Example

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan