Linux/Unix sorting, merging, and segmentation

Source: Internet
Author: User
Linux/Unix sort, merge and split sort: sort [oracle@localhostsort.folder] $ sort-cls.outsort: ls. out: 2: disorder:-rw-r -- 1oracleoinstall2653Feb2321: 30awk.outwww.2cto.com indicates... linux/Unix sorting, merge and split sorting: sort [oracle @ localhost sort. folder] $ sort-c ls. out sort: ls. out: 2: disorder:-rw-r -- 1 oracle oinstall 2653 Feb 23 awk. out www.2cto.com indicates that it is not classified and sort ls is used. out> ls. try again by sort: [oracle @ localhost sort. folder] $ sort ls. ou T> ls. sort [oracle @ localhost sort. folder] $ sort-c ls. sort-k5 indicates that the data is sorted in descending order according to row 5th: [oracle @ localhost sort. folder] $ sort-k 5 ls. out total 80-rw-r -- r -- 1 oracle oinstall 0 Feb 24 ls. out-rw-r -- 1 oracle oinstall 0 Feb 24 14:21 sed. sh-rwxr-xr-x 1 oracle oinstall 26 Nov 10 22:30 main. sh-rwxrwxrwx 1 oracle oinstall 44 Jan 7 22: 39 shelltest. sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 :51 test1.sh-rwxr-x R-x 1 oracle oinstall 99 Feb 23 test2.sh-rw-r -- 1 oracle oinstall 216 Feb 23 blank_file-rwxr -- r -- 1 oracle oinstall 642 Feb 15 find. sh-rwxr -- r -- 1 oracle oinstall 2250 Feb 23 awk. sh-rw-r -- 1 oracle oinstall 2653 Feb 23 awk. outdrwxr-xr-x 2 oracle oinstall 4096 Feb 15 folder www.2cto.com-k5 indicates to sort by 5th rows in descending order: [oracle @ localhost sort. folder] $ sort-k5-r ls. out drw Xr-x 2 oracle oinstall 4096 Feb 15 folder-rw-r -- 1 oracle oinstall 2653 Feb 23 awk. out-rwxr -- r -- 1 oracle oinstall 2250 Feb 23 awk. sh-rwxr -- r -- 1 oracle oinstall 642 Feb 15 find. sh-rw-r -- 1 oracle oinstall 216 Feb 23 blank_file-rwxr-xr-x 1 oracle oinstall 99 Feb 23 test2.sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 test1.sh-rwxrwxrwx 1 oracle oinstall 44 Jan 7 shelltest. sh-rwxr-xr-x 1 oracle oinstall 26 Nov 10 22:30 main. sh-rw-r -- 1 oracle oinstall 0 Feb 24 14:21 sed. sh-rw-r -- 1 oracle oinstall 0 Feb 24 14:21 ls. outtotal 80 returns the first line after sorting: [oracle @ localhost sort. folder] $ sort-k5-r ls. out | head-1drwxr-xr-x 2 oracle oinstall 4096 Feb 15 folder returns the last two rows after sorting: [oracle @ localhost sort. folder] $ sort-k5-r ls. out | tail-2-rw-r -- r -- 1 ora Cle oinstall 0 Feb 24 14:21 ls. output result of using sort for outtotal 80 awk: [oracle @ localhost sort. folder] $ sort-k5-r ls. out | head-2 | awk '{print "file_name:" $9 "size:" $5} 'file_name: folder size: 4096file_name: awk. out size: 2653 the content of a file is as follows: [oracle @ localhost sort. folder] $ cat iplist 193.132.80.123 dave tansley193.132.80.23 HP printer 2nd floor193.132.80.198 JJ. peter's scanner193.132.80.38 SPARE193.132.80.78 P. E Dron needs to be sorted according to the last segment of the IP address: [oracle @ localhost sort. folder] $ sort-t. -k4-r-n iplist193.132.80.198 JJ. peter's scanner193.132.80.123 dave tansley193.132.80.78 P. edron193.132.80.38 SPARE193.132.80.23 HP printer 2nd floor-r descending order,-t. indicates that. for classification,-n indicates that the value type uniq is sorted for repeated usage: its option meaning:-u only displays non-repeated rows. -D: only duplicate data rows are displayed. for each duplicate row, only one-c row is displayed. -F n is a number, and the first n fields are ignored. Deduplication of sorted file content: [oracle @ localhost sort. folder] $ cat ls. sort total 80 total 80-rw-r -- r -- 1 oracle oinstall 0 Feb 24 ls. out-rw-r -- 1 oracle oinstall 0 Feb 24 14:21 ls. out-rw-r -- 1 oracle oinstall 0 Feb 24 14:21 sed. sh-rw-r -- 1 oracle oinstall 0 Feb 24 14:21 sed. sh-rwxr-xr-x 1 oracle oinstall 26 Nov 10 22:30 main. sh-rwxr-xr-x 1 oracle oinstall 26 Nov 10 22:30 main. sh-rwxrwxrwx 1 oracle Oinstall 44 Jan 7 shelltest. sh-rwxrwxrwx 1 oracle oinstall 44 Jan 7 22: 39 shelltest. sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 :51 test1.sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 :51 test1.sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 test2.sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 test2.sh-rw-r -- 1 oracle oinstall 216 Feb 23 blank_file-rw-r -- r -- 1 oracle oinstall 216 Feb 23 Blank_file-rwxr -- r -- 1 oracle oinstall 642 Feb 15 find. sh-rwxr -- r -- 1 oracle oinstall 642 Feb 15 find. sh-rwxr -- r -- 1 oracle oinstall 2250 Feb 23 awk. sh-rwxr -- r -- 1 oracle oinstall 2250 Feb 23 awk. sh-rw-r -- 1 oracle oinstall 2653 Feb 23 awk. out-rw-r -- 1 oracle oinstall 2653 Feb 23 awk. outdrwxr-xr-x 2 oracle oinstall 4096 Feb 15 folderdrwxr-xr-x 2 oracle oinstall 4096 Feb 15 folder [oracle @ localhost sort. folder] $ uniq ls. sort total 80-rw-r -- r -- 1 oracle oinstall 0 Feb 24 ls. out-rw-r -- 1 oracle oinstall 0 Feb 24 14:21 sed. sh-rwxr-xr-x 1 oracle oinstall 26 Nov 10 22:30 main. sh-rwxrwxrwx 1 oracle oinstall 44 Jan 7 22: 39 shelltest. sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 :51 test1.sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 11: 52 test2.sh-rw-r -- 1 oracle oinstall 216 Feb 23 blank_file-rwxr -- r -- 1 oracle oinstall 642 Feb 15 find. sh-rwxr -- r -- 1 oracle oinstall 2250 Feb 23 awk. sh-rw-r -- 1 oracle oinstall 2653 Feb 23 awk. outdrwxr-xr-x 2 oracle oinstall 4096 Feb 15 folder show repeated times: [oracle @ localhost sort. folder] $ uniq-dc ls. sort 2 total 80 2-rw-r -- 1 oracle oinstall 0 Feb 24 ls. ou T 2-rw-r -- 1 oracle oinstall 0 Feb 24 14:21 sed. sh 2-rwxr-xr-x 1 oracle oinstall 26 Nov 10 main. sh 2-rwxrwxrwx 1 oracle oinstall 44 Jan 7 22: 39 shelltest. sh 2-rwxr-xr-x 1 oracle oinstall 99 Feb 23 :51 test1.sh 2-rwxr-xr-x 1 oracle oinstall 99 Feb 23 :52 test2.sh 2-rw-r -- oracle oinstall 216 Feb 23 blank_file 2-rwxr -- r -- 1 oracle oinstall 642 Feb 15 find. sh 2-r Wxr -- r -- 1 oracle oinstall 2250 Feb 23 awk. sh 2-rw-r -- 1 oracle oinstall 2653 Feb 23 awk. out 2 drwxr-xr-x 2 oracle oinstall 4096 Feb 15 folder ls. the sort2 file contains the following content: [oracle @ localhost sort. folder] $ cat ls. sort2 0 oracle oinstall0 oracle oinstall0 oracle oinstall0 oracle oinstall26 oracle oinstall26 oracle oinstall44 oracle oinstall44 oracle oinstall99 oracle oinstall99 oracle oin Stall99 oracle oinstall99 oracle oinstall216 oracle oinstall216 oracle oinstall1_oracle oinstall1_oracle oinstall2250 oracle oinstall2250 oracle oinstall2653 oracle oinstall2653 oracle oinstall4096 oracle oinstall4096 oracle oinstall deduplication display: [oracle @ localhost sort. folder] $ uniq-c ls. sort2 4 0 oracle oinstall 2 26 oracle oinstall 2 44 oracle oinstall 4 99 oracle oinstall 2 216 oracle oinstall 2 642 oracle oinstall 2 2250 oracle oinstall 2 2653 oracle oinstall 2 4096 oracle oinstall ignore using the first two fields of-f, the system determines that all data is duplicated: [oracle @ localhost sort. folder] $ uniq-cf2 ls. sort2 22 0 oracle oinstall join usage: There are two files with the following content: [oracle @ localhost sort. folder] $ cat names.txt M. golls 12 Hidd RdP. heller The AcreP. willey 132 The GroveT. norms 84 Connaught RdK. fletch 12 Woodlea [oracle @ localhost sort. folder] $ cat town.txt M. golls Norwith NRDP. willey Galashiels GDDT. norms Brandon BSLK. fletch Mildenhall MAF: connect two files (similar to join in SQL): [oracle @ localhost sort. folder] $ join names.txt town.txt M. golls 12 Hidd Rd Norwith NRDP. willey 132 The Grove Galashiels GDDT. norms 84 Connaught Rd Brandon BSLK. fletch 12 Woodlea Mildenhall MAF shows the lines in the first file that do not match the content of the second file: [oracle @ localhost sort. folder] $ join-a1 names.txt town.txt M. golls 12 Hidd Rd No Rwith NRDP. heller The AcreP. willey 132 The Grove Galashiels GDDT. norms 84 Connaught Rd Brandon BSLK. fletch 12 Woodlea Mildenhall MAF displays the first domain of the first file and the second domain of the second file: [oracle @ localhost sort. folder] $ join-o 1.1, 2.2 names.txt town.txt M. golls NorwithP. willey GalashielsT. norms BrandonK. fletch Mildenhall uses domain 1 of file 1 and domain 1 of file 2 as the connection key: [oracle @ localhost sort. folder] $ join-j1 1-j2 1 names.txt town.txt M. golls 12 Hidd Rd Norwith NRDP. Willey 132 The Grove Galashiels GDDT. Norms 84 Connaught Rd Brandon BSLK. Fletch 12 Woodlea Mildenhall MAF cut is used to cut columns or fields from standard input or text. The cut text can be pasted into a text file: cut the third domain separated by dots: [oracle @ localhost sort. folder] $ cut-d. -f3 iplist 8080808080 paste: [oracle @ localhost sort. folder] $ ls | paste-d "" -iplistls.outls.sortls.sort2names.txtpaste1paste2sort.shtown.txt paste the two files into two columns separated by colons: [oracle @ localhost sort. folder] $ paste-d: names.txt town.txt M. golls 12 Hidd Rd: M. golls Norwith NRDP. heller The Acre: P. willey Galashiels GDDP. willey 132 The Grove: T. norms Brandon BSLT. norms 84 Connaught Rd: K. fletch Mildenhall MAFK. fletch 12 Woodlea: split File: This method can be used when the file is large: [oracle @ localhost sort. folder] $ lltotal 64-rw-r -- r -- 1 oracle oinstall 143 Mar 3 iplist-rw-r -- 1 oracle oinstall 620 Mar 3 ls. out-rw-r -- 1 oracle oinstall 1240 Mar 3 ls. sort-rw-r -- 1 oracle oinstall 436 Mar 3 ls. sort2-rw-r -- r -- 1 oracle oinstall 104 Mar 3 names.txt-rw-r -- 1 oracle oinstall 12 Mar 3 paste1-rw-r -- r -- 1 oracle oinstall 0 Mar 3 paste2-rwxr -- r -- 1 oracle oinstall 0 Mar 3 09:51 sort. sh-rw-r -- 1 oracle oinstall 88 Mar 3 town.txt splits the file into two lines and one File: [oracle @ localhost sort. folder] $ split-2 ls. out [oracle @ localhost sort. folder] $ lltotal 112-rw-r -- r -- 1 oracle oinstall 143 Mar 3 iplist-rw-r -- 1 oracle oinstall 620 Mar 3 ls. out-rw-r -- 1 oracle oinstall 1240 Mar 3 ls. sort-rw-r -- 1 oracle oinstall 436 Mar 3 ls. sort2-rw-r -- r -- 1 oracle oinstall 104 Mar 3 names.txt-rw-r -- 1 oracle oinstall 12 Mar 3 paste1-rw-r -- r -- 1 oracle oinstall 0 Mar 3 paste2-rwxr -- r -- 1 oracle oinstall 0 Mar 3 09:51 sort. sh-rw-r -- 1 oracle oinstall 88 Mar 3 town.txt-rw-r -- 1 oracle oinstall 108 Mar 3 xaa-rw-r -- 1 oracle oinstall 113 Mar 3 xab-rw-r -- 1 oracle oinstall 109 Mar 3 xac-rw-r -- 1 oracle oinstall 111 Mar 3 xad-rw-r -- r -- 1 oracle oinstall 116 Mar 3 xae-rw-r -- 1 oracle oinstall 63 Mar 3 xaf
 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.