Lecture5-stream+redirection
Lecture5-stream+redirection
• List the content of the data directory. You will find data files temp.dat, temp-clean.dat, and
temp-clean1.dat, which are used during this lecture
If you do not have the data files, go on Canvas -> Files -> zip files and download data-temp.zip
• Unzip the file data-temp.zip and a directory called data-temp will be created
You can also unzip by typing at the terminal
unzip data-temp.zip
Slides 1-3 refer to video Lecture5-stdoutput-redirection
1
Standard output
Standard output, sometimes abbreviated stdout, refers to the streams of data (plain text)
that are produced by command line programs.
For example:
ls ~/
cat program_1.bash
grep UT temp.dat
The output of a command can be redirected into a file. For this use >
command (options) argument(s) > file
If file does not exist it will be created, if file already exists it will be overwritten
ls ~/ # output to screen
ls ~/ > my_homedir # output redirected to a file
Since > will overwrite an existing file, you can use >> to append the output of a line of code
into a file
Before you redirect, use the command the usual way (without redirection) and make sure
the command prints the correct output to screen.
Standard error
Standard error (stderr) is another output stream (plain text) typically used by programs to report
status messages such as error messages and warning messages.
For example, if you type data instead of date you will get an error message:
data
ls date2.txt
Also, for example if you try using find commands to search files in the root directory:
You will get messages that permission was denied to search certain folders.
5
Redirecting standard error with 2>
Use 2> to redirect standard error (stderr) into a file:
Example:
data 2> my_error
now look at the content of my_error
By using the pipe operator | the output text (stdout) of one command can be piped
into the input (stdin) of another command
Always add commands one by one. First try out the 1st command, then the 2nd, and then
3rd. This will minimize the chance of errors.
Common error: Repeat filename in 2nd , 3rd , 4th commands and so on…of a pipeline
Example:
Q: Extract all lines containing keyword AZ from the first 8 lines of temp.dat
Which pipeline is incorrect and why?
head -8 temp.dat | grep AZ
head -8 temp.dat | grep AZ temp.dat
8
Search and print all the lines in a file that match multiple patterns.
Command substitution
Example:
grep –c UT temp.dat
x=`grep –c UT temp.dat`
echo $x
Example
mycurrentdir=`pwd`
echo $mycurrentdir
If you change directories, the variable will stay the same.
Slides 10-13 refer to video Lecture5-input redirection and tr command
10
Try to save the output of the command above in a new file temp_lower.dat
Try this
tr A-Z a-z temp-clean.dat # you get an error
tr does not accept file names as arguments
11
tr command - translate characters
sed in pipeline
echo bla | sed 's/bla/tea/'
13
Difference between tr and sed
If you do this example you would think that tr and sed are quite similar:
However, if you do this example, you will realize that they are different: