UNIX For Dummies [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

UNIX For Dummies [Electronic resources] - نسخه متنی

John Levine, Margaret Levine Young

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید






Assorted Files


Computers are good at putting stuff in order. Indeed, at one time a third of all computer time was spent sorting. UNIX has a quite capable sorting program, cleverly named sort , that you may remember meeting briefly in Chapter 7. Here, we talk about some other ways to use the program.

The sort command sorts the lines of a file into alphabetical order. From the sort point of view, a line is anything that ends with a carriage return (that is, you pressed Enter). If you have a file containing a list, with one item per line, this command alphabetizes the list.

The easiest way to use sort is to sort one file into another. In other words, you tell sort to place the sorted version of the original file in another file. This way, you don’t risk screwing up the original file if the sort runs amok. To sort the original myfile into a second file named sortedfile , type this command:


sort myfile > sortedfile

Tip Although you can sort a file back into itself, you can’t do it in the obvious way. The following line, for example, doesn’t work:


sort myfile > myfile

The problem with this command is that the UNIX shell clears out myfile before the sort starts (with the result that, when sort tries to sort something, it finds that myfile is empty). You can use the -o (for output) option to tell sort where to put the results, like this:


sort myfile -o myfile

This command works because sort doesn’t start to write to the output file until it has read all its input.

Normally, sort orders its results based on a strict comparison of the internal ASCII codes the computer uses for storing text. The good news is that this command sorts letters and digits in the correct way, although some peculiarities exist: Normally, uppercase letters are sorted before lowercase letters, so ZEBRA precedes aardvark. You can use the -f (for fold cases together) option to sort regardless of uppercase and lowercase letters:


sort -f animals -o sortedanimals

Although we could use the > redirection symbol in this example, with the sort command using the -o option is safer. You can use several other options also to tell it to sort:

















-b

Ignore spaces at the beginning of the line.


-d

Use dictionary order and ignore any punctuation. You usually use this option with -f .


-n

Sort based on the number at the beginning of the line. With this option, 99 precedes 100 rather than follows it, as it does in usual alphabetical order. (Yes, the normal thing the computer does is pretty dumb. Are you surprised?)

-r

Sort in the reverse order of whatever would have been done otherwise. You can combine this option with any of the others.


We find sorting to be particularly useful in files in which every line starts with a date, as shown in these examples:

0505 Tonia’s birthday
1204 Meg’s birthday
1102 Zac’s birthday
0318 Sarah’s birthday
We could type sort -n to sort this file by date. Notice that we wrote May 5 as 0505 (not 55, for example) so that a numeric sort works.

You can do much more complex sorting and treat every line as a sequence of “fields” that sort uses to decide the final sorted order. If you really need to do complex sorting, talk to someone who knows something about sorting or, if you’re feeling adventurous, type man sort .

/ 213