Shell Script (iii)

Source: Internet
Author: User
Tags ranges



Shell Script (iii)2. File sorting, merging and segmentation(1) Sort command

#-c: The test file is sorted;-K: Specifies the sort field;-M: merges two sorted files;

#-n: Sort by number size;-o: Writes output to file;-r: Reverses sorting results

#-t: Changing the field delimiter;-U: Removing duplicate rows from the result

Reverse sort #将a. txt and remove duplicate rows

Sort-u-R A.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/10/wKioL1VyvlHgv2Q5AACL5nWYlss092.jpg "/>

#以 ":" is a delimiter that sorts/etc/passwd according to the size of the 3rd field number

SORT-T:-k3n/etc/passwd

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/14/wKiom1VyvLKSkVryAAMRrUH0Wns003.jpg "/>

#合并已经排序的两个文件, the two files that need to be merged must have been sorted.

Sort-u-M A.txt b.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/10/wKioL1VyvlKyjEBrAACcA49_NiI117.jpg "/>

#根据第2域来排序

Sort-k2 A.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/10/wKioL1VyvlLjXpP6AABr_8PtRws334.jpg "/>


(2) uniq command


#去除文本中重复的行,-C: Prints the number of occurrences of the first line in the text;-D: Only duplicate records are displayed.

#每个重复记录只出现一次;-U: Show only records with no duplicates

#需要特别注意的是所说的 "Repetition" refers to repeated occurrences! such as file Test.txt content:

1

1

2

1

Uniq-c Test.txt

#结果为, the 1th row number 1 appears as 2, the 2nd row number 2 appears 1, the 4th number 1 appears 1

2 1

1 2

1 1

#再如

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/14/wKiom1VyvLLiqO81AACsq75LAwI222.jpg "/>

#如果需要统计文件中所有重复的行, you need to sort it first

Sort 10.txt | Uniq-c 10.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/14/wKiom1VyvLOwv-gSAABiNgzk164835.jpg "/>

#注意与uniq-C 10.txt Difference

#只显示重复的行, notice the difference.

Uniq-d 10.txt

Sort 10.txt | Uniq-d 10.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/10/wKioL1VyvlKj3r_lAACES8IjnVA766.jpg "/>

#只显示没有重复的行, note the comparison of the result of removing duplicate rows before and after file sorting

#因为第1行开始数字1连续出现3次, so Uniq-u 10.txt will not show 1 on line 1th

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/14/wKiom1VyvLOSoWM1AAC3DWT4wbA100.jpg "/>


(3) Join command

#实现两个文件记录的连接, it is necessary to note that the file to be connected to the first order, or error!

#必需对连接域进行排序再连接, or Error!

#-i: Ignores case differences when comparing field content;-O: Sets the format of the result display;-T: Changes the field delimiter;

#-1 and -2:-1 settings file 1 for connected domains, 2 settings file 2 for connected domains

#-a1 or-A2: In addition to displaying the results of a connection to a common domain,-A1 indicates that there is no common in file 1

#域的记录;-a2 shows records with no common fields in file 2

#连接两个文件相同的域, the field with ":" As the delimiter, note that "B Lily" and "B Lily" is different!

JOIN-T: A.txt b.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/10/wKioL1VyvlOhKhw9AADVYZuaKw0771.jpg "/>

#添加-I ignores case join effect

JOIN-T:-I a.txt b.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/10/wKioL1VyvlThBfqnAABgRZWFRkc282.jpg "/>

#显示文件1第1域, file 2, domain 3, and file 1 domain 2nd

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/14/wKiom1VyvLWipdJ5AAElYDEQgyI701.jpg "/>

#-v1: The result of a connection to a common domain is not displayed for file 1

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/14/wKiom1VyvLXAs_y5AADG_qO4TtU253.jpg "/>

#-v2: File 2 is not displayed as a result of connection to the common domain 650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M01/6E/10/wKioL1VyvlXTm_ Gbaadopjqqlak249.jpg "/>

#-1: The domain to which the file 1 needs to connect;-2: File 2 The domain to which the connection is required. The specified field must be sorted first

#默认比较文件1和文件2的第1域.

#以文件1的第1域和文件2的第3域连接

