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

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

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

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

John Levine, Margaret Levine Young

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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






Gurgle, Gurgle: Running Data through Pipes

The process of redirecting the output of one program so that it becomes the input of another program can be quite useful. This process is the electronic equivalent of whisper-down-the-lane, with each program passing information to the next program and doing something to the information being whispered.

To play whisper-down-the-lane with UNIX, you use a pipe. The symbol for a pipe is a vertical bar (| ). Search your keyboard for this character. It’s often on the same key with \ (the backslash). Sometimes the key shows the vertical bar with a gap in the middle, although the gap doesn’t matter. If you type two commands separated by a | , you tell UNIX to use the output of the first command as input for the second command.


Gimme just a little at a time


When you have many files in a directory, the output of the ls command can go whizzing by too fast to read, which makes seeing the files at the beginning of the list impossible before they disappear off the top of the screen. A UNIX program called more solves this problem. The more program displays on-screen the input you give it, and it pauses as soon as it fills the screen and waits for you to press a key to continue. To display your list of files one screen at a time, type this line:


ls | more

This line tells the ls command to send the file listing to the more command. The more command then displays the listing. You can think of the information from the ls command gurgling down through the little pipe to the more command (we think of it this way).


The cat and the fiddle . . . er, file


As we explain in Chapter 5, you can use the cat command to display the contents of a text file. If the text file is too long to fit on-screen, however, the beginning of the file disappears too fast to see. You can display a long file one screen at a time in these two ways:



Redirect the output of the cat command to more by typing the following line (assuming, of course, that the file is called really.long.file ):


cat really.long.file | more



Just use the more command by typing this line:


more really.long.file



Tip If you use the more command without a pipe (without the | ), more takes the file you suggest and displays it on-screen a page at a time.


Sorting, sort of


A program called sort sorts a file line-by-line in alphabetical order. The program alphabetizes all the lines according to the beginning of each line. Each line in the file is unaffected; just the order of the lines changes.

Suppose that you have a file called honors.students , which looks like this:
Meg Young
Shelly Horwitz
Neil Guertin
Stuart Guertin
Sarah Saxon
Zac Young
Gillian Guertin
Tucker Myhre
Andrew Guertin
Megan Riley
Chloe Myhre
To sort it line by line into alphabetical order, type this:


sort honors.students

The result looks like this:

Andrew Guertin
Chloe Myhre
Gillian Guertin
Meg Young
Megan Riley
Neil Guertin
Sarah Saxon
Shelly Horwitz
Stuart Guertin
Tucker Myhre
Zac Young
The list appears on-screen, however, and nowhere else. If you want to save the sorted list, type
sort honors.students > students.sorted

You can also sort the output of a command:


ls | sort

Because ls displays filenames in alphabetical order anyway, of course, this example doesn’t do you much good. If you want the filenames in reverse alphabetical order, however (we’re stretching for an example here), you can use the -r option with the sort command:


ls | sort -r

Tip If you are sorting numbers, be sure to tell UNIX. Otherwise, it sorts the numbers alphabetically (the sort of imbecilic and useless trick only a computer would do). To sort numbers, use the -n option:


sort -n order.numbers

Suppose that your file of honors students contains total test scores:

10000 Meg Young
8000 Shelly Horwitz
7000 Neil Guertin
5000 Stuart Guertin
9000 Sarah Saxon
5000 Zac Young
8000 Gillian Guertin
7000 Tucker Myhre
11000 Andrew Guertin
6000 Megan Riley
7000 Chloe Myhre
When you alphabetize things as letters, not as numbers, a 1 comes before an 8 no matter what, even if it’s the first number of 10 . When you alphabetize things as numbers, 10 comes after 8 , not before it. If you sort this file as letters, with this command:


sort honors.students

you get 10000 Meg Young
11000 Andrew Guertin
5000 Stuart Guertin
5000 Zac Young
6000 Megan Riley
7000 Chloe Myhre
7000 Neil Guertin
7000 Tucker Myhre
8000 Gillian Guertin
8000 Shelly Horwitz
9000 Sarah Saxon
This output does not show the bonus amounts in any useful order. If you sort the file as numbers, with this command:


sort -n honors.students

you get this more useful listing:
5000 Stuart Guertin
5000 Zac Young
6000 Megan Riley
7000 Chloe Myhre
7000 Neil Guertin
7000 Tucker Myhre
8000 Gillian Guertin
8000 Shelly Horwitz
9000 Sarah Saxon
10000 Meg Young
11000 Andrew Guertin
If the file contains letters, not numbers, the -n option has no effect.


Can we get that on paper?


Being able to print the output of a command is terrifically useful when you want to send to a printer something that normally appears on-screen. To print a listing of your files, for example, type this line:


ls | lp

Linux Users of Linux and BSD UNIX use the lpr command rather than lp . (Chapter 9 explains other stuff about printing.) You can use more than one pipe if you want to be advanced. To print a list- ing of your files in reverse order, for example, you can use this convoluted command:


ls | sort -r | lp

/ 213