intro unix/linux 06

25
Lesson 6-Using Utilities to Accomplish Complex Tasks

description

using utilities to accomplish complex tasks

Transcript of intro unix/linux 06

Page 1: intro unix/linux 06

Lesson 6-Using Utilities to Accomplish Complex Tasks

Page 2: intro unix/linux 06

Overview

Creating and executing a script to list user information.

Listing directories and files separately.

Identifying changes made to files in a directory.

Creating a complex script.

Page 3: intro unix/linux 06

Creating and Executing a Script to List User Information

Creating a script.

Executing a script.

Page 4: intro unix/linux 06

Creating a Script

A series of commands can be executed

repeatedly/automatically by placing them in a script file.

Placing commands in scripts helps avoid errors and save

time.

The vi editor can be used to create a file.

The ":wq" commands are used to write to a file and return

to the shell.

Page 5: intro unix/linux 06

Executing a Script

The commands in a file can be executed in two ways:

Instructions can be given to the current shell to read the file

(source) and execute all the commands.

The script file can be made executable, and a child shell can

be started to read the script file and execute the commands.

Page 6: intro unix/linux 06

Executing a Script

The command used to make the shell executable is “chmod

+x filename”.

By default, the output of the script is redirected to the

screen.

The output of the script can be redirected to a file.

Page 7: intro unix/linux 06

Listing Directories and Files Separately

Selecting only directories / files:

The “ls –F | grep /” command is used to select only lines that

contain directory names.

The “ls –F | grep / | column” command can be used to put the

output into columns.

The “ls –F | grep –v / | column” command is used to select only

files.

Page 8: intro unix/linux 06

Listing Directories and Files Separately

Selecting Directories Using Multiple Utilities

Page 9: intro unix/linux 06

Listing Directories and Files Separately

Interpretation by grep Using –v option

Page 10: intro unix/linux 06

Listing Directories and Files Separately

Adding comments to scripts:

The # (pound) sign is used to add comments to scripts.

Ensure that the # sign is placed at the beginning of each line.

The pound (#) sign cannot be used inside a long command

line.

Page 11: intro unix/linux 06

Identifying Changes Made to Files in a Directory

Collecting data about files in a directory:

The “ls –l directory name” command can be used to provide

information about the files in a particular directory.

The output of the code can also be redirected to another file by

the command "ls -l directoryname > filename”.

The information includes the current status of each file, such

as permissions, date of modification, owner, etc.

Page 12: intro unix/linux 06

Identifying Changes Made to Files in a Directory

Comparing file information:

The “comm” command is used to identify the common aspects

between two files.

It requires two arguments – the names of the two the files that

are to be compared.

The command gives three columns of output – lines unique in

the first file, lines unique in the second file, and lines in

common.

Page 13: intro unix/linux 06

Identifying Changes Made to Files in a Directory

Comparing file information:

The “diff” command is used to identify the differences between

two files.

It requires two arguments – the names of the two files to be

compared.

The command gives three columns of output – lines unique in

the first file, lines unique in the second file, and lines common

to the two files.

Page 14: intro unix/linux 06

Creating a Complex Script

Determining the number of unique words in a file.

Removing punctuation.

Converting characters to lowercase.

Putting each word on a line.

Removing blank lines.

Sorting the lines.

Page 15: intro unix/linux 06

Determining the Number of Unique Words in a File

The “uniq” utility is used to:

Provide a list of unique words in a file.

The number of times that each word is used.

The number of unique words.

Page 16: intro unix/linux 06

Determining the Number of Unique Words in a File

The uniq utility outputs both, unique lines as well as single

copies of any lines that are duplicate and adjacent.

The utility deletes identical lines.

The uniq utility deletes only those lines that are identical

and adjacent.

Page 17: intro unix/linux 06

Determining the Number of Unique Words in a File

The following things must be ensured for the uniq utility to

work properly:

Punctuation and blank lines must be removed.

Differences in case for the same word must be reconciled.

The words must be one word to a line in a sorted order.

Page 18: intro unix/linux 06

Removing Punctuation

Shell and Utility Interpretation

Page 19: intro unix/linux 06

Converting Characters to Lowercase

Shell and tr Interpretation

Page 20: intro unix/linux 06

Putting Each Word on a Line

To remove duplicate words using the uniq utility, the data

must be modified to ensure that each word is on a line by

itself.

The new line character “\n” is used to separate lines in a

file.

The ASCII character code 012 can also be used to separate

lines in a file.

Page 21: intro unix/linux 06

Putting Each Word on a Line

The “man ascii” command is used to examine the ASCII

characters and their associated codes.

TAB characters can also be replaced with new line

characters.

Page 22: intro unix/linux 06

Putting Each Word on a Line

Using tr Utility to Replace Characters

Page 23: intro unix/linux 06

Removing Blank Lines

A View from the Shell

Page 24: intro unix/linux 06

Sorting the Lines

The “sort” command can be used to sort the output such

that all lines containing the same word are on adjacent

lines.

The “uniq –c” command can be used to remove all

duplicate lines.

The “sort –rn” command can be used to list the most

frequently used words first.

Page 25: intro unix/linux 06

Summary

A series of commands placed in a script file can be

repeatedly executed, avoiding errors and saving time.

The ls utility provides a list of all the files and subdirectories

in the current directory.

The # sign is used to add comments to script files.

The pipeline feature of UNIX is very useful and is central to

manipulating data effectively with UNIX utilities.