Difference between revisions of "Tutorial5: Redirection"
(→Additional File Manipulation Commands) |
(→Additional File Manipulation Commands) |
||
Line 86: | Line 86: | ||
There are some additional regular file manipulation commands that you can use with redirection<br>(in addition to the other regular file manipulation commands introduced in week 2). These commands are displayed in the table below: | There are some additional regular file manipulation commands that you can use with redirection<br>(in addition to the other regular file manipulation commands introduced in week 2). These commands are displayed in the table below: | ||
− | <table cellpadding="5"><tr><th style="border-bottom: 1px solid black;text-align:left;">Linux Command</th><th style="border-bottom: 1px solid black;text-align:left;">Purpose</th></tr><tr valign="top"><td>'''cut'''</td><td>'''Used to extract fields and characters from records. The option '''-c''' option is used to cut by a character or a range of characters. The '''-f''' option indicates the field number or field range to display (this may require using the '''-d''' option to indicate the field separator (delimiter).'''<br><br>''Examples:''<br><span style="font-family:courier;font-weight:bold;">cut -f2 filename</span> - extract 2nd field from all records in file, using tab as delimiter (default)<br><span style="font-family:courier;font-weight:bold;">cut -d' ' -f2,5 filename</span> - extract 2nd and 5th field, using space as delimiter<br><span style="font-family:courier;font-weight:bold;">cut -d' ' -f1-3,5 filename</span> - extract 1st through 3rd and 5th fields, using space as delimiter<br><span style="font-family:courier;font-weight:bold;">cut -c3-5 filename</span> - extract 3rd to 5th characters</td></tr><tr valign="top"><td>'''tr'''</td><td>used to translate characters to different characters.<br><br>''Examples:''<br><span style="font-family:courier;font-weight:bold;">tr a A < filename</span> - translate all characters "a" to "A"<br><span style="font-family:courier;font-weight:bold;">tr "[a-z]" "[A-Z]" < filename</span> - translate lowercase "a" through "z" to uppercase<br><span style="font-family:courier;font-weight:bold;">tr "a-z" "A-Z" < filename</span> - translate lowercase "a" through "z" to uppercase, different syntax (non-System V)<br><span style="font-family:courier;font-weight:bold;">tr ':' ' ' < filename</span> - translate all colons to spaces<br><span style="font-family:courier;font-weight:bold;">tr ' ' '\n' < filename</span> - translate all spaces to newline characters<br><span style="font-family:courier;font-weight:bold;">tr 'abc' 'A' < filename</span> - translate 'a', 'b', and 'c' to 'A', the last character in the "to" string repeats<br></td></tr><tr valign="top"><td><span style="font-family:courier;font-weight:bold;">wc</span></td><td> | + | <table cellpadding="5"><tr><th style="border-bottom: 1px solid black;text-align:left;">Linux Command</th><th style="border-bottom: 1px solid black;text-align:left;">Purpose</th></tr><tr valign="top"><td>'''cut'''</td><td>'''Used to extract fields and characters from records. The option '''-c''' option is used to cut by a character or a range of characters. The '''-f''' option indicates the field number or field range to display (this may require using the '''-d''' option to indicate the field separator (delimiter).'''<br><br>''Examples:''<br><span style="font-family:courier;font-weight:bold;">cut -f2 filename</span> - extract 2nd field from all records in file, using tab as delimiter (default)<br><span style="font-family:courier;font-weight:bold;">cut -d' ' -f2,5 filename</span> - extract 2nd and 5th field, using space as delimiter<br><span style="font-family:courier;font-weight:bold;">cut -d' ' -f1-3,5 filename</span> - extract 1st through 3rd and 5th fields, using space as delimiter<br><span style="font-family:courier;font-weight:bold;">cut -c3-5 filename</span> - extract 3rd to 5th characters</td></tr><tr valign="top"><td>'''tr'''</td><td>'''used to translate characters to different characters.'''<br><br>''Examples:''<br><span style="font-family:courier;font-weight:bold;">tr a A < filename</span> - translate all characters "a" to "A"<br><span style="font-family:courier;font-weight:bold;">tr "[a-z]" "[A-Z]" < filename</span> - translate lowercase "a" through "z" to uppercase<br><span style="font-family:courier;font-weight:bold;">tr "a-z" "A-Z" < filename</span> - translate lowercase "a" through "z" to uppercase, different syntax (non-System V)<br><span style="font-family:courier;font-weight:bold;">tr ':' ' ' < filename</span> - translate all colons to spaces<br><span style="font-family:courier;font-weight:bold;">tr ' ' '\n' < filename</span> - translate all spaces to newline characters<br><span style="font-family:courier;font-weight:bold;">tr 'abc' 'A' < filename</span> - translate 'a', 'b', and 'c' to 'A', the last character in the "to" string repeats<br></td></tr><tr valign="top"><td><span style="font-family:courier;font-weight:bold;">wc</span></td><td>'''Displays various counts of the contents of a file.'''<br><br>''Examples:''<br><span style="font-family:courier;font-weight:bold;">wc -l filename</span> - displays number of lines in file<br><span style="font-family:courier;font-weight:bold;">wc -c filename</span> - displays number of characters in file<br><span style="font-family:courier;font-weight:bold;">wc -w filename</span> - displays number of words in fil<br></td></tr></table> |
===Piping (Using Pipes)=== | ===Piping (Using Pipes)=== |
Revision as of 17:54, 23 January 2020
Contents
REDIRECTION: STANDARD INPUT / STANDARD OUTPUT / STANDARD ERROR
Main Objectives of this Practice Tutorial
- x
- x
- x
- x
Tutorial Reference Material
Course Notes |
Linux Command/Shortcut Reference |
YouTube Videos | ||
Course Notes:
|
Redirection
Multiple Commands
|
Redirection Filters
|
Brauer Instructional Videos: |
KEY CONCEPTS
Redirection (Standard Input, Standard Output, Standard Error)
... standard streams are preconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin), standard output (stdout) and standard error (stderr). Originally I/O happened via a physically connected system console (input via keyboard, output via monitor), but standard streams abstract this. When a command is executed via an interactive shell, the streams are typically connected to the text terminal on which the shell is running, but can be changed with redirection or a pipeline.
Reference: https://en.wikipedia.org/wiki/Standard_streams
Standard input (stdin) is a term which describes from where a command receives input.
This would apply only to Unix/Linux commands that accept stdin input (like cat, more, less, sort, grep, head, tail, etc.).
With the examples below, standard input is being sent into the command from the text file.
Examples:
tr 'a-z' 'A-Z' < words.txt
cat < abc.txt
sort < xyz.txt
Standard output (stdout) describes where a command sends it's output.
In the examples below, output from a command is sent to the monitor, unless it is sent to a regular file.
We will discuss another redirection technique called pipelines later.
Examples:
ls -l
ls -l > detailed-listing.txt
ls /bin >> output.txt
Standard Error (stderr) describes where a command sends it's error messages.
In the examples below, error messages from a command are sent to a file or are redirected to a file that acts as a "garbage can".
Examples (remember, Unix/Linux is case sensitive and issuing PWD in caps will generate an error message):
PWD
PWD 2> error-message.txt
PWD 2 >> error-messages.txt
PWD 2> /dev/null
Additional File Manipulation Commands
There are some additional regular file manipulation commands that you can use with redirection
(in addition to the other regular file manipulation commands introduced in week 2). These commands are displayed in the table below:
Linux Command | Purpose |
---|---|
cut | Used to extract fields and characters from records. The option -c option is used to cut by a character or a range of characters. The -f option indicates the field number or field range to display (this may require using the -d option to indicate the field separator (delimiter). Examples: cut -f2 filename - extract 2nd field from all records in file, using tab as delimiter (default) cut -d' ' -f2,5 filename - extract 2nd and 5th field, using space as delimiter cut -d' ' -f1-3,5 filename - extract 1st through 3rd and 5th fields, using space as delimiter cut -c3-5 filename - extract 3rd to 5th characters |
tr | used to translate characters to different characters. Examples: tr a A < filename - translate all characters "a" to "A" tr "[a-z]" "[A-Z]" < filename - translate lowercase "a" through "z" to uppercase tr "a-z" "A-Z" < filename - translate lowercase "a" through "z" to uppercase, different syntax (non-System V) tr ':' ' ' < filename - translate all colons to spaces tr ' ' '\n' < filename - translate all spaces to newline characters tr 'abc' 'A' < filename - translate 'a', 'b', and 'c' to 'A', the last character in the "to" string repeats |
wc | Displays various counts of the contents of a file. Examples: wc -l filename - displays number of lines in file wc -c filename - displays number of characters in file wc -w filename - displays number of words in fil |
Piping (Using Pipes)
x
Multiple Commands Using Semicolon ; / Grouping Commands ( )
x
INVESTIGATION 1: BASICS OF REDIRECTION
In this section, you will learn how to ...
Perform the Following Steps:
- x
In the next investigation, you will ...
INVESTIGATION 2: REDIRECTION USING PIPES
In this section, you will learn how to ...
Perform the Following Steps:
- x
In the next investigation, you will ...
INVESTIGATION 3: MULTIPLE COMMANDS / COMMAND GROUPING
In this section, you will learn how to ...
Perform the Following Steps:
- x
LINUX PRACTICE QUESTIONS
The purpose of this section is to obtain extra practice to help with quizzes, your midterm, and your final exam.
Here is a link to the MS Word Document of ALL of the questions displayed below but with extra room to answer on the document to simulate a quiz:
https://ict.senecacollege.ca/~murray.saul/uli101/uli101_week5_practice.docx
Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc).
Review Questions:
- x
- x
- x
- x
- x
- x
- x
- x
- Create a table listing each Linux command, useful options that were mentioned in the online assignment #1 and command purpose for the following Linux commands: cut , tr , wc