Home About BC DR HA Support Training Download
You are here: Home/ Training/ Unix-Bourne-Shell/ Please Login or Register

-
Current Location
-

js
  Training
    Unix-Bourne-Shell
-
AIX Admin Methodology
Global Consolidation Project
All AIX admins should join
www.aixexpert.com


Join our LinkedIn Group
AIX Advanced Technical Experts
Contract Opportunities

www.LinkedIn.com

-
digg Digg this page
del.icio.us Post to del.icio.us
Slashdot Slashdot it!


LPAR Leasing
Lease an AIX / i5 LPAR
Reduce your costs

www.mtxia.com

Server Leasing
Lease a Server off-site
Reduce your costs

www.mtxia.com

Data Center Automation
Business Continuity and DR
Virtualization/Consolidation

www.mtxia.com

HMC Service
Hardware Management Console
Manage Remote AIX / i5 LPARs

www.siteox.com

Business Web Site Hosting
$3.99 / month includes Tools,
Shopping Cart, Site Builder

www.siteox.com

Disaster Recovery
Small Business Oriented
Off-Site Facilities

www.mtxia.com

IBM pSeries / iSeries
Reduce your Costs
Off-Site Server Hosting

www.mtxia.com

-

Bourne Shell: Chapter 6c

-

Please contact or Mt Xia for assistance with all your shell programming needs.


  • 6. UNIX TOOLS. Go to the bottom, first,previous,next, last section, table of contents.

                                                                     
    

    6.7 sort: Sort a File

    The sort utility sorts line of all the named files together and writes the result to standard output. The standard input is used if - is used as a file name or no input files are specified. Comparisons are based one or more sort keys extracted from each line of input. There is only one key by default, that's the entire line, and ordering is lexicographic by bytes in machine collating sequence.
                                                                    
        Command format: sort [-cmu][-ooutput][-ykmem][-zrecsz]     
                             [-dfiMnr][-btx][+pos][-pos2][files]   
                                                                   
         See on-line manual for options etc.                       
    
    The easiest way to use sort is to add it at the end af a pipeline. What does the following command line accomplish: Sample session: $grep '[45]' phone.lis | sed 's//73/' | sort Benson, Sam 734-5587 StClair, Fred 734-6122 Stair, Rich 735-5972 $ The grep command will select only those records that have a 4 of a 5 in the phone number, those records are then sent to sed which will add "73" just after the tab character, then the records are sent to sort and put in alphabetical order. Notice that there is a problem here, should StClair come before Stair in an alphabetical listing? The answer is NO. Why did this happen? It occurred because of the collating sequence for the default sort. This can be fixed by specifying some options on the call to the sort utility. Here are some options for sort. Let's see if we can determine how to remedy the problem discovered in the default sort. Sort options: -f Fold lower case into upper case -r Reverse the sort from highest to lowest -b Ignore leading blank spaces -d Dictionary sort - ignore non alphanumeric characters -m Merge two sorted files together -n Sort the list as numbers not digit characters Notice the -f options folds lower case into upper case. This option will make the sort for our problem work correctly. Here's the sample. Sample session: $grep '[45]' phone.lis|sed 's//73/'|sort -f Benson, Sam 734-5587 Stair, Rich 735-5972 StClair, Fred 734-6122 $ The sort can also be directed to use only a portion of the line as a sorting key versus the entire line. The utility will automatically break each line into fields at whitespace delimiters. You can use a character other than whitespace by using the -t option. The fields are set up like this: 0 1 2 /----|/---|/-------------| Adams, Fran 2-3876 In order to sort by the second field here is the sort command to enter. Sample session: $sort +1 phone.lis Adams, Fran 2-3876 StClair, Fred 4-6122 Smith, Joan 7-7989 Stair, Rich 5-5972 Benson, Sam 4-5587 Jones, Ted 1-3745 $ Here's a sample of a sort on the 3rd field. Sample session: $sort +2 phone.lis Jones, Ted 1-3745 Adams, Fran 2-3876 Benson, Sam 4-5587 StClair, Fred 4-6122 Stair, Rich 5-5972 Smith, Joan 7-7989 $ A sort can also be performed by a character position within a field. Here's the sample. Sample session: $sort +2.4 phone.lis StClair, Fred 4-6122 Benson, Sam 4-5587 Jones, Ted 1-3745 Adams, Fran 2-3876 Stair, Rich 5-5972 Smith, Joan 7-7989 $ Note: The first character of a field is the delimiter for that field

    6.8 What Other Useful UNIX Tools are Available

    As stated from the beginning, one of the maxims used to develop UNIX was that tools would continue to be developed. Here is a list of tools that might be of interest to you.
      
        UNIX Tool         Description                              
                                                                   
        comm           Compares two sorted file and reports        
                       differences                                 
                                                                   
        cut            Select columns or fields from lines in a    
                       field (System V only)                       
                                                                   
        diff           Report the differences between two files    
                                                                   
        join           Join lines in two files that contain a      
                       common field                                
                                                                   
        pg             Show files (or standard input) on a terminal
                       a screen at a time                          
                                                                   
        od             Print the numeric equivalent of a file's    
                       content                                     
                                                                  
        tail           List end of files (or standard input) on    
                       standard output                             
                                                                   
        tee            Sends standard input to two different places
                                                                   
        tr             Transforms all occurrences of one character 
                       into another                                
                                                                   
                                                                   
        wc             Count the characters, words, and lines in a 
                       file                                        
     

    6.9 Archiver and Library Maintainer

    This command will maintain groups of files combined into a single archive file. The main use of ar is to create and update library files as used by the link editor. It can also be used for any other similar purpose. The file header consists of printable ASCII characters. If the archive consists of printable characters, then the entire archive is also printable. The format for the ar command is as follows:
     Command Format:  ar key [posname] afile [name]...             
                                                                   
     Unlike command options, the command key is required.  The key,
     usually a - sign, is formed with one of the following letters 
     drqtpmx.  Arguments to the key are made from one or more of   
     the following set, vuaibcis.  See Appendix I for a complete   
     list of command keys.                                         
                                                                   
     posname is an archive member name used as a reference for     
     positioning other files in the archive.                      
                                                                   
     afile is the name of the archive.                             
                                                                  
     name[s] are the constituent files in the archive.             
    
    To illustrate how to create and use an archive file, we will use the "C" program called main.c and the two functions, funct1.c and funct2.c. First, create the object files that we intend to put into the archive file. Sample Session: $cc -c main.c funct1.c funct2.c main.c: funct1.c: funct2.c: $ls -C *.o funct1.o funct2.o main.o $ Remember the -c option will not produce an executable module, but it does create the object modules. These object modules are file files that we will place into an archive.

    6.9.1 ar: Creating an Archive File with Object Modules

    In this call to ar, we will use the r command key which will replace the named files in the archive. The v option will give a verbose file-by-file description of the making of the new archive file. Sample Session: $ar rv functs.a funct1.o funct2.o a - funct1.o a - funct2.o ar: creating functs.a $ The name of the new archive file is functs.a. The files that have been added to that archive are funct1.o and funct2.o. The file protections for the new archive file are rw-r--r--.

    6.9.2 ar: Verifying the Contents of the Archive File

    The key command to list the table of contents is t. The t command will print a table of contents of the archive file. When the v option is used with the t command it will give a long listing of all information about the files. Sample Session: $ar tv functs.a rw-r--r-- 115/ 200 448 Sep 27 09:56 1990 funct1.o rw-r--r-- 115/ 200 448 Sep 27 09:56 1990 funct2.o $ This output shows that there are two members in this archive file, namely, funct1.o and funct2.o. The protections of these files is: owner - read and write group - read other - read The fields are, left to right, the file protections, owner, group, size (in bytes), creation date and time, and finally the name of the constituent.

    6.9.3 ar: Removing Duplicate Object Files

    Once the archive has been created and verified, the object files in your directory can be deleted. This can be accomplished with the rm command. Sample Session: $rm funct?.o $ The question mark (?) is a wildcard that stands for any single character. The files funct1.o and funct2.o no longer exist in your subdirectory.

    6.9.4 ar: Compiling Main and Archive Files

    Now that the object files, funct1.o and funct2.o, are in the archive file functs.a you, can link them with main.o in the following manner. Sample Session: $cc -o new_hello main.o functs.a $ls -la new_hello -rwxr-xr-x 1 teacher class 17570 Sep 27 12:58 new_hello $

    Workshop 6

    This workshop will reinforce your understanding of the ideas presented in Chapter 6. Login to the Multimax using the username and password given to you by the instructor. Each student is to complete the entire workshop. DESK EXERCISES 1. What is a UNIX process? 2. When a command is given to the Shell it will fork a child process to execute the command. True/False 3. What is a process identification number (PID)? 4. What is the name of the Shell variable that contains the current PID? 5. What is the UNIX command to find the PIDs associated with the controlling terminal? What option is needed to get detailed information? 6. What does the UNIX command grep do? 7. What do the following regular expressions represent? ^Ba .* BB* J* [0-9]*$ 8. What does the UNIX command sed do? 9. What does the UNIX command awk do? 10. What does the UNIX command sort do? 11. What is the main use for the UNIX command ar? COMPUTER EXERCISES Use the phone.lis database file to answer the following questions. 12. "I want to find all the phone numbers that begin with a 4 and end with a 2" 13. "I can't remember the name but I believe the last name starts with an S and the first name with an F" 14. Find all the people with 3 character first names. 15. Write a grep command that finds all the phone numbers that don't begin with a 4, 5, or 6. 16. Write a grep command that finds all entries beginning with J-Z and ending with a 2 or 5. 17. Put a 23 in front of every phone number. (Hint:sed) 18. Replace the first name with the person's first initial and a period. 19. Task: A new phone system has been installed and people with phone extensions beginning with 4 or 5 now have a new prefix: 73. Create a file of only the people with the new phone numbers. 20. Print out the phone list showing last name and first name in the following format and sorted by last name. First name Last name That's enough, don't you think?

    Go to the top, first,previous,next, last section, table of contents.
  • -
    Chapter 6c
    -
     

    Introduction
    Table of Contents
    Chapter 1
    Chapter 2a
    Chapter 2b
    Chapter 3
    Chapter 4a
    Chapter 4b
    Chapter 5a
    Chapter 5b
    Chapter 6a
    Chapter 6b
    Chapter 6c
    Chapter 7a
    Chapter 7b
    Chapter 8
    Chapter 9a
    Chapter 9b
    Manual: ar(1)
    Manual: cb(1)
    Manual: cc(1)
    Manual: expr(1)
    Manual: f77(1)
    Manual: ftp(1)
    Manual: ksh(1)
    Manual: lint(1)
    Manual: sh(1)
    Manual: test(1)
    Manual: time(1)


    LPAR Leasing
    Lease an AIX / i5 LPAR
    Reduce your costs

    www.mtxia.com

    Server Leasing
    Lease a Server off-site
    Reduce your costs

    www.mtxia.com

    Data Center Automation
    Business Continuity and DR
    Virtualization/Consolidation

    www.mtxia.com

    HMC Service
    Hardware Management Console
    Manage Remote AIX / i5 LPARs

    www.siteox.com

    Business Web Site Hosting
    $3.99 / month includes Tools,
    Shopping Cart, Site Builder

    www.siteox.com

    FREE Domain Registration
    included with Web Site Hosting
    Tools, Social Networking, Blog

    www.siteox.com

    Disaster Recovery
    Small Business Oriented
    Off-Site Facilities

    www.mtxia.com

    IBM pSeries / iSeries
    Reduce your Costs
    Off-Site Server Hosting

    www.mtxia.com