ITBP 315 - Operating Systems Fundamentals Lab 4 - Text Manipulation Command
ITBP 315 - Operating Systems Fundamentals Lab 4 - Text Manipulation Command
Procedure:
less
The command ‘less’ writes the contents of a file onto the screen a page at
a time. Type
$ less zmore
Press the [space-bar] if you want to see another page, type [q] if you want
to quit reading. As you can see, less is used in preference to cat for long
files.
Task:
1. Copy the ‘zmore’ from /bin
2. View the contents of the file with the command
less zmore
3. Quit by typing ‘q’
head
The head command writes the first ten lines of a file to the screen.
Task:
1. Practice the ‘head’ command with zmore file
tail
The tail command writes the last ten lines of a file to the screen.
Task:
1. Practice the ‘tail’ command with zmore file
1
Simple searching using less
Task:
Using less, you can search though a text file for a keyword (pattern). For
example, to search through zmore for the word 'fi', type
$ less zmore
then, still in less (i.e. don't press [q] to quit), type a forward slash [/]
followed by the word to search
/fi
As you can see, less finds and highlights the keyword. Type [n] to search
for the next occurrence of the word.
grep
Task:
grep is one of many standard UNIX utilities. It searches files for specified
words or patterns. First clear the screen, then type
$ grep Fi zmore
As you can see, grep has printed out each line containing the word fi.
$ grep fi zmore
$ grep -i fi zmore
2
To search for a phrase or pattern, you must enclose it in single quotes (the
apostrophe symbol). For example, type
Try some of them and see the different results. Don't forget, you can use
more than one option at a time, for example, the number of lines without
the words fi or Fi is
wc (word count)
$ wc -w zmore
$ wc -l zmore
Redirection
Task:
Most processes initiated by UNIX commands write to the standard output
(that is, they write to the terminal screen), and many take their input from
the standard input (that is, they read it from the keyboard). There is also
the standard error, where processes write their error messages, by default,
to the terminal screen.
3
We have already seen one use of the cat command to write the contents of
a file to the screen.
$ cat
Then type a few words on the keyboard and press the [Return] key.
Finally hold the [Ctrl] key down and press [d] (written as ^D for short) to
end the input.
If you run the cat command without specifying a file to read, it reads the
standard input (the keyboard), and on receiving the 'end of file' (^D),
copies it to the standard output (the screen).
In UNIX, we can redirect both the input and the output of commands.
Task:
We use the > symbol to redirect the output of a command. For example,
to create a file called list1 containing a list of fruit, type
Then type in the names of some fruit. Press [Return] after each one.
pear
banana
apple
^D (Control D to stop)
What happens is the cat command reads the standard input (the keyboard)
and the > redirects the output, which normally goes to the screen, into a
file called list1
$ cat list1
4
Exercise
Using the above method, create another file called list2 containing the
following fruit: orange, plum, mango, grapefruit. Read the contents of
list2
The form >> appends standard output to a file. So to add more items to
the file list1, type
peach
grape
orange
^D (Control D to stop)
$ cat list1
You should now have two files. One contains six fruit, the other contains
four fruit. We will now use the cat command to join (concatenate) list1
and list2 into a new file called biglist. Type
What this is doing is reading the contents of list1 and list2 in turn, then
outputing the text to the file biglist
$ cat biglist
Task:
We use the < symbol to redirect the input of a command.
5
$ sort
Then type in the names of some vegetables. Press [Return] after each one.
carrot
beetroot
artichoke
^D (control d to stop)
artichoke
beetroot
carrot
Using < you can redirect the input to come from a file rather than the
keyboard. For example, to sort the list of fruit, type
Summary
more file display a file a page at a time
head file display the first few lines of a file
tail file display the last few lines of a file
grep 'keyword' file search a file for keywords
wc file count number of lines/words/characters in file
Input/Output Redirection
Source: http://www.ee.surrey.ac.uk/Teaching/Unix/index.html