awk是什么
awk命令是一个强大的文本处理工具,用于在Unix和Linux系统中对文本进行模式扫描和处理。它逐行读取文本,默认以空格或制表符为分隔符进行字段分割,并将分隔后的字段保存到内建变量中,可以根据模式或条件执行编辑命令,使得用户可以进行复杂和强大的文本操作。
awk命令如何用
awk [参数] [处理内容] [操作对象]
以下内容来自awk的man手册。
THE AWK LANGUAGE
-
Program structure
An AWK program is a sequence of pattern {action} pairs and user function definitions.A pattern can be:
BEGIN
END
expression
expression , expressionOne, 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统计的脚本。
方法:
- 利用nm命令把目标文件的各个函数符合、全局变量进行排序,输出到map文本。
- 利用cat+grep命令过滤出需要诊断的符号。
- 利用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