tailieunhanh - Unix Shell Programming Third Edition phần 3

Như đã đề cập, nếu bạn bỏ qua trên các lĩnh vực, sắp xếp giả định rằng các lĩnh vực bị bỏ qua được giới hạn bởi các ký tự không gian hoặc tab. Tùy chọn-t nói khác đi. Trong trường hợp này, nhân vật sau t-được thực hiện như là ký tự phân cách. Nhìn vào tập tin mật khẩu mẫu của chúng tôi một lần nữa: | 5 27 sort -n data Sort arithmetically -5 11 2 12 3 33 5 27 14 -9 15 6 23 2 Skipping Fields If you had to sort your data file by the y value that is the second number in each line you could tell sort to skip past the first number on the line by using the option 1n instead of -n. The 1 says to skip the first field. Similarly 5n would mean to skip the first five fields on each line and then sort the data numerically. Fields are delimited by space or tab characters by default. If a different delimiter is to be used the -t option must be used. sort 1n data Skip the first field in the sort 14 -9 23 2 15 6 -5 11 2 12 5 27 3 33 The -t Option As mentioned if you skip over fields sort assumes that the fields being skipped are delimited by space or tab characters. The -t option says otherwise. In this case the character that follows the -t is taken as the delimiter character. Look at our sample password file again cat etc passwd root 0 0 The super User usr bin ksh steve 203 100 users steve usr bin ksh bin 3 3 The owner of system files cron 1 1 Cron Daemon for periodic tasks george 75 75 users george usr lib rsh pat 300 300 users pat usr bin ksh uucp 5 5 usr spool uucppublic usr lib uucp uucico asg 6 6 The Owner of Assignable Devices sysinfo 10 10 Access to System Information usr bin sh mail 301 301 usr mail If you wanted to sort this file by username the first field on each line you could just issue the command sort etc passwd To sort the file instead by the third colon-delimited field which contains what is known as your user id you would want an arithmetic sort skipping the first two fields 2n specifying the colon character as the field delimiter -t sort 2n -t etc passwd Sort by user id root 0 0 The Super User usr bin ksh cron 1 1 Cron Daemon for periodic tasks bin 3 3 The owner of system files uucp 5 5 usr spool uucppublic usr lib uucp uucico asg 6 6 The Owner of Assignable Devices sysinfo 10 10 Access to System Information usr bin sh george 75 75 users george usr lib rsh