Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Complete Idiot's Guide to Linux
(Publisher: Macmillan Computer Publishing)
Author(s): Manuel Ricart
ISBN: 078971826x
Publication Date: 12/22/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Sorting Lines of Text: sort

The sort command allows you to order lines in a file according to numeric value, alphabetic order, or phone book order. The sort command makes use of the notion of ”fields,” in which a line of text represents various bits (fields) of data and each line in the file represents a record. Fields are typically separated by a special character, such as a colon, tab, comma, or any other character that the developer of the file thought of.

The more frequent options to sort are

-t To specify a field separator
-n To specify numeric sorting
-k To specify the fields where the sort will take place (default is first character on the line)
-f To ignore case (the letters ”B” and ”b” are treated as equal)
-r To reverse result

If you wanted to sort the /etc/password file according to username, you could type this:

     [alberto@digital alberto]$ sort /etc/passwd
     adm:x:3:4:adm:/var/adm:
     alberto:x:501:501:Caldera OpenLinux
      User:/home/alberto:/bin/bash
     bin:x:1:1:bin:/bin:
     ...
     shutdown:x:6:11:shutdown:/sbin:/sbin/shutdown
     sync:x:5:0:sync:/sbin:/bin/sync
     uucp:x:10:14:uucp:/var/spool/uucp:

To sort the file numerically according to UID (the user id for the user is the third field), use the following command. Note that in the /etc/passwd file, fields are separated by colons:

    [alberto@digital alberto]$ sort -t : -k 3 -n /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:
    daemon:x:2:2:daemon:/sbin:
    adm:x:3:4:adm:/var/adm:
    …
    nobody:x:65534:65534:Nobody:/:/bin/false

Formatting Text: fmt

If you write a file using a console-based editor, the editor might not word-wrap your text into lines. If you were to print the file, the text will probably not be wrapped for you. Most users like to have files with lines that are around 75 characters wide, because that is the amount of text that fits into a text terminal. You can apply word-wrap to any file by using the fmt command. Here’s an example:

     [alberto@digital alberto]$ cat intro.txt
     What This Book Is About (Paraphrasing Randal L. Schwartz in
     Learning  Perl)

     Among other things, this book is about 300 pages (and then some).
     This book is also
     a gentle introduction to Linux. By the time you’ve gone
     through this book, you’ll
     have touched on the majority of the most common operations found
     in Linux.

     This book is not intended as a comprehensive guide to Linux   - on
     the contrary,
     in order to keep the book from being yet another comprehensive
     reference guide,
     I‘ve been selective about covering the things you are
     most likely to
     use early in
     your Linux hacking career. For more information, check out the
     voluminous and
     readily available Linux and UNIX reference material.
     ...

If you look at the output, you’ll notice that words are broken at the wrong places. Using the fmt command, I can easily reformat this file for printing and reading better on the screen:

     [alberto@digital alberto]$ fmt -w 68 intro.txt
     What This Book Is About (Paraphrasing Randal L. Schwartz in
     Learning Perl)

     Among other things, this book is about 300 pages (and then
     some). This book is also a gentle introduction to Linux. By the
     time you’ve gone through this book, you’ll have touched on the
     majority of the most common operations found in Linux.

     This book is not intended as a comprehensive guide to Linux -
     on the contrary, in order to keep the book from being yet another
     comprehensive reference guide, I’ve been selective about covering
     the things you are most likely to use early in your Linux hacking
     career. For more information, check out the voluminous and readily
     available Linux and UNIX reference material.

Much better! The fmt command has many options; however, the -w and -s options are the most important. The -w option tells fmt how wide to make the line. If you don’t provide a -w option, fmt reverts to using 75 characters as the width of a line. The -s option tells fmt to only split lines. If you had some sample text that was shorter, the default behavior is for fmt to join lines and then split them according to its internal rules.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.