➜ testawk'END {print NR}'data2.txt4#实例2:先输出行号,再输出本行有几个数据字段,再输出本行,最后输出文件名 ➜ testawk'{print NR,NF,$0} END {print FILENAME}'data2.txt16line1:This is the header line1.27line2:This is the first data line2.37line3:This is the second data line3.46...
$ awk -F : '{if($3 == 0){print $1"是超级用户";num1++;}else if($3>1 && $3 <1000){print $1"是系统用户";num2++;}else{print $1 "是普通用户";num3++;}}END{print"超级用户有:"num1"系统用户有:"num2"普通用户有:"num3}' passwd root是超级用户 bin是普通用户 daemon是系统用户...
echo -e "A line 1\nA line 2" | awk 'BEGIN{ print "Start" } { print } END{ print "End" }' Start A line 1 A line 2 End 当使用不带参数的print时,它就打印当前行,当print的参数是以逗号进行分隔时,打印时则以空格作为定界符。在awk的print语句块中双引号是被当作拼接符使用,例如: echo |...
举例: awk ‘($1 < 10 ) && ($2 > 10) {print “ok”}’ input_file awk ‘/^d/ || /x$/ {print “ok”}’ input_file ④ 其它表达式用作awk_script,如赋值表达式等 eg: awk ‘(tot+=$6); END{print “total points :” tot }’ input_file // 分号不能省略 awk ‘tot+=$6 {pri...
# tail -n3 /etc/services |awk -F'[ /]+' '{print $2}' 48619 48619 49000 []元字符的意思是符号其中任意一个字符,也就是说每遇到一个/或#时就分隔一个字段,当用多个分隔符时,就能更方面处理字段了。 3)变量赋值 # awk-v a=123 'BEGIN{print a}' ...
print n } 并执行下列命令 : awk -f count.awk 执行结果将会印出目前在线人数 [说明 : ] awk 程序并不一定要处理数据文件. 以本例而言, 仅输入程序文件count.awk, 未输入任何数据文件. BEGIN 和 END 同为awk中的一种 Pattern. 以 BEGIN 为 Pattern的Actions ,只有在awk开始执行程序,尚未开启任何输入文件...
echo"line\n"|awk'BEGIN{i='a12b34'} {i=i+9; print i}'#9echo"line\n"|awk'BEGIN{i='12b34'} {i=i+9; print i}'#21 打印文件的行数 awk'END{print NR}'access.log 打印第10行的内容 awk'NR==10 {print $0}'access.log 或者 ...
awk '/^d/ || /x$/ {print "ok"}' input_file 4) 其它表达式用作awk_script,如赋值表达式等 举例: awk '(tot+=$6); END{print "total points :" tot }' input_file// 分号不能省略 awk 'tot+=$6 {print $0} END{print "total points :" tot }' input_file // 与上面等效 ...
BEGIN { print "The number of times tecmint.com appears in the file is:" ; } /^tecmint.com/ { counter+=1 ; } END { printf "%s\n", counter ; } ' $file After making the changes to theAwkcommand, the complete shell script now looks like this: ...
awk '{print NR}' /etc/passwd :读取/etc/passwd的每一行行号 (NR至今的读取记录数) awk 'END{print NR}' /etc/passwd : 加了个END,读最后一个读取记录数的数字! 输出结果:29 表示29个用户 awk '/root/' /etc/passwd :输出 /etc/passwd里包含 root 这个字眼的 记录!打印出来 。'/ /'里表示的是...