Linux Advanced command ==> find, grep, sed, awk

A, find

  find command to find the file in the specified directory. If you use this command does not set any parameters, the find command will find subdirectories and files in the current directory. And will look into all the subdirectories and files are displayed.

grammar

find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \;

Common options:

-name  find the root directory and all subdirectories to the end of the log file, the file names are case sensitive, for example: find / -name '* logf'

-iname       find the current all files named test directory and its subdirectories, file names are case insensitive, such as:. find -name test

-user    find the file belongs to the user to mysql all files, for example: find -user mysql.

-group      find the file belongs to group all files git, for example: find -group git.

-type    according to the type of look: following

  • f   file find. -type f
  • d  directory find. -type d
  • c  character device file find. -type c
  • b  block device file find. -type b
  • l   linked file find. -type l
  • p  pipe file find. -type p

-size    depending on the file size of the query

  • -n  is smaller than the  size  n  of the file
  • + n  is larger than  the size  n  of the file
  • Find / directory under ect, less than 10,000 bytes of the file. find / etc -size + 10000c
  • Find the / etc directory, files larger than 1M. find / etc -size -1M

-mtime    find the file changes according to time

  • -n n days within the modified file.
  • N + n days other than the modified file.
  • n exactly n days to modify file
  • Under Query / etc directory, and to modify the end of the conf file within 5 days. find / etc -mtime -5 -name '* .conf'
  • Query / etc directory, 10 days before the modification, and belong to yangyang file. find / etc -mtime +10 -user yangyang

-mmin  

  • -n  modified files within n minutes
  • + n  n minutes ago modified file.
  • Revised 30 minutes before the next query file / etc directory. find / etc -mmin +30
  • Query / etc directory modified 30 minutes before directory. find / etc -mmin -30 -type d

-mindepth n   from the n-th stage to start the search directory

  • Start searching from / etc subdirectory of the third stage. find / etc -mindepth 3

-maxdepth n   represents up to search for the first stage n-1 subdirectory.

  • Searching for qualifying files in / etc, but the most searched Class 2 subdirectory. find / etc -maxdepth 3 -name '* .conf'
  • find /etc -type f -name '*.conf' -size +10k -maxdepthc 2

Operation to find the file:

-print   print output. The default option, print out the results found.

-exec   perform specific operations to search for a file, a fixed format: -exec 'commond' {} \    ; Note: {} represents the results of the query.

  • Search / file (non-catalog) etc directory, files that end in conf, and greater than 10k, and then delete it.
    • find /etc -type f -name '*.conf' -size +10k -exec rm -f {} \;
  • The next / data / log / directory for files ending in .log, delete and change the time in more than seven days.
    • find /data/log -name '*.log' -mtime +7 -exec rm -f \;
  • Search conditions were the same as in Example 1, but not delete, just copy it to / root / conf directory
    • find /etc -type f -name '*.conf' -size +10k -exec cp {} /root/conf/ \;

-ok      and  -exec  function the same, but each operation will give the user prompt.

Logical Operators:

-a with   (default is the relationship between the conditions of inquiry)

-o or

-not |! non- 

Two, grep

  grep (global search regular expression (RE) and print out the line, comprehensive search regular expression and print out the line) is a powerful text search tool, you can use a regular expression search text, and print the matching lines come out.

grammar:

grep [-abcEFGhHilLnqrsvVwxy] [- A <shows the number of columns>] [- B <shows the number of columns>] [- C <shows the number of columns>] [- d <operates>] [- e <style templates>] [- f <templates file>] [- help] [template style] [file or directory ...]

Options:

