Redirecting And Piping Output

Written by Chris Gregg, with modifications by Nick Troccoli

Click here for a walkthrough video.

Redirecting Output

Sometimes you want the output of a command or a program to go into a file, or you want to read input from a file as if you typed it in yourself. We use the angle brackets, < and > to redirect input and output respectively.

For example, if you want to send the output of a Hello World program to a file, you could do it like this:

$ ./hello > outputFile.txt
$ cat outputFile.txt
Hello, World!
$

To append to the end of an existing file, use >>:

$ ./hello >> outputFile.txt
$ cat outputFile.txt
Some text that was here already
Hello, World!
$

If you want to read input from a file, you use the < character:

$ ./addTwoNumbers
This program will add two doubles!
Please enter a double: 40.5
Please enter a double: 1.5
The sum is: 42.000000
$ cat twoNumbers.txt
40.5
1.5
cgregg@myth3:~/cs107/addTwoNumbers$ ./addTwoNumbers < twoNumbers.txt
This program will add two doubles!
Please enter a double: Please enter a double: The sum is: 42.000000

Note that the input isn't actually shown on the screen (which may screw up your formatting). In CS107, we generally avoid reading input from a user, and if we do need to read input, we explicity use command-line parameters for our programs.

Piping Output

The original unix methodology for utility files was "make small programs that only do one thing, and do them well." For example, the cat command simply takes a list of files, and prints them one after another (we normally just cat one file at at time).

The "killer" idea for unix was that, once you have small programs that each do one thing, you can then chain the output of one program into the input of another program, called piping. The result of this is the same as writing the output if the first command to some temporary file, and then reading the input into the second command. To pipe output, we use the | ("pipe") character. For example, a nice command is the cut command, which takes input one line at a time and breaks it at a delimiting character, then printing one or more of the sub-parts, called fields. For instance, if you wanted to just print the name of files without their extensions, you could cut on the . character, and then just print the first field. E.g.:

$ cut -d. -f1
hello.c
hello
outputFile.txt
outputFile
readme.txt
readme
(type ctrl-d)
$

If you want to do this for all files in a directory, you could pipe the ls command to the cut command:

$ ls
hello  hello.c  hello.c~  innerFolder  outputFile.txt  readme.txt  samples
$ ls | cut -d. -f1
hello
hello
hello
innerFolder
outputFile
readme
samples
$

You can pipe an unlimited number of commands to other commands, which is very helpful when you want to manipulate text. E.g.,

$ # Downloading Taylor Swift, Look What You Made Me Do 
$ wget http://www.paroles-musique.com/paroles-Taylor_Swift-Look_What_You_Make_Me_Do-lyrics,p192724 -O look.html
$ # Now process it to strip just the lyrics
$ sed -n '/id="lyrics"/,$p' look.html | tail -n  +2 | sed "s/<br>//g" | sed "s/<\/div>//g" | sed '/class="cont_act"/q' | head -n-1
I don't like your little games
Don't like your tilted stage
The role you made me play
Of the fool, no, I don't like you
I don't like your perfect crime
How you laugh when you lie
You said the gun was mine
Isn't cool, no, I don't like you (oh!)

But I got smarter, I got harder in the nick of time
Honey, I rose up from the dead, I do it all the time
I've got a list of names and yours is in red, underlined
I check it once, then I check it twice, oh!

Ooh, look what you made me do
Look what you made me do
Look what you just made me do
Look what you just made me
Ooh, look what you made me do
Look what you made me do
Look what you just made me do
Look what you just made me do

I don't like your kingdom keys
They once belonged to me
You ask me for a place to sleep
Locked me out and threw a feast (what?)
The world moves on, another day, another drama, drama
But not for me, not for me, all I think about is karma
And then the world moves on, but one thing's for sure
Maybe I got mine, but you'll all get yours

But I got smarter, I got harder in the nick of time
Honey, I rose up from the dead, I do it all the time
I've got a list of names and yours is in red, underlined
I check it once, then I check it twice, oh!

Ooh, look what you made me do
Look what you made me do
Look what you just made me do
Look what you just made me
Ooh, look what you made me do
Look what you made me do
Look what you just made me do
Look what you just made me do

I don't trust nobody and nobody trusts me
I'll be the actress starring in your bad dreams
I don't trust nobody and nobody trusts me
I'll be the actress starring in your bad dreams
I don't trust nobody and nobody trusts me
I'll be the actress starring in your bad dreams
I don't trust nobody and nobody trusts me
I'll be the actress starring in your bad dreams
(Look what you made me do)
(Look what you made me do)
"I'm sorry, the old Taylor can't come to the phone right
now."
"Why?"
"Oh, 'cause she's dead!" (ohh!)

Ooh, look what you made me do
Look what you made me do
Look what you just made me do
Look what you just made me
Ooh, look what you made me do
Look what you made me do
Look what you just made me do
Look what you just made me do
$