Objectives: Input Output Manipulation
Standard Input:
If a program reads from standard input, it will by default be reading from the keyboard.
Standard Output:
If a program writes to standard output, by default it will be writing to the screen.
Redirection:
Redirection means causing the shell to change what it considers to be standard input or where the standard output is going.
Output Redirection:
To redirect standard output, we’ll use the” >” symbol. Placing “>” after any utility or application that writes to (standard output) will direct its output to the filename following the symbol, except to the standard output device.
Example1:
Ls > filename.
With this command the directory listing of your home directory will not shows on the screen(standard output), but will store in the specified file.
Example2:
Cat filename1 > filename2
With the help of cat command and the output (contents) of filename1 is directed to filename2.
Appending Output Redirection:
You can use output redirection to add new information to the end of an existing file. Similar to when you used the “>” symbol, you tell your shell to send the information somewhere other than standard output. However, when you use “ >>” you’re adding information, rather than replacing it.
Example:
Cat filemane1 >> filename2
The contents of filename1 is appended at the end of filename2.
Input Redirection:
To redirect standard input, this symbol “<” tells the shell that you want a file to be read as input for a command except from the keyboard (Standard input)
Example:
Cat < filename1
Here the input for the cat command to display the output is filename1.
Pipes:
In Linux, pipes connect the standard output of one command to the standard input of another command. Use the vertical bar (|) to pipe the commands. see the result of this piping.
Example1:
Ls | less
In this case the standard output of ls command will be the standard input for the less command, so that you can see your directory listing page by page.
Example2:
Cat filename1 | grep “abc”
In this command filename1 is displayed on the screen and from this, the lines containing word “abc” will be filtered with the help of piping.
Filters:
A filter is a program that manipulates the output of one command before passing it on to another command.
The most common Linux filter is sort, which sorts input lines producing sorted output lines.
Options to sort commands
-b: Ignoring leading blanks in a fields. -c: Check sequence only (no output). -d: Dictionary order (ignore non-alphanumeric). -f: Fold cases (uppercase=lowercase). -n: Numeric sort ;sign and points counts. -o: filename: Output filename -r: Reverse order(descending order) -u: Unique lines only in output |
Example1.
sort filename1.
The contents of filename1 is displayed in sorted order.(By default the sorting order is A/c to ASCII sequence).
Example2.
Sort < filename1.
Filename1 is served as input for the sort command.(In this case the output is on the standard device, screen).
Example3.
Sort > flename1
Given input is served as the sorted output for the filename1.
Some more commands.
Tee:
It will copy input to the standard output screen and to the specified file
Format
Tee [option] filename1 filename2…………
Option
Test with the option -a
-a: append to an existing file
Example1:
Tee filename1 filename2………
The tee command copies input to the screen and to the named file(s) as well.
Example2:
ls –l | tee filename
who:
This command shows all logged on users.
Tasks To Be Perform
Ex1. Use sort to produce a sorted listing of all logged on users (display it on screen).
Also redirect it to a file.
Ex2. Use cat > filename to create a file containing some lines (Use ctrl to quit input), then sort this file to the screen.
Ex3. Use tee command to send a long listing of your home directory to a file. Generete another listing and just appends it to the same file. Now observe its contents.
Ex 4. How can we extract a line containing a specific word from the file?
(Hint: use cat and grep using redirection).
Ex5. sort <filename1> filename2
You can also use redirection in both sides. Here first the filename1is sorted and the output is redirected to filename2
Try this by appending filename2
Ex.6. From what we have learned, how can we print the number of files in our present working directory .
Ex.7. Create a directory called redirtest in your home directory
– From within your home directory execute this command :
ls ./redirtest/ > filelist.txt
Ex.8. Append output to our existing file
– ls ./redirtest/ >> filelist.txt