Options description
-a  Do not ignore binary data
-A <shows the number of columns> In addition to displaying the line in line with the style of the model outside, and displays the contents after the line 
-b In that display rows that match the style of the model outside, and displays the contents of the previous line 
-c The number of columns is calculated in line with the template style 
-C <shows the number of columns> or - <Display Sequence> In addition to displaying the row in line with the style of the model outside, and displays the contents of the column before 
-d <operates> When you specify when you want to find is a directory rather than a file, you must use this parameter, otherwise the grep command will return information and stop action 
-e <style templates> Find specified string as the content of the document template style 
-E The template style for the general representation extends to the use, means that can use extended regular expressions 
-f <template file> Specify a template file, the contents of one or more template styles, so grep to find content template files that meet the conditions, the format of each column as a template style 
-F The template style seen as a list of fixed strings 
-G The template as a normal style of notation to use 
-h Before displaying the row in line with the template styles, do not indicate the file name of the column belongs 
-H Before displaying the row in line with the style of the template, the file name for the column marked 
-i Ignore character case differences 
-l Lists the contents of the documents in line with the file name specified in the template style 
-L Lists the contents of the file does not comply with the file name specified in the template style 
-n Before displaying the row in line with the style of the template, mark the number of the column 
-q Does not display any information 
-R/-r And the effect of this parameter specifies the same "-d recurse" parameter 
-s No error message 
-v Reverse lookup 
-w Show only in line with the whole-word column 
-x Column column display only full compliance with the
-Y This parameter effect with the "-i" same
-The Only the output file to match a portion

Example:

# Search for a word in the file, the command returns a "match_pattern" line of text:
 grep match_pattern file_name    
 grep  " match_pattern " file_name

# Find in multiple files
grep "match_pattern" file_1 file_2 file_3 ...

# Output all lines except the - v option
 grep -v " match_pattern " file_name

# Using regular expressions - E option to
 grep -E " [1-9] + " 
egrep  " [1-9] + "

# 只输出文件中匹配到的部分 -o 选项
grep -o "match_pattern" file_name

# 统计文件或者文本中包含匹配字符串的行数 -c 选项
grep -c "text" file_name

# 输出包含匹配字符串的行数 -n 选项
grep "text" -n file_name
cat file_name | grep "text" -n
grep "text" -n file_1 file_2

# 打印样式匹配所位于的字符或字节偏移
echo gun is not unix | grep -b -o "not"

# 搜索多个文件并查找匹配文本在哪些文件中
grep -l "text" file1 file2 file3...

# 在多级目录中对文本进行递归搜索
grep "text" . -r -n

# 忽略匹配样式中的字符大小写
echo "hello world" | grep -i "HELLO"

# 选项 -e 制动多个匹配样式
echo this is a text line | grep -e "is" -e "line" -o

#只在目录中所有的.php和.html文件中递归搜索字符"main()"
grep "main()" . -r --include *.{php,html}

#在搜索结果中排除所有README文件
grep "main()" . -r --exclude "README"

#在搜索结果中排除filelist文件列表里的文件
grep "main()" . -r --exclude-from filelist

#不会输出任何信息,如果命令运行成功返回0,失败则返回非0值。一般用于条件测试
grep -q "test" filename

#显示匹配某个结果之后的3行,使用 -A 选项:
seq 10 | grep "5" -A 3
5
6
7
8

#显示匹配某个结果之前的3行,使用 -B 选项:
seq 10 | grep "5" -B 3
2
3
4
5

#显示匹配某个结果的前三行和后三行,使用 -C 选项:
seq 10 | grep "5" -C 3
2
3
4
5
6
7
8

#如果匹配结果有多个,会用“--”作为各匹配结果之间的分隔符:
echo -e "a\nb\nc\na\nb\nc" | grep a -A 1
a
b
--
a
b
grep 示例

三、sed

sed 命令是利用脚本来处理文本文件。

sed 可依照脚本的指令来处理、编辑文本文件。

Sed 主要用来自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等。

语法:

sed [选项] [脚本命令] 文件名

选项与参数

  • -n :使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN 的数据一般都会被列出到终端上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。
  • -e :直接在命令列模式上进行 sed 的动作编辑;
  • -f :直接将 sed 的动作写在一个文件内, -f filename 则可以运行 filename 内的 sed 动作;
  • -r :sed 的动作支持的是延伸型正规表示法的语法。(默认是基础正规表示法语法)
  • -i :直接修改读取的文件内容,而不是输出到终端。

动作:

  • a :新增行, a 的后面可以是字串,而这些字串会在新的一行出现(目前的下一行)
  • c :取代行, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行
  • d :删除行,因为是删除,所以 d 后面通常不接任何参数,直接删除地址表示的行;
  • i :插入行, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
  • p :列印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行
  • s :替换,可以直接进行替换的工作,通常这个 s 的动作可以搭配正规表示法,例如 1,20s/old/new/g 一般是替换符合条件的字符串而不是整行