JOIN-T:-1 1-2 3 name.txt num.txt650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M00/6E/10/ Wkiol1vyvlxs02hvaaeklmrl75q410.jpg "/>

(4) Cut command

#cut从标准输入或文本文件中按域或行提取文本

#-c: Specifies the number of characters or ranges of characters to extract;-F: Specifies the number of domains or domains to extract;-D: Specify the domain delimiter

#cut从标准输入或文本文件中按域或行提取文本

#-c: Specifies the number of characters or ranges of characters to extract;-F: Specifies the number of domains or domains to extract;-D: Specify the domain delimiter

#-cm-n: Represents the M to nth character;-cm,n: Represents the M and nth characters;

#-cn: represents n characters;-cn-: Represents the nth character to the last character.

#-fm-n: Represents the M to Nth field;-fm,n: Represents the M field and the Nth field;-FN: Represents the nth field;

#-fn-: Represents the field from Nth to the last. Similar to-C.

#提取第5个字符

CUT-C5 Name.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/14/wKiom1VyvLagKnvbAAB55UMAhu0887.jpg "/>

#提取第4到第6个字符

Cut-c4-6 Name.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/10/wKioL1VyvlXAl3BZAAB8qXLhDdk753.jpg "/>

#提取第2到最后字符

Cut-c2-name.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/14/wKiom1VyvLaTkYgGAACKJNwNhJc875.jpg "/>

#提取第2和第6个字符

cut-c2,6 Name.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/14/wKiom1VyvLaALsO0AACi09VUUsw723.jpg "/>

#以 ":" As a delimiter to extract the 1th field

cut-d:-f1 num.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/10/wKioL1VyvlbzbYGsAACYBEHxdl8652.jpg "/>

#以 ":" As a delimiter, extracting 1th domain to 3rd domain

cut-d:-f1-3 num.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/10/wKioL1VyvlaAJe7PAAC4IlN42hs117.jpg "/>

#以 ":" As a delimiter, extracting the 2nd field to the last 1 domains

Cut-d:-f2-num.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/14/wKiom1VyvLfA1HJ9AADGTheIalo160.jpg "/>

#以 ":" for delimiters, extracting 1th and 4th domains

cut-d:-f1,4 num.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/14/wKiom1VyvLfR9-L8AAC7ZTwDD28561.jpg "/>

#提取第3域的第5个字符

cut-d:-f3 num.txt | Cut-c5

(5) Paste command

#将文本或标准输出中的内容粘贴到新的文件, you can paste data from different files together

#-d: Specifies the delimiter, which defaults to a space or tab;-s: Pastes each file into 1 rows;-: Reads data from standard output

#合并name. txt and num.txt, and with "-" as a delimiter

Paste-d-Name.txt Num.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/10/wKioL1VyvlihbCFNAADuw4e36Gk284.jpg "/>

#以 "-" as a delimiter, a file of 1 lines to merge Name.txt and Num.txt

#即每一个文件行与行之间以 "-" As a connection, concatenate all rows into 1 rows

#后面的文件以同样方式连接, then append to the last file of the previous one, and eventually a file 1 lines

Paste-d--S Name.txt num.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/10/wKioL1VyvliTVhTHAADiHO8RT1E833.jpg "/>

#合并name. txt and num.txt, with the default delimiter as the delimiter

Paste-s Name.txt Num.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/14/wKiom1VyvLmzat-UAAB-vVCZvt8424.jpg "/>

(6) Split command

#split option file Star_name, the file name of the cut starts with Start_name and begins with X by default

#将大文件切割成小文件, you can cut files according to the number of lines, bytes, and the output of the small

#小文件中自动加上编号

#-number N or-l digit N: N rows as a file;-B: Specifies how many bytes to use as a file;

#-c: Similar to-B, but as much as possible to maintain the integrity of each line when cutting


#以2行作为1个文件, the cut file name starts with StartName

Split-2 Name.txt StartName

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/14/wKiom1VyvLqgF0hZAAEIB2avtBA411.jpg "/>

This article is from the "Love On Action" blog, please be sure to keep this source http://1055745601.blog.51cto.com/5003160/1659235

Shell Script (iii)

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.