利用awk命令统计code size

本文介绍了awk命令的基本概念及其强大功能,包括模式匹配、文本处理流程和语法结构等,并通过多个实例展示了如何使用awk进行复杂的文本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

awk是什么

awk命令是一个强大的文本处理工具,用于在Unix和Linux系统中对文本进行模式扫描和处理。它逐行读取文本,默认以空格或制表符为分隔符进行字段分割,并将分隔后的字段保存到内建变量中,可以根据模式或条件执行编辑命令,使得用户可以进行复杂和强大的文本操作。

awk命令如何用

awk [参数] [处理内容] [操作对象]
以下内容来自awk的man手册。
THE AWK LANGUAGE

  1. Program structure
    An AWK program is a sequence of pattern {action} pairs and user function definitions.

    A pattern can be:
    BEGIN
    END
    expression
    expression , expression

    One, but not both, of pattern {action} can be omitted. If {action} is omitted it is implicitly { print }. If pattern is omitted, then it is implicitly matched. BEGIN
    and END patterns require an action.

    Statements are terminated by newlines, semi-colons or both. Groups of statements such as actions or loop bodies are blocked via { … } as in C. The last statement in
    a block doesn’t need a terminator. Blank lines have no meaning; an empty statement is terminated with a semi-colon. Long statements can be continued with a backslash,
    . A statement can be broken without a backslash after a comma, left brace, &&, ||, do, else, the right parenthesis of an if, while or for statement, and the right
    parenthesis of a function definition. A comment starts with # and extends to, but does not include the end of line.

    The following statements control program flow inside blocks.

      if ( expr ) statement
    
      if ( expr ) statement else statement
    
      while ( expr ) statement
    
      do statement while ( expr )
    
      for ( opt_expr ; opt_expr ; opt_expr ) statement
    
      for ( var in array ) statement
    
      continue
    
      break
    

EXAMPLES
1. emulate cat.

        { print }

   2. emulate wc.

        { chars += length($0) + 1  # add one for the \n
          words += NF
        }

        END{ print NR, words, chars }

   3. count the number of unique “real words”.

        BEGIN { FS = "[^A-Za-z]+" }

        { for(i = 1 ; i <= NF ; i++)  word[$i] = "" }

        END { delete word[""]
              for ( i in word )  cnt++
              print cnt
        }

   4. sum the second field of every record based on the first field.

        $1 ~ /credit|gain/ { sum += $2 }
        $1 ~ /debit|loss/  { sum -= $2 }

        END { print sum }

   5. sort a file, comparing as string

        { line[NR] = $0 "" }  # make sure of comparison type
                        # in case some lines look numeric

        END {  isort(line, NR)
          for(i = 1 ; i <= NR ; i++) print line[i]
        }

        #insertion sort of A[1..n]
        function isort( A, n,    i, j, hold)
        {
          for( i = 2 ; i <= n ; i++)
          {
            hold = A[j = i]
            while ( A[j-1] > hold )
            { j-- ; A[j+1] = A[j] }
            A[j] = hold
          }
          # sentinel A[0] = "" will be created if needed
        }

awk命令应用举例

利用nm命令和cat、grep、awk命令组合来实现一个code size统计的脚本。
方法:

  1. 利用nm命令把目标文件的各个函数符合、全局变量进行排序,输出到map文本。
  2. 利用cat+grep命令过滤出需要诊断的符号。
  3. 利用awk命令承接统计2中的内容。
    脚本内容如下:
if [ $# -gt 2 ];then
    echo "such as: ./code_size_summart.sh ./nuttx/vela_sensor.elf "
    exit
fi

tmp_nm_file=$1+"nm_size.txt"
arm-none-eabi-nm -S -C -t 'd' --size-sort  -r  -l -A $1 > $tmp_nm_file
sum_summary=0
if [ $# -eq 2 ] ;then
	cat $tmp_nm_file | grep -aiE "$2" | awk 'BEGIN{sum_summary=0}{print $2,$3,$4,$5,$6;sum_summary=sum_summary+$2}END{print "Total:" sum_summary;}'
else
	cat $tmp_nm_file | awk 'BEGIN{sum_summary=0}{print $2,$3,$4,$5,$6;sum_summary=sum_summary+$2}END{print "Total:" sum_summary;}'
fi
rm  $tmp_nm_file

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值