Linux1017 shell:awk print printf
查询
[caozx26@client ~]$ cat 2.txt
root:x:0:0:root:/root:/bin/bash hello world
bin:x:1:1:bin:/bin:/sbin/nologin test1 test2
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
[caozx26@client ~]$ awk -F: 'BEGIN {OFS "\t\t";print "u_name\t\th_dir\t\tshell\nxxxxxx"} {printf "%-20s %-20s %-20s\n",$1,$(NF-1),$NF}' END{print "xxxxxxxx"} 2.txt
u_name h_dir shell
xxxxxx
awk: cmd. line:1: fatal: cannot open file `END{print' for reading (没有那个文件或目录)
[caozx26@client ~]$
[caozx26@client ~]$ awk -F: 'BEGIN {print "u_name\t\th_dir\t\tshell" RS="xxxxxxxx"} {printf %-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}' /etc/passwd
awk: cmd. line:1: BEGIN {print "u_name\t\th_dir\t\tshell" RS="xxxxxxxx"} {printf %-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: BEGIN {print "u_name\t\th_dir\t\tshell" RS="xxxxxxxx"} {printf %-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: BEGIN {print "u_name\t\th_dir\t\tshell" RS="xxxxxxxx"} {printf %-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}
awk: cmd. line:1: ^ backslash not last character on line
awk: cmd. line:1: BEGIN {print "u_name\t\th_dir\t\tshell" RS="xxxxxxxx"} {printf %-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}
awk: cmd. line:1: ^ syntax error
[caozx26@client ~]$ awk -F: 'BEGIN {print "u_name\t\th_dir\t\tshell" RS="xxxxxxxx"} {printf "%-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}' /etc/passwd
awk: cmd. line:1: BEGIN {print "u_name\t\th_dir\t\tshell" RS="xxxxxxxx"} {printf "%-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}
awk: cmd. line:1: ^ syntax error
[caozx26@client ~]$
[caozx26@client ~]$ awk -F: 'BEGIN {print "u_name\t\th_dir\t\tshell"; RS="xxxxxxxx"} {printf "%-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}' /etc/passwd
u_name h_dir shell
root /home/u88 /bin/bashxxxxxxxx
[caozx26@client ~]$ ifconfig ens33|awk 'NR>1 {print $2}' |awk -F"" 'NR<2 {print $2}'
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options: GNU long options: (standard)-f progfile --file=progfile-F fs --field-separator=fs-v var=val --assign=var=val
Short options: GNU long options: (extensions)-b --characters-as-bytes-c --traditional-C --copyright-d[file] --dump-variables[=file]-e 'program-text' --source='program-text'-E file --exec=file-g --gen-pot-h --help-L [fatal] --lint[=fatal]-n --non-decimal-data-N --use-lc-numeric-O --optimize-p[file] --profile[=file]-P --posix-r --re-interval-S --sandbox-t --lint-old-V --versionTo report bugs, see node `Bugs' in `gawk.info', which is
section `Reporting Problems and Bugs' in the printed version.gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.Examples:gawk '{ sum += $1 }; END { print sum }' filegawk -F: '{ print $1 }' /etc/passwd
[caozx26@client ~]$
[caozx26@client ~]$ ifconfig ens33|grep broadcast |awk -F'' '{print $2}'|awk '{print $1}'
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options: GNU long options: (standard)-f progfile --file=progfile-F fs --field-separator=fs-v var=val --assign=var=val
Short options: GNU long options: (extensions)-b --characters-as-bytes-c --traditional-C --copyright-d[file] --dump-variables[=file]-e 'program-text' --source='program-text'-E file --exec=file-g --gen-pot-h --help-L [fatal] --lint[=fatal]-n --non-decimal-data-N --use-lc-numeric-O --optimize-p[file] --profile[=file]-P --posix-r --re-interval-S --sandbox-t --lint-old-V --versionTo report bugs, see node `Bugs' in `gawk.info', which is
section `Reporting Problems and Bugs' in the printed version.gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.Examples:gawk '{ sum += $1 }; END { print sum }' filegawk -F: '{ print $1 }' /etc/passwd
[caozx26@client ~]$
awk
print printf
格式化输出
print ->echo
printf ->echo -n
[caozx26@client ~]$ awk -F: 'BEGIN {OFS="\t\t";print "u_name\t\th_dir\t\tshell\nxxxxxx"} {printf "%-20s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxxx"}' 2.txt
u_name h_dir shell
xxxxxx
root /root /bin/bash hello world
bin /bin /sbin/nologin test1 test2
daemon /sbin /sbin/nologin
adm /var/adm /sbin/nologin
lp /var/spool/lpd /sbin/nologin
xxxxxxxxx
[caozx26@client ~]$ awk -F: 'BEGIN {print "u_name\t\th_dir\t\tshell"; RS="xxxxxxxx"} {printf "%-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}' /etc/passwd
u_name h_dir shell
root /home/u88 /bin/bashxxxxxxxx
ip截取
[caozx26@client ~]$ ifconfig ens33|grep broadcastinet 192.168.235.200 netmask 255.255.255.0 broadcast 192.168.235.255
[caozx26@client ~]$ ifconfig ens33 |grep broadcast|awk '{print $2}'
192.168.235.200
[caozx26@client ~]$ ifconfig ens33|awk 'NR==2 {print $2}'
192.168.235.200
记录
caozx26@192.168.235.200's password:┌────────────────────────────────────────────────────────────────────┐│ • MobaXterm 20.0 • ││ (SSH client, X-server and networking tools) ││ ││ ➤ SSH session to caozx26@192.168.235.200 ││ • SSH compression : ✘ ││ • SSH-browser : ✔ ││ • X11-forwarding : ✔ (remote display is forwarded through SSH) ││ • DISPLAY : ✔ (automatically set on remote server) ││ ││ ➤ For more info, ctrl+click on help or visit our website │└────────────────────────────────────────────────────────────────────┘Last login: Fri Oct 17 02:54:00 2025 from 192.168.235.1
[caozx26@client ~]$ awk -F: 'BEGIN {OFS "\t\t";print "u_name\t\th_dir\t\tshell\nxxxxxx"} {printf "%-20s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}'
u_name h_dir shell
xxxxxx
^C
[caozx26@client ~]$ awk -F: 'BEGIN {OFS "\t\t";print "u_name\t\th_dir\t\tshell\nxxxxxx"} {printf "%-20s %-20s %-20s\n",$1,$(NF-1),$NF}' END{print "xxxxxxxx"} 2.txt
u_name h_dir shell
xxxxxx
awk: cmd. line:1: fatal: cannot open file `END{print' for reading (没有那个文件或目录)
[caozx26@client ~]$ ls 2.txt
2.txt
[caozx26@client ~]$ cat 2.txt
root:x:0:0:root:/root:/bin/bash hello world
bin:x:1:1:bin:/bin:/sbin/nologin test1 test2
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
[caozx26@client ~]$ awk -F: 'BEGIN {OFS "\t\t";print "u_name\t\th_dir\t\tshell\nxxxxxx"} {printf "%-20s %-20s %-20s\n",$1,$(NF-1),$NF}' END{print "xxxxxxxx"} 2.txt
u_name h_dir shell
xxxxxx
awk: cmd. line:1: fatal: cannot open file `END{print' for reading (没有那个文件或目录)
[caozx26@client ~]$ awk -F: 'BEGIN {OFS="\t\t";print "u_name\t\th_dir\t\tshell\nxxxxxx"} {printf "%-20s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxxx"} 2.txt
> ^C
[caozx26@client ~]$ awk -F: 'BEGIN {OFS="\t\t";print "u_name\t\th_dir\t\tshell\nxxxxxx"} {printf "%-20s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxxx"}' 2.txt
u_name h_dir shell
xxxxxx
root /root /bin/bash hello world
bin /bin /sbin/nologin test1 test2
daemon /sbin /sbin/nologin
adm /var/adm /sbin/nologin
lp /var/spool/lpd /sbin/nologin
xxxxxxxxx
[caozx26@client ~]$ awk -F: 'BEGIN {print "u_name\t\th_dir\t\tshell" RS "xxxxxxxx"} {printf %-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}' /etc/passwd
awk: cmd. line:1: BEGIN {print "u_name\t\th_dir\t\tshell" RS "xxxxxxxx"} {printf %-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: BEGIN {print "u_name\t\th_dir\t\tshell" RS "xxxxxxxx"} {printf %-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}
awk: cmd. line:1: ^ backslash not last character on line
awk: cmd. line:1: BEGIN {print "u_name\t\th_dir\t\tshell" RS "xxxxxxxx"} {printf %-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}
awk: cmd. line:1: ^ syntax error
[caozx26@client ~]$ awk -F: 'BEGIN {print "u_name\t\th_dir\t\tshell" RS="xxxxxxxx"} {printf %-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}' /etc/passwd
awk: cmd. line:1: BEGIN {print "u_name\t\th_dir\t\tshell" RS="xxxxxxxx"} {printf %-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: BEGIN {print "u_name\t\th_dir\t\tshell" RS="xxxxxxxx"} {printf %-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: BEGIN {print "u_name\t\th_dir\t\tshell" RS="xxxxxxxx"} {printf %-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}
awk: cmd. line:1: ^ backslash not last character on line
awk: cmd. line:1: BEGIN {print "u_name\t\th_dir\t\tshell" RS="xxxxxxxx"} {printf %-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}
awk: cmd. line:1: ^ syntax error
[caozx26@client ~]$ awk -F: 'BEGIN {print "u_name\t\th_dir\t\tshell" RS="xxxxxxxx"} {printf "%-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}' /etc/passwd
awk: cmd. line:1: BEGIN {print "u_name\t\th_dir\t\tshell" RS="xxxxxxxx"} {printf "%-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}
awk: cmd. line:1: ^ syntax error
[caozx26@client ~]$ awk -F: 'BEGIN {print "u_name\t\th_dir\t\tshell" RS="xxxxxxxx"} {printf "%-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}' /etc/passwd
awk: cmd. line:1: BEGIN {print "u_name\t\th_dir\t\tshell" RS="xxxxxxxx"} {printf "%-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}
awk: cmd. line:1: ^ syntax error
[caozx26@client ~]$ awk -F: 'BEGIN {print "u_name\t\th_dir\t\tshell"; RS="xxxxxxxx"} {printf "%-15s %-20s %-20s\n",$1,$(NF-1),$NF} END{print "xxxxxxxx"}' /etc/passwd
u_name h_dir shell
root /home/u88 /bin/bashxxxxxxxx
[caozx26@client ~]$ ifconfig ens33|awk 'NR>1 {print $2}' |awk -F':' 'NR<2 {print $2}'[caozx26@client ~]$ ifconfig ens33|awk 'NR>1 {print $2}' |awk -F';' 'NR<2 {print $2}'[caozx26@client ~]$ ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.235.200 netmask 255.255.255.0 broadcast 192.168.235.255inet6 fe80::20c:29ff:fed2:84d0 prefixlen 64 scopeid 0x20<link>ether 00:0c:29:d2:84:d0 txqueuelen 1000 (Ethernet)RX packets 9735 bytes 798775 (780.0 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 22139 bytes 8474072 (8.0 MiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[caozx26@client ~]$ ifconfig ens33|awk 'NR>1 {print $2}' |awk -F'' 'NR<2 {print $2}'
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options: GNU long options: (standard)-f progfile --file=progfile-F fs --field-separator=fs-v var=val --assign=var=val
Short options: GNU long options: (extensions)-b --characters-as-bytes-c --traditional-C --copyright-d[file] --dump-variables[=file]-e 'program-text' --source='program-text'-E file --exec=file-g --gen-pot-h --help-L [fatal] --lint[=fatal]-n --non-decimal-data-N --use-lc-numeric-O --optimize-p[file] --profile[=file]-P --posix-r --re-interval-S --sandbox-t --lint-old-V --versionTo report bugs, see node `Bugs' in `gawk.info', which is
section `Reporting Problems and Bugs' in the printed version.gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.Examples:gawk '{ sum += $1 }; END { print sum }' filegawk -F: '{ print $1 }' /etc/passwd
[caozx26@client ~]$ ifconfig ens33|awk 'NR>1 {print $2}' |awk -F"" 'NR<2 {print $2}'
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options: GNU long options: (standard)-f progfile --file=progfile-F fs --field-separator=fs-v var=val --assign=var=val
Short options: GNU long options: (extensions)-b --characters-as-bytes-c --traditional-C --copyright-d[file] --dump-variables[=file]-e 'program-text' --source='program-text'-E file --exec=file-g --gen-pot-h --help-L [fatal] --lint[=fatal]-n --non-decimal-data-N --use-lc-numeric-O --optimize-p[file] --profile[=file]-P --posix-r --re-interval-S --sandbox-t --lint-old-V --versionTo report bugs, see node `Bugs' in `gawk.info', which is
section `Reporting Problems and Bugs' in the printed version.gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.Examples:gawk '{ sum += $1 }; END { print sum }' filegawk -F: '{ print $1 }' /etc/passwd
[caozx26@client ~]$ ifconfig ens33|awk 'NR==2 {print $2}'
192.168.235.200
[caozx26@client ~]$ ifconfig ens33|grep Bcast |awk -F':' '{print $2}'|awk '{print $1}'
[caozx26@client ~]$ ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.235.200 netmask 255.255.255.0 broadcast 192.168.235.255inet6 fe80::20c:29ff:fed2:84d0 prefixlen 64 scopeid 0x20<link>ether 00:0c:29:d2:84:d0 txqueuelen 1000 (Ethernet)RX packets 10822 bytes 896412 (875.4 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 25135 bytes 9606174 (9.1 MiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[caozx26@client ~]$ ifconfig ens33|grep broadcast |awk -F'' '{print $2}'|awk '{print $1}'
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options: GNU long options: (standard)-f progfile --file=progfile-F fs --field-separator=fs-v var=val --assign=var=val
Short options: GNU long options: (extensions)-b --characters-as-bytes-c --traditional-C --copyright-d[file] --dump-variables[=file]-e 'program-text' --source='program-text'-E file --exec=file-g --gen-pot-h --help-L [fatal] --lint[=fatal]-n --non-decimal-data-N --use-lc-numeric-O --optimize-p[file] --profile[=file]-P --posix-r --re-interval-S --sandbox-t --lint-old-V --versionTo report bugs, see node `Bugs' in `gawk.info', which is
section `Reporting Problems and Bugs' in the printed version.gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.Examples:gawk '{ sum += $1 }; END { print sum }' filegawk -F: '{ print $1 }' /etc/passwd
[caozx26@client ~]$ ifconfig ens33|grep broadcast |awk -F '{print $2}'
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options: GNU long options: (standard)-f progfile --file=progfile-F fs --field-separator=fs-v var=val --assign=var=val
Short options: GNU long options: (extensions)-b --characters-as-bytes-c --traditional-C --copyright-d[file] --dump-variables[=file]-e 'program-text' --source='program-text'-E file --exec=file-g --gen-pot-h --help-L [fatal] --lint[=fatal]-n --non-decimal-data-N --use-lc-numeric-O --optimize-p[file] --profile[=file]-P --posix-r --re-interval-S --sandbox-t --lint-old-V --versionTo report bugs, see node `Bugs' in `gawk.info', which is
section `Reporting Problems and Bugs' in the printed version.gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.Examples:gawk '{ sum += $1 }; END { print sum }' filegawk -F: '{ print $1 }' /etc/passwd
[caozx26@client ~]$ cat 2.txt
root:x:0:0:root:/root:/bin/bash hello world
bin:x:1:1:bin:/bin:/sbin/nologin test1 test2
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
[caozx26@client ~]$ cat ifconfig ens33
cat: ifconfig: 没有那个文件或目录
cat: ens33: 没有那个文件或目录
[caozx26@client ~]$ ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.235.200 netmask 255.255.255.0 broadcast 192.168.235.255inet6 fe80::20c:29ff:fed2:84d0 prefixlen 64 scopeid 0x20<link>ether 00:0c:29:d2:84:d0 txqueuelen 1000 (Ethernet)RX packets 12351 bytes 1032138 (1007.9 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 30154 bytes 11537282 (11.0 MiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[caozx26@client ~]$ ifconfig ens33|grep broadcastinet 192.168.235.200 netmask 255.255.255.0 broadcast 192.168.235.255
[caozx26@client ~]$ ifconfig ens33 |grep broadcast|awk '{print $2}'
192.168.235.200
[caozx26@client ~]$