Shell三剑客:awk(格式化输出)

发布时间:2023年12月22日

一、格式符?

%d?

十进制有符号整数

%u?

十进制无符号整数

%f?

浮点数

%s?

字符串

%c?

单个字符

%p?

指针的值

%e?

指数形式的浮点数

%x?

%X?无符号以十六进制表示的整数

%o?

无符号以八进制表示的整数

%g?

自动选择合适的表示法

%

%?

显示%自身

#

[.#]?

第一个数字控制显示的宽度;第二个#表示小数点后精度,%3.1f

-?

左对齐(默认右对齐);%-15s,就是以左对齐方式显示15个字符长度

+?

显示数值的正负符号?%+d

二、区别

  • ????????printf 时默认是不会换行的
  • ????????print 函数默认会在每行后面加上 \n 换行符?

? ? ? ? print函数:

[root@localhost ~]# awk '{print "hello,awk"}'

hello,awk

hello,awk
;
hello,awk
^C
[root@localhost ~]# awk -F: '{print}' passwd.txt 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin10
[root@localhost ~]# awk -F: '{print "root"}' passwd.txt 
root
root
root
root
root
root
root
root
root
root
[root@localhost ~]# awk -F: '{print $1}' passwd.txt 
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
[root@localhost ~]# awk -F: '{print $0}' passwd.txt 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin10
[root@localhost ~]# awk -F: '{print $1"\t"$3}' passwd.txt 
root    0
bin     1
daemon  2
adm     3
lp      4
sync    5
shutdown        6
halt    7
mail    8
operator        11
[root@localhost ~]# data | awk '{print "Month: "$2"\nYear:"$NF}'
-bash: data: 未找到命令
[root@localhost ~]# date | awk '{print "Month: "$2"\nYear:"$NF}'
Month: 12月
Year:CST
[root@localhost ~]# awk -F: '{print "username is: " $1 "\t uid is: " $3}' passwd.txt 
username is: root        uid is: 0
username is: bin         uid is: 1
username is: daemon      uid is: 2
username is: adm         uid is: 3
username is: lp  uid is: 4
username is: sync        uid is: 5
username is: shutdown    uid is: 6
username is: halt        uid is: 7
username is: mail        uid is: 8
username is: operator    uid is: 11
[root@localhost ~]# awk -F: '{print "\tusername and uid: " $1,$3 "!"}'

        username and uid:  !

        username and uid:  !

        username and uid:  !

        username and uid:  !
^C
[root@localhost ~]# tail -3 passwd.txt | awk '{print $1,$3}'
halt:x:7:0:halt:/sbin:/sbin/halt 
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 
operator:x:11:0:operator:/root:/sbin/nologin10 

?

?

?

? ? ? ? printf函数:

[root@localhost ~]# awk -F: '{printf "%-15s %-10s %-15s\n", $1,$2,$3}' passwd.txt 
root            x          0              
bin             x          1              
daemon          x          2              
adm             x          3              
lp              x          4              
sync            x          5              
shutdown        x          6              
halt            x          7              
mail            x          8              
operator        x          11             
[root@localhost ~]# awk -F: '{printf "|%-15s| |%-10s| |%-15s|\n", $1,$2,$3}' passwd.txt 
|root           | |x         | |0              |
|bin            | |x         | |1              |
|daemon         | |x         | |2              |
|adm            | |x         | |3              |
|lp             | |x         | |4              |
|sync           | |x         | |5              |
|shutdown       | |x         | |6              |
|halt           | |x         | |7              |
|mail           | |x         | |8              |
|operator       | |x         | |11             |
[root@localhost ~]# awk -F: '{printf "%s\n",$1}' passwd.txt 
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
[root@localhost ~]# awk -F: '{printf "%-20s %10d\n", $1,$3}' passwd.txt 
root                          0
bin                           1
daemon                        2
adm                           3
lp                            4
sync                          5
shutdown                      6
halt                          7
mail                          8
operator                     11
[root@localhost ~]# awk -F: '{printf "username: %s\n", $1}' passwd.txt 
username: root
username: bin
username: daemon
username: adm
username: lp
username: sync
username: shutdown
username: halt
username: mail
username: operator
[root@localhost ~]# awk -F: '{printf "username: %s,UID: %d\n", $1,$3}' passwd.txt 
username: root,UID: 0
username: bin,UID: 1
username: daemon,UID: 2
username: adm,UID: 3
username: lp,UID: 4
username: sync,UID: 5
username: shutdown,UID: 6
username: halt,UID: 7
username: mail,UID: 8
username: operator,UID: 11
[root@localhost ~]# awk -F: '{printf "username: %15s,UID: %d\n", $1,$3}' passwd.txt 
username:            root,UID: 0
username:             bin,UID: 1
username:          daemon,UID: 2
username:             adm,UID: 3
username:              lp,UID: 4
username:            sync,UID: 5
username:        shutdown,UID: 6
username:            halt,UID: 7
username:            mail,UID: 8
username:        operator,UID: 11
[root@localhost ~]# awk -F: '{printf "username: %-15s,UID: %d\n", $1,$3}' passwd.txt 
username: root           ,UID: 0
username: bin            ,UID: 1
username: daemon         ,UID: 2
username: adm            ,UID: 3
username: lp             ,UID: 4
username: sync           ,UID: 5
username: shutdown       ,UID: 6
username: halt           ,UID: 7
username: mail           ,UID: 8
username: operator       ,UID: 11
[root@localhost ~]# lsmod | awk -v FS=" " 'BEGIN{printf "%s %26s %10s\n","Mo
dule","Size","Used by"}{printf "%-20s %13d %5s %s\n",$1,$2,$3,$4}' /proc/modules
Module                       Size    Used by
ip6t_rpfilter                12595     1 -
ip6t_REJECT                  12625     2 -
nf_reject_ipv6               13717     1 ip6t_REJECT,
ipt_REJECT                   12541     2 -
nf_reject_ipv4               13373     1 ipt_REJECT,
xt_conntrack                 12760    11 -
ebtable_nat                  12807     1 -
ebtable_broute               12731     1 -
bridge                      151336     1 ebtable_broute,
stp                          12976     1 bridge,
llc                          14552     2 bridge,stp,
ip6table_nat                 12864     1 -
nf_conntrack_ipv6            18935     7 -
nf_defrag_ipv6               35104     1 nf_conntrack_ipv6,
nf_nat_ipv6                  14131     1 ip6table_nat,
ip6table_mangle              12700     1 -
ip6table_security            12710     1 -
ip6table_raw                 12683     1 -
iptable_nat                  12875     1 -
nf_conntrack_ipv4            15053     6 -
nf_defrag_ipv4               12729     1 nf_conntrack_ipv4,
nf_nat_ipv4                  14115     1 iptable_nat,
nf_nat                       26583     2 nf_nat_ipv6,nf_nat_ipv4,
iptable_mangle               12695     1 -
iptable_security             12705     1 -
iptable_raw                  12678     1 -
nf_conntrack                139264     6 xt_conntrack,nf_conntrack_ipv6,nf_nat_ipv6,nf_conntrack_ipv4,nf_nat_ipv4,nf_nat,
ip_set                       45799     0 -
nfnetlink                    14519     1 ip_set,
ebtable_filter               12827     1 -
ebtables                     35009     3 ebtable_nat,ebtable_broute,ebtable_filter,
ip6table_filter              12815     1 -
ip6_tables                   26912     5 ip6table_nat,ip6table_mangle,ip6table_security,ip6table_raw,ip6table_filter,
iptable_filter               12810     1 -
vmw_vsock_vmci_transport         30577     1 -
vsock                        36367     2 vmw_vsock_vmci_transport,
iosf_mbi                     15582     0 -
crc32_pclmul                 13133     0 -
ppdev                        17671     0 -
ghash_clmulni_intel          13273     0 -
btusb                        41520     0 -
btrtl                        12945     1 btusb,
btbcm                        14040     1 btusb,
btintel                      15709     1 btusb,
aesni_intel                 189456     0 -
lrw                          13286     1 aesni_intel,
gf128mul                     15139     1 lrw,
bluetooth                   548688     5 btusb,btrtl,btbcm,btintel,
vmw_balloon                  18094     0 -
glue_helper                  13990     1 aesni_intel,
ablk_helper                  13597     1 aesni_intel,
cryptd                       21190     3 ghash_clmulni_intel,aesni_intel,ablk_helper,
pcspkr                       12718     0 -
joydev                       17389     0 -
sg                           40719     0 -
rfkill                       22391     1 bluetooth,
vmw_vmci                     67168     1 vmw_vsock_vmci_transport,
i2c_piix4                    22401     0 -
parport_pc                   28205     0 -
parport                      46395     2 ppdev,parport_pc,
ip_tables                    27126     5 iptable_nat,iptable_mangle,iptable_security,iptable_raw,iptable_filter,
xfs                         997727     2 -
libcrc32c                    12644     3 nf_nat,nf_conntrack,xfs,
sr_mod                       22416     0 -
cdrom                        42600     1 sr_mod,
ata_generic                  12923     0 -
pata_acpi                    13053     0 -
vmwgfx                      291993     1 -
sd_mod                       46281     3 -
crc_t10dif                   12912     1 sd_mod,
crct10dif_generic            12647     0 -
drm_kms_helper              186531     1 vmwgfx,
syscopyarea                  12529     1 drm_kms_helper,
sysfillrect                  12701     1 drm_kms_helper,
sysimgblt                    12640     1 drm_kms_helper,
fb_sys_fops                  12703     1 drm_kms_helper,
ttm                          96673     1 vmwgfx,
crct10dif_pclmul             14307     1 -
crct10dif_common             12595     3 crc_t10dif,crct10dif_generic,crct10dif_pclmul,
crc32c_intel                 22094     1 -
drm                         456166     4 vmwgfx,drm_kms_helper,ttm,
ata_piix                     35052     0 -
mptspi                       22673     2 -
e1000                       137624     0 -
nfit                         55639     0 -
libata                      243094     3 ata_generic,pata_acpi,ata_piix,
scsi_transport_spi           30732     1 mptspi,
serio_raw                    13434     0 -
mptscsih                     40150     1 mptspi,
libnvdimm                   159524     1 nfit,
mptbase                     106036     2 mptspi,mptscsih,
drm_panel_orientation_quirks         17180     1 drm,
dm_mirror                    22289     0 -
dm_region_hash               20813     1 dm_mirror,
dm_log                       18411     2 dm_mirror,dm_region_hash,
dm_mod                      124501     8 dm_mirror,dm_log,
fuse                        100350     1 -

?

?

?

?

?

?

文章来源:https://blog.csdn.net/weixin_73783595/article/details/135162244
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。