linux command awk

 

A, ls -l | grep ^ d 

 

Two, shell awk powerful file search analysis tool of choice Brush

Reference: https://www.cnblogs.com/chenhuan001/p/6297615.html

1,  the object awk '{pattern + action}' {filenames} action content command pattern looking to find filenames

Although the operation can be complex, but the syntax is always the case in which pattern represents the content AWK find in the data, and the action is a series of commands when a match is found executed . Curly braces ({}) need not always appear in the program, they are used to group a series of instructions according to a particular pattern. pattern is a positive expression to be represented, with slash marks.

The most basic function of awk language is a browser-based rules specified in the document or string and extract information, the awk to extract information in order to carry out other text manipulation. Complete awk scripts are often used information in a formatted text file.

Usually, awk is a behavior file processing units. awk each line of the document received, and executing the first command to process text.

There are three ways to call awk

  1.  
    1 . Command line
  2.  
    awk [-F field-separator] 'commands' input- file(s)
  3.  
    Wherein, commands are really awk command, [- F field delimiter] is optional. input- File (S) is a file to be processed.
  4.  
    In awk, each line file, separated by a field separator for each called a domain. Typically, in the case where the unnamed -F field delimiter, the default field separator is a space.
  5.  
     
  6.  
    2 .shell scripted
  7.  
    All of the awk command to insert a file, and the executable program awk, awk command interpreter and then as the first line of the script, again invoked by typing the name of the script.
  8.  
    Equivalent to shell script the first line:! # / Bin / sh
  9.  
    Can be replaced with: # / bin / awk!
  10.  
     
  11.  
    3 . All of the awk command to insert a separate file, and then call:
  12.  
    awk -f awk-script- file input- file(s)
  13.  
    Which, -f option to load-script- awk File in awk script, input- File (S) with the above it is the same.

Example:

Show / etc / passwd account

 

2, # cat / etc / passwd | awk -F ':' '{print $ 1}' awk workflow

awk工作流程是这样的:读入有'\n'换行符分割的一条记录,然后将记录按指定的  域分隔符  -F指定域分隔符为':'   (单词域的缩写)。划分域,填充域,$0则表示所有域,$1表示第一个域,$n表示第n个域默认域分隔符是"空白键" 或 "[tab]键",所以$1表示登录用户 

这种是awk+action的示例,每行都会执行action{print $1}。  

 

如果只是显示/etc/passwd的账户和账户对应的shell,而账户与shell之间以tab键分割

 

3、#awk -F: '/root/' /etc/passwd  搜索/etc/passwd有root关键字的所有行

 

这种是pattern的使用示例,匹配了pattern(这里是root)的行才会执行action(没有指定action,默认输出每行的内容)。

 

4、shell编程统计某个文件夹下的文件占用的字节数,过滤4096大小的文件(if语句):

 

  1.  
    ls -l |awk ' BEGIN { size= 0;print "[ start] size is ", size} {if($5!=4096){size=size+$5;}} END{print "[ end] size is ", size/1024/1024,"M "}'
  2.  
    [end]size is  8.22339 M

awk中的条件语句 if 是从C语言中借鉴来的: 

  1.  
    if (expression) {
  2.  
    statement;
  3.  
    statement;
  4.  
    ... ...
  5.  
    }
  6.  
     
  7.  
    if (expression) {
  8.  
    statement;
  9.  
    } else {
  10.  
    statement2;
  11.  
    }
  12.  
     
  13.  
    if (expression) {
  14.  
    statement1;
  15.  
    } else if (expression1) {
  16.  
    statement2;
  17.  
    } else {
  18.  
    statement3;
  19.  
    }

5、 awk编程

这里没有初始化count,虽然默认是0,但是妥当的做法还是初始化为0:

 

以M为单位无非就是除于1024再除于1024 

 

  1.  
    if (expression) {
  2.  
    statement;
  3.  
    statement;
  4.  
    ... ...
  5.  
    }
  6.  
     
  7.  
    if (expression) {
  8.  
    statement;
  9.  
    } else {
  10.  
    statement2;
  11.  
    }
  12.  
     
  13.  
    if (expression) {
  14.  
    statement1;
  15.  
    } else if (expression1) {
  16.  
    statement2;
  17.  
    } else {
  18.  
    statement3;
  19.  
    }

显示/etc/passwd的账户

  1.  
    awk - F ':' ' BEGIN { count= 0;} {name[ count] = $ 1; count++;}; END{ for (i = 0; i < NR; i++) print i, name[i]}' /etc/passwd
  2.  
    0 root
  3.  
    1 daemon
  4.  
    2 bin
  5.  
    3 sys
  6.  
    4 sync
  7.  
    5 games
  8.  
    ......

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/shenLong1356/p/11299665.html