Linux / Centos regular expressions

Regular expressions, regular expressions, also known as conventional notation (Regular Expression, the code is often abbreviated as regex, regexp or RE), a concept in computer science. Regular expression to describe the use of a single string, the string line with a series of matching syntax rules. In many text editor, a regular expression is often used to retrieve, replace the text in line with those of a model. Regular expressions are divided into basic regular expressions and extended regular expressions.

Cloud character refers to those special characters have special significance in the regular expression
Linux / Centos regular expressions
1, the filter / etc / passwd or to S at the beginning of the line s

grep "^[sS]" /etc/passwd

2, the filter / etc / passwd to the end of the row nologin

grep "nologin$" /etc/passwd

3, filter / etc / passwd to space at the beginning of the line

grep "^ " /etc/passwd

4, the filter / etc / passwd blank lines in

grep "^$" /etc/passwd

5, the filter / etc / passwd to the beginning of the row r, r may be the back or any number with 0 o (that is the first character must be a line r, this line can also be zero or any number of o)

grep "^ro*" /etc/passwd

6, filter / etc / passwd to the beginning of the line r, r behind must be followed by one or more than one character (this line is the first character must be r, and that this line there are other characters, spaces also count characters)

grep "^r." /etc/passwd

7, the filter / etc / passwd followed by an arbitrary character in the r, h, any character followed by a line (i.e. the line have to have r and h, h and r must follow a)

grep "r.*h" /etc/passwd

8, filter / etc / passwd, the line containing the root of the word, and not root and other neighboring character (not a space)

grep "\<root\>" /etc/passwd

9, the filter / etc / passwd in row r oo followed by zero or 1 occurrence of this

grep "ro\?" /etc/passwd

10, filtering / etc / passwd in, followed by the R & lt o, o 2 times line appears only

grep "ro{2}" /etc/passwd

11, filtering / etc / passwd in, followed by the R & lt o, o appears at least once up to 2 times OK

grep "ro{1,2}" /etc/passwd

12, filtering / etc / passwd in, followed by the R & lt o, o 2 times line appears at least

 grep "ro{2,}" /etc/passwd

tr

命令                                                含义
tr set1  set2                                    用set2 的字符替换 set1
tr -d set1                                        删除 set1 中指定的字符
tr  -s  set1                                        压缩 set1 中指定的字符
tr -s  set1  set2                                用 set2 的字符替换 set1 ,然后压缩 set2 中指定的字符
tr  -ds  set1  set2                           先删除 set1 的字符,再压缩 set1 中的字符!

grep: filter out the contents of the file, folder grep can not filter keywords / etc / passwd

         Invert -v
         -c total number of lines containing keyword
         -n keyword display the number of lines in the file
         -i ignore the case of search terms when searching for
         the keyword is displayed simultaneously when -AX X is a number that represents the search X line
         -BX X is a number that simultaneously displays the first line of X keyword search
different diff passwd passwd2 compare two files

Herein represents the passwd file and file passwd2 line 2 different rows 4, 5 and 7 rows, respectively, and print out the file 1 and the file 2, 4 3, 5, 7 line
Linux / Centos regular expressions
diff passwd passwd2> diff.path the and passwd2 different passwd file is written to diff.path

patch  -b  passwd2  diff.path                        用 diff.path文件修改 passwd2 中的不同,最后得到的 passwd2 文件和 passwd 文件一模一样

du : 查看目录或文件占用大小

         不加参数时,显示当前文件夹下的所有文件夹的磁盘使用情况(包括递归文件夹内的文件夹)
         -a  给指定文件夹内的所有文件显示磁盘使用情况( 包括层层递归文件 )
         -s  仅显示总计,即当前目录的大小
         -h 以人性化方式显示
         du -sh    /tmp/test     或者      du -sh  /tmp/test/                      显示test文件夹的占用大小
wc : 统计文件中的数据 

        -  l 统计行数
        - c 统计字节数
      - w 统计字数
        - m 统计字符数
统计文件夹内文件的个数:         ls -l | grep "^-" | wc -l
统计文件夹内包括递归文件的个数:   ls -lR | grep "^-" | wc -l
统计文件夹内文件夹的个数 :     ls -l | grep "^d" | wc -l
统计文件夹内包括递归文件夹的个数:   ls -lR | grep "^d" | wc -l

find :查找文件或文件夹

        Find find the file name / tmp -name abc Find / tmp directory under the file name is abc, it will traverse all the files and folders under the tmp directory
        to find find / tmp -size -1M + number based on file size that is greater than - No. It represents less than
       -name find the file name
       -size look through the file size
       -type find by file type
       -user user Find files by
       -perm permission to look through
        because find support on the pipeline is relatively weak, it can make use of the extended option exec
        find / etc -size + 1M -exec ls -lh {} \ ; {} on behalf of the content to find find
        find / etc -size + 1M -exec cp {} / tmp / test / \;
If you are interested or have questions to ask, please Add: 15149813470, free answer.

Guess you like

Origin blog.51cto.com/11233498/2405129