0% found this document useful (0 votes)
95 views6 pages

ITBP 315 - Operating Systems Fundamentals Lab 4 - Text Manipulation Command

The document discusses several common UNIX commands for text manipulation and redirection. It describes using less to view files page by page, head to view the first lines, and tail to view the last lines. Grep is used to search files for keywords. Word count (wc) counts lines, words, and characters. Input and output redirection uses < and > symbols to redirect command input and output to files. The lab exercise has students practice these commands on sample text files to gain experience with text manipulation in UNIX.

Uploaded by

figeket745
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views6 pages

ITBP 315 - Operating Systems Fundamentals Lab 4 - Text Manipulation Command

The document discusses several common UNIX commands for text manipulation and redirection. It describes using less to view files page by page, head to view the first lines, and tail to view the last lines. Grep is used to search files for keywords. Word count (wc) counts lines, words, and characters. Input and output redirection uses < and > symbols to redirect command input and output to files. The lab exercise has students practice these commands on sample text files to gain experience with text manipulation in UNIX.

Uploaded by

figeket745
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ITBP 315 – Operating Systems Fundamentals

Lab 4 – Text Manipulation Command


Objective:
Practice common UNIX commands.

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.

What did you see ?

Now, Try this command:

$ grep fi zmore

What you observe?

The grep command is case sensitive; it distinguishes between Science and


science.

To ignore upper/lower case distinctions, use the -i option, i.e. type

$ 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

$ grep -i ‘uncompressed contents’ zmore

Some of the other options of grep are:

-v display those lines that do NOT match


-n precede each matching line with the line number
-c print only the total count of matched lines

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

$ grep -ivc fi zmore

wc (word count)

A handy little utility is the wc command, short for word count. To do a


word count on zmore, type

$ wc -w zmore

To find out how many lines the file has, type

$ wc -l zmore

To find out how many characters the file has, type


$ wc -m 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.

Now type cat without specifying a file to read

$ 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.

What has happened?

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.

Redirecting the Output

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

$ cat > list1

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

To read the contents of the file, type

$ 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

$ cat >> list1

Then type in the names of more fruit

peach
grape
orange
^D (Control D to stop)

To read the contents of the file, type

$ 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

$ cat list1 list2 > biglist

What this is doing is reading the contents of list1 and list2 in turn, then
outputing the text to the file biglist

To read the contents of the new file, type

$ cat biglist

Redirecting the Input

Task:
We use the < symbol to redirect the input of a command.

The command sort alphabetically or numerically sorts a list. Type

5
$ sort

Then type in the names of some vegetables. Press [Return] after each one.

carrot
beetroot
artichoke
^D (control d to stop)

The output will be

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

$ sort < biglist

and the sorted list will be output to the screen.

To output the sorted list to a file, type,

$ sort < biglist > slist

Use cat to read the contents of the file slist

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

You might also like