Changes

Jump to: navigation, search

Tutorial5: Redirection

92 bytes added, 13:54, 31 August 2020
KEY CONCEPTS
===Redirection (Standard Input, Standard Output, Standard Error)===
===Additional File Manipulation Commands=== Before proceeding, 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" width="55%"><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) which is tab by default).<br><br>''Examples:''<br><span style="font-family:courier;font-weight:bold;">cut -f2 filename</span> - extract 2nd field from all records in file<br><span style="font-family:courier;font-weight:bold;">cut -d' ' -f2,5 filename</span> - extract 2nd and 5th field<br><span style="font-family:courier;font-weight:bold;">cut -d' ' -f1-3,5 filename</span> - extract 1st to 3rd and 5th fields<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-z]" "[A-Z]" < filename</span> - translate lower to upper case<br><span style="font-family:courier;font-weight:bold;">tr "a-z" "A-Z" < filename</span> - same as above (non-System V servers)<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><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><br><br>  ''... '''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
[[Image:stdin-symbol.png|thumb|right|250px|The '''standard input''' ('''stdin''') symbol that describes where a Unix/Linux command receives '''input''']]
'''Standard input''' ('''stdin''') is a term which describes from where a command receives '''input'''.<br>
This would apply only to Unix/Linux commands that accept stdin input (like ''cat'', ''more'', ''less'', ''sort'', ''grep'', ''head'', ''tail'', etc.).<br>
 
''Examples:''
<table align="right"><tr><td>[[Image:stdout-symbol-1.png|thumb|right|250px|The '''standard input''' ('''stdin''') symbol with one greater than sign '''overwrites ''' existing file content with command output]]</td><td>[[Image:stdout-symbol-2.png|thumb|right|250px|The '''standard input''' ('''stdin''') symbol with two greater than signs '''add ''' command's output to '''bottom ''' of existing file's contents.]]</td></tr></table>'''Standard output''' ('''stdout''') describes where a command sends itits '''s output'''.<br>In the examples below, output from a command is sent to the '''monitor''', unless it is sent to a '''text file'''.
<table align="right"><tr><td>[[Image:stderr-symbol-1.png|thumb|right|250px|The '''standard error''' ('''sterr''') symbol with one greater than sign '''overwrites ''' existing file content with command's '''error message'''.]]</td><td>[[Image:stderr-symbol-2.png|thumb|right|250px|The '''standard error''' ('''stderr''') symbol with two greater than signs '''add ''' command's error message to '''bottom ''' of existing file's contents.]]</td></tr></table>
'''Standard Error''' ('''stderr''') describes where a command sends it's error messages. In the examples below we issue the pwd in capitals on purpose to generate an error message, which can be redirected to a '''text file'''.
<span style="font-family:courier;font-weight:bold">PWD<br>PWD 2> error-message.txt<br>PWD 2 >> error-messages.txt<br>PWD 2> /dev/null</span>
The '''/dev/null''' file (sometimes called the '''bit bucket''' or '''black hole''') is a special system file<br>that discards '''discard''' all data written into it. This is useful to discard unwanted command output.
<br><br>
[[Image:Here Document.png|thumb|right|175px|The '''Here Document''' allows a user to redirect stdin from <u>within </u> the command itself.]]
In Linux, the '''Here Document''' allows a user to redirect stdin from within the command itself.
<span style="font-family:courier;font-weight:bold">cat <<+<br>Line 1<br>Line 2<br>Line 3<br>+<br><br>
===Additional File Manipulation Commands===
 
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" width="55%"><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) which is tab by default.<br><br>''Examples:''<br><span style="font-family:courier;font-weight:bold;">cut -f2 filename</span> - extract 2nd field from all records in file<br><span style="font-family:courier;font-weight:bold;">cut -d' ' -f2,5 filename</span> - extract 2nd and 5th field<br><span style="font-family:courier;font-weight:bold;">cut -d' ' -f1-3,5 filename</span> - extract 1st to 3rd and 5th fields<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-z]" "[A-Z]" < filename</span> - translate lower to upper case<br><span style="font-family:courier;font-weight:bold;">tr "a-z" "A-Z" < filename</span> - same as above (non-System V servers)<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><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>
<br><br>
===Piping (Using Pipes)===
13,420
edits

Navigation menu