awk

[root@mhc regular]# last -n 2|awk '{print $1 " " $3}'
root    :0
root    :0
    
wtmp    Wed

[root@mhc regular]# last -n 2|awk '{print $1 " lines:" NR " columes:" NF}'
root    lines:1     columes:10
root    lines:2     columes:10
    lines:3     columes:0
wtmp    lines:4     columes:7

[root@mhc regular]# cat /etc/passwd | awk '{FS=":"} $3 < 10 {print $1 " " $3}'
root:x:0:0:root:/root:/bin/bash    
bin    1
daemon    2
adm    3
lp    4
sync    5
shutdown    6
halt    7
mail    8

[root@mhc regular]# cat /etc/passwd | awk '{FS=":"} NR==2 {print $1 " " $3}'
bin    1

[root@mhc regular]# cat /etc/passwd | awk 'BEGIN {FS=":"} $3 < 10 {print $1 " " $3}'
root    0
bin    1
daemon    2
adm    3
lp    4
sync    5
shutdown    6
halt    7
mail    8

[root@mhc regular]# cat pay.txt
Name    1st    2nd
aa    200    300
bb    250    550

[root@mhc regular]# cat pay.txt | awk 'NR==1{printf "%10s %10s %10s %10s ", $1, $2, $3, "Total"} NR>=2{total = $2 + $3
> printf "%10s %10d %10d %10.2f ", $1, $2, $3, total}'
      Name        1st        2nd      Total
        aa        200        300     500.00
        bb        250        550     800.00

[root@mhc regular]# cat a.sh
#!/bin/bash

cat pay.txt | awk 'NR==1{printf "%10s %10s %10s %10s ", $1, $2, $3, "Total"} NR>=2{total = $2 + $3
printf "%10s %10d %10d %10.2f ", $1, $2, $3, total}'
[root@mhc regular]#
[root@mhc regular]#
[root@mhc regular]# ./a.sh
      Name        1st        2nd      Total
        aa        200        300     500.00
        bb        250        550     800.00

原文地址:https://www.cnblogs.com/mhc-fly/p/8318528.html