Shell Script (iv)

Source: Internet
Author: User


Shell Script (iv)

(7) TR command

#实现字符转换功能

#-c string: Inverse string character set, that is, all character sets except for the string

#-d string: Delete all occurrences of characters in string

#-s: Deletes all occurrences of a sequence of characters, leaving only one


#删除所有数字

Tr-d "[[:d Igit:]]" < name.txt

Tr-d 0-9 < Name.txt

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

#所有小写字母转换成大写字母

TR "[A-Z]" "[A-Z]" < name.txt

TR "[[: Lower:]]" "[[: Upper:]]" < name.txt

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

#-c usage, replaces all non-26 letters with newline characters, and repeated newline characters are compressed into 1 newline characters

Tr-cs "[A-z][a-z]" "[\ n]" < Name.txt

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

#支持使用字符 * Digital mode, such as N*4 for n appears 4 times


#-s is a continuous repetition of letters, numbers compressed into 1

Tr-s "[a-z][a-z],[[:d Igit:]]" < num.txt

#结果同1行的test, T and T are not duplicates.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/46/wKiom1V325GgL8wDAAEJKtj-REc150.jpg "/>

3. Regular Expressions

(1) match: "*", "^", ".", "$", etc.

"*": 0 or more ordinary characters before the * character

".": matches any character

"^": matches the beginning of the line, placed in the [] character set to indicate the inverse

"$": Match line end

"[]": Match character Set

"\": Escape character, shielding the special meaning of a meta-character

"\<\>": Exact match symbol

"\{n\}": matches the preceding character n times

"\{n,\}": matches the preceding character at least n times

"\{n,m\}": Matches the previous character appears n-m times

(2) Extension: "?", "+", "()", "|"

"?": Match 0 or 1 ordinary characters before the? character

"+": match 1 or more of the ordinary characters before the + character

"()": Represents a character set

"|": means "or" to match an optional set of characters

(3) grep command

#支持正则表达式的多用途文本搜索工具

#-c: Outputs only the number of matching rows;-I: Ignores case when searching;-H: Does not display filenames when querying multiple files;

#-l: Lists only matching file names and does not list specific matching lines;-N: Lists the matches and line numbers;

#-s: does not display error messages for nonexistent or no matching files;-V: Displays all lines that do not contain matching text;

#-w: Match thing word;-x: Match the row;-r: Recursive query, search subdirectory;-Q: Suppresses output of any result to exit

#状态来表示搜索是否成功; b: Prints the offset of the header of the matching leading file, in bytes;

#-o: Used in conjunction with-B to print the offset of the matching word from the file header, in bytes;-e: Using extended expressions

#-f: Do not use regular expressions to search the literal meaning of a string


#需要特别注意的是, the number of occurrences refers to successive occurrences, not the total number of occurrences!!!


# "*" usage, the letter M appears at least 0 times

grep "Nam*3" Name.txt

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

#"." Usage, search for "na any character 3"

grep "Na.3" Name.txt

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

#搜索以A开头的行, and lose the travel number

Grep-n ^a A.txt

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

# "$" usage, search for lines ending with 10

grep 10$ Name.txt

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

# "[]" usage, search for an arbitrary lowercase letter followed by an arbitrary number of lines

grep [a-z][0-9] Name.txt

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

# "\" usage, search for lines beginning with *

grep ^\* Name.txt

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

# "\<\>" usage, search for rows that appear Name3

grep "\<name3\>" Bkname.txt

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

# "\<n,\>" usage, search number 0 appears at least 5 times

grep "0\{5,}" Bkname.txt

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

# "\<n,m\>" usage, search number 7 appears 2 times to 3 rows

grep "7\{2,3\}" Name.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/46/wKiom1V325OzffxVAACot_R-xe8406.jpg "/>

# "?", "+", "()" and "|" is a regular expression extension, you need to use the-e parameter

#"?" Usage, search for lines that contain nu or num

Grep-e "num?" Bkname.txt

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

# "+" usage, search for a line with the letter T appearing at least 1 times

Grep-e "t+" Bkname.txt

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

# "()" and "|" Use combination to search for lines that contain nu or num

Grep-e "(nu|num)" Bkname.txt

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

#多文件搜索包含name1的行, and lose the travel number

grep name1 Name.txt Num.txt

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

#显示匹配行前后2行

Grep-2 name2 Name.txt

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

#显示匹配行的

GREP-A2 name2 Name.txt

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

#-c: Usage, counting blank lines

Grep-c ^$/etc/httpd/conf/httpd.conf/

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

#-i: Ignoring case

Grep-i name Bkname.txt

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

#-h: Multiple file queries do not display file names

Grep-h Test Bkname.txt Num.txt

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/42/wKioL1V33TuQe8P0AADl-Ax1IQU529.jpg "/>

#-n: Display matching lines and line numbers

Grep-n Test Bkname.txt

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

#-w: the string to be searched must be a complete word;

#-x: Displays fully compliant rows, and the searched string must be a separate line

Grep-w Test Bkname.txt

Grep-x Test Bkname.txt

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

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

Shell Script (iv)

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.