Linux restudying Seven

Source: Internet
Author: User

Text processing, regular expressions, cut, grep, Egrep, Fgrep


Linux Chinese this processing tool is most commonly used in the text Processing Three Musketeers grep, SED, awk and the regular expression, you can achieve enough text processing functions. The power of the tool is because the people who use it, how can they play the role of a text processing tool? The answer is the regular expression, in fact the regular expression, is just a thought, a representation method, as long as we use the tool to support the expression of this idea, then this tool can handle the string of regular expressions. This part of the content can be said to learn before the shell script must learn, the more profound understanding of the shell scripting ability will be more powerful.



Text Processing


Text Viewing Tools

Cat-n (contains the $ carriage return),-b,-a (-VTE),-E (-ve),-V (with ^m no ^t),-s,-e (carriage return)

Tac-n,-b,-a (-VTE),-E (-ve),-V (with ^m without ^t),-S,-E (carriage return)

Rev

MORE-D (hint), can only be turned upside down, can command

Less PgUp page, can search, can command

Head-n first few lines, default 10 rows

Tail-n after a few lines, default 10 lines,-f/var/log/messages front-end monitoring log, tail-n 0-f

/var/log/message & Back-end display new log, without affecting use, FG 1 switch foreground, CTRL c terminates

Cut

cut-d:-F1-5,6,7/ETC/PASSWD #: Number Intercept, section –output-delimiter=+ # Replace Delimiter

cut-c44-46 file #字符截取, monitoring disk

Paste horizontal merging of two files

Paste-d: #指定横向分隔的符号, default tab

Paste-s #一个文件的内容合并成一行显示

WC (word count) #字符收集统计数据

WC file #行, Word, byte

Wc-m #统计字符数

Wc-l #统计行数

Wc-w #统计以标点或space隔开的单词数

Wc-c #统计字节数

WC input character, last input space to open the statistical character, and then ctrl D

Sort #文本排序

Sort-t '. ' #使用. As a field delimiter

Sort-r #反序

Sort-f #忽略字符串中的大小写

Sort-n #按数字排序

Sort-u #去除重复行

Sort-k3 #按. Separators are organized according to the 3rd column and can be repeated multiple times

Sort-ut '. '-k3 #按. Separators are sorted and categorized according to the 3rd column, removing rows that do not belong to the class

Uniq #从输入中山城重复的前后相接的行

Uniq file #合并相邻的重复行

Uniq-c file #显示合并的重复次数

uniq-d file #只显示重复的行

Uniq-u file #显示不重复的行

Diff #比较两个文件之间的区别

diff xx yy

Diff-c xx yy

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/89/AE/wKioL1gZwvKAy49EAABoomIgjc4886.png-wh_500x0-wm_3 -wmp_4-s_2740770115.png "title=" 1.png "alt=" Wkiol1gzwvkay49eaaboomigjc4886.png-wh_50 "/>

Where 1-3 means that the line number C is different, the usual a represents the extra rows, d means the deleted rows, that is, the fewer rows,< represents the first file,> represents the second file: Diff-c will show the difference between the two files in the form above, the file time will be compared, different places in the line will be added before this line "!":

Diff-u #选项来输出 "Unified (Unified)" diff format file, best for patch files

Patch #复制对文件改变

Patch-b #自动备份改变了的文

Diff-u xx yy>zz.patch

Patch-b xx zz.patch

Grep:global serarch REgular expressin and print out the line

GREP-E = Egrep, grep-f fgrep (regular expression not supported)

. BASHRC Join alias grep= ' Grep–color=auto '

grep xxxx/etc/passwd/etc/group .... With color on the CENTOS7

grep ' $USER '/' $USER '/whoami/etc/passwd

Grep-v string #显示不包含string的行, excluding

Grep-i String #忽略大小写

Grep-n string #显示包含string的行号

Grep-c string #统计string的行数

Grep-o strng #仅显示string, with regular expression, exact match

Grep-q string #静默模式, echo $? Look at the return value.

Grep-a3 string #显示string与string后面 (after) 3 lines, which can be used as a string-assisted filter,

GREP-C3 #string前后的3行

Grep-e ' root '-e ' sbin ' file #实现多个选项的逻辑或关系, similar to ' Root|sbin ' in regular

Grep-w string #完全匹配string, matches a word, is delimited by a punctuation mark or a Space tab, similar to the \bstring\b or \<string\> in a regular, and has the same effect, but this is a different concept that must be differentiated

GREP-E = Egrep #使用扩展的正则表达式

Standard Regular Expressions

A pattern written by a class of special characters and text characters that have control or matching functions

Man 7 regex #看正则表达式

The program supports grep, VIM, less, nginx

Number of Matches

grep r.t #匹配任意单个字符

grep r[a-z]t #匹配任一指定范围内的任一个字符, such as [ABc], that matches a or B or C

grep r[^[1-7]]t #不在指定范围内的任一个字符

[: Alpha:], [:d igital:], [: Alnum:], [:p UNCT:], [: Space:], [: Lower:], [: Upper:]

\* # * Before the character repeats any number of times, as long as possible match

. * # any length of characters \? #前面的字符0次或1次, there will be no match.

\+ #前面的字符至少1次

\{n\} #n次

\{m,n\} #至少m次, up to N times

\{,n\} #至多n次

\{m,\} #至少m次

Position anchoring

^ #句子的行首锚定

$ #句子的行尾锚定

^\$ empty line anchor, with Cat-a to see the number for line break, ^i means tab

Grep-v "^ *\$" #不显示以空格开头的空行

Grep-v "^[[:space:]]*$" #不显示以空格和Tab开头的空行

grep ' \bstr ' or ' \<str ' #str开头单词的词首锚定

grep ' st\b ' or ' \>str ' #str结尾单词的词尾锚定

\<string\> #匹配整个单词

Group

\ (\): Bind one or more characters together as a whole, such as: (root) + the contents of the pattern match in the grouping brackets are recorded in the internal variables by the regular expression engine, the variables are named: \1, \2, \3, ...

\1: The character that matches the pattern between the first opening parenthesis and the matching closing parenthesis, starting from the left

Example ': (string1+ (string2) *)

\1:string1+ (string2) *

\2:string2

Back reference: References the pattern in the preceding grouping brackets to match the character (not the pattern itself)

Logical OR

' (Root|bash) ' #匹配root或bash

' \ (ba|c\) SH ' #匹配bash或csh

Egrep and extended regular expressions, omitting escape characters

Egrep = Grep-e #可使用扩展的正则表达式

Character matching:. [] [^]

Number of matches: *? + {n} {m,n}

Location Anchor: ^ $ \< \> \b

Group: (') Back reference: \1,\2 ...

Logical OR: (A|B) (a|b) C

The difference between * and *: * matches 0 or more times, repeated content may not appear; + appears at least once as ca+t matches cat and Caaat, but does not match CT

Note: Do not add escape characters for the above symbols when using GREP-E or Egrep


Linux restudying Seven

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.