一般动作的前面会有一个地址的限制,例如 [地址]动作,表示我们的动作要操作的行。下面我们通过具体的例子直观的看看 sed 的使用方法。

删除行

// example.txt 内容如下
11 aa
22 bb
33 cc
23 dd
55 2e

sed '1,2d' example.txt

输出:
33 cc
23 dd
55 2e

  其中1,2d中的d表示删除,而d前面的表示删除的行的地址,而1,2表示一个地址范围,也就是删除第1行和第2行。地址范围的表示一般是  m,n 表示对m和n行之间的所有行进行操作,也包含第m行和第n行。sed的地址寻址中可以使用$表示最后一行,例如 m,$ 表示对m行以及其后面的所有行进行操作,包括最后一样。m,$d就是删除m行以及其后面的所有行内容。当然我们还可以对某一行进行操作,例如2d表示仅仅删除第2行。除了使用数字范围 m,n 表示多行区间,以及m表示单行以外,我们还可以使用正则表达式选出符合条件的行,并对这些行进行操作,同样的是上面的文件:

sed '/2/d' example.txt

输出:
11 aa
33 cc

  上面的命令中 /2/ 是一个正则表达式,在sed中正则表达式是写在 /.../ 两个斜杠中间的,这个正则的意思是寻找所有包含2的行,执行相应的操作,也就是删除所有包含2的行,如果我们只想删除以2开头的行呢,只需要修改一下正则表达式就可以了:

sed '/^2/d' example.txt

输出:
11 aa
33 cc
55 2e

新增行

sed '1a hello world' example.txt

输出:
11 aa
hello world
22 bb
33 cc
23 dd
55 2e

  其中a命令表示在指定行的后面附加一行,1a则是在第一行的后面添加一行,添加的内容就是a后面的内容,如果a的前面没有地址限定则在所有行的后面都会添加指定的字符串

sed '1i hello world' example.txt

输出:
hello world
11 aa
22 bb
33 cc
23 dd
55 2e

命令i表示在指定的行的前面插入一行,插入的内容为其后面的字符串

替换行

sed '1c hello world' example.txt

输出:
hello world
22 bb
33 cc
23 dd
55 2e

  命令c会替换指定的行的所有内容,替换成其后面的字符串,所有的新增,删除,替换行,这些命令前面的地址修饰都可以指定地址空间,也都可以使用正则表达式,命令会应用在选出的符合地址条件的所有行上面,例如:

sed '/^2/c hello world' example.txt

输出:
11 aa
hello world
33 cc
hello world
55 2e

替换以2开头的行,其内容是c命令后面的字符串 

替换部分字符串而不是整行

  sed中除了上面的命令是针对整行进行操作的之外,还提供一个替换命令,该命令对某一行中的部分字符串进行操作,下面举一个简单的例子,还是同样的文本内容,执行下面的命令:

sed 's/aa/AA/' example.txt

输出:
11 AA
22 bb
33 cc
23 dd
55 2e

  我们这里说的就是s命令,执行的结果是我们文件中的 aa 被替换成 AA ,我们看一下s命令后面接的是3个斜杠分隔的两串字符串,其含义是   s/待替换的字符串/新字符串/ 也就是说使用后面的 AA 替换文件中出现的前面的 aa 。实际上这里的替换仅仅替换每一行遇到的第一个aa,我们修改一下文件的内容:

// example.txt
11 aa
22 bb
33 cc
23 dd
55 2e
66 aaff ccaa
zz ggaa

sed 's/aa/AA/' example.txt

输出:
11 AA
22 bb
33 cc
23 dd
55 2e
66 AAff ccaa
zz ggAA

  可以看到第6行的ccaa中的aa是没有被替换的,也就是说此时仅仅替换了每一行搜索到的第一个aa字符串进行操作,那么如果要对一行里面的所有的符合条件的字符串都做替换操作呢,我们可以使用参数g,例如修改命令如下:

sed 's/aa/AA/g' example.txt

输出:
11 AA
22 bb
33 cc
23 dd
55 2e
66 AAff ccAA
zz ggAA

  在最后一个斜杠后面加上g选项之后,表示进行全局替换,也就是说一行中所有符合条件的旧字符串都会被替换成新字符串,而不仅仅是第一个。与其他针对行的操作一样,s命令也可以进行地址选择,其地址使用方法与我们之前的一样,也就是在s的前面加上地址空间限定,例如:

sed '1s/aa/AA/g' example.txt

输出:
11 AA
22 bb
33 cc
23 dd
55 2e
66 aaff ccaa
zz ggaa

可以看到仅仅对第一行进行了替换操作,其他的地址限定方法同样也是可以使用的,我们可以使用m,n的限定,例如:

sed '5,$s/aa/AA/g' example.txt

输出:
11 aa
22 bb
33 cc
23 dd
55 2e
66 AAff ccAA
zz ggAA

  表示对第5行直到文件末尾的所有行进行搜索替换操作,同样s命令的地址限定也支持使用正则表达式限定符合条件的行,然后在这些行中进行字符串的搜索替换操作,例如:

sed '/^[0-9]/s/aa/AA/g' example.txt

输出:
11 AA
22 bb
33 cc
23 dd
55 2e
66 AAff ccAA
zz ggaa

我们在s命令前面添加了 /^[0-9]/ 这个修饰,该正则表达式表示对所有以数字开头的行,执行s操作

另外一个要说明的是  s/待替换的字符串/新字符串/ 这种格式中 / 作为分隔符并不是一定的,当使用s命令时候,我们可以使用别的分隔符,实际上s后面紧接着的字符就是分隔符,所以不一定是 / 符号。例如:

echo 'aabbccaadd' | sed s#aa#AA#g

输出:
AAbbccAAdd

这里s命令后面跟着的#符号被当作分隔符了

搜索并输出行内容

sed还提供一个p命令用于搜索符合条件的行,并输出该行的内容,而不做其他的任何修改,例如:

// example.txt
11 aa
22 bb
33 cc
23 dd

sed '2p' example.txt

输出:
11 aa
22 bb
22 bb
33 cc
23 dd

  可以看到第二行被输出来了,但是sed好像将文件的所有内容输出了一遍,而第2行则多输出了一次,实际上sed默认情况下是会将所有标准输入的数据又重新输出到标准输出的,我们可以加上 -n 选项让sed仅仅是输出经过处理之后的那些行,而不是输出之前从标准输入中获取到的所有行内容,例如:

sed -n '2p' example.txt

输出:
22 bb

  这样仅仅会输出p命令的处理结果了,-n 选项一般是与p命令联合使用的,其他的增加,删除,替换行的命令是不需要 -n 选项的

将修改应用到文件当中

我们之前做的所有实验,实际上都没有修改test.txt文件的内容,也就是说我们看到的修改结果仅仅输出到控制台上,而文件test.txt的内容是没有修改的,我们可以使用 -i 选项告诉sed直接修改文件的内容,而不是将修改结果输出到终端上,例如:

sed -i '2d' example.txt 

命令运行之后,我们发现example.txt的第2行没有了

sed正则中的元字符

我们知道sed中的命令前面可以使用地址范围进行限制,表示对文件的某些符合条件的行执行相应的操作,其中我们可以使用正则表达式选出要操作的行,而sed中正则的语法可能与我们其他命令的正则语法有一些不同,这里我们有必要列出sed中常用的正则元字符:

$ 表示行尾
^ 表示行首
[a-z0-9]表示字符范围
[^]表示除了字符集中的字符以外的字符

sed的正则中  \(\)  和 \{m,n\} 需要转义 
. 表示任意字符 
* 表示零个或者多个 
\+ 一次或多次  
\? 零次或一次    
\| 表示或语法

四、awk

https://www.cnblogs.com/wangqiguo/p/5863266.html (转) 

五、grep、sed、awk总结

  • grep:适合单纯的查找或匹配文本;
  • sed:适合对匹配到的文本进行编辑;
  • awk:适合对文本进行较复杂的格式化处理;

Guess you like

Origin www.cnblogs.com/L-Test/p/11801789.html