Huu Phan | Blog Linux operating system | Linux operating system -Blog Huu Phan | www.huuphan.com
huuphan.com. Powered by Blogger.

Explore the world of seamless email management with Zimbra Mail on Linux! Our blog delves into a step-by-step guide on harnessing the potential of Linux commands, providing easy-to-follow how-tos, and unlocking the magic of Bash scripting. Simplify your email administration journey and enhance your skills with our insightful tips and tricks.

awk useful commands examples

awk useful comand examples. How to Output Field Separator Variable, Number of Fields in a record, To print Non-system users and ignoring "nobody" etc. 

You may reading link below :
For example, the output passwd file use awk command
$ cat /etc/passwd | awk -F':' 'BEGIN{ print "Name\tUID,GID\tHomeDirectory" }{print $1"\t"$3,$4"\t"$6}'
The output as below:
Name UID,GID HomeDirectory
root 0 0 /root
bin 1 1 /bin
daemon 2 2 /sbin
adm 3 4 /var/adm
lp 4 7 /var/spool/lpd
sync 5 0 /sbin
shutdown 6 0 /sbin
halt 7 0 /sbin
mail 8 12 /var/spool/mail
operator 11 0 /root
games 12 100 /usr/games
ftp 14 50 /var/ftp
nobody 99 99 /
systemd-bus-proxy 999 998 /
systemd-network 192 192 /
dbus 81 81 /
polkitd 998 997 /
tss 59 59 /dev/null
sshd 74 74 /var/empty/sshd
postfix 89 89 /var/spool/postfix
dockerroot 997 994 /var/lib/docker
huupv 1000 1000 /home/huupv

Awk OFS Example: Output Field Separator Variable


By default awk OFS is a single space character. How to use OFS assgin a single space character equal to ',' as below:

$ cat /etc/passwd | awk -F':' 'BEGIN{{OFS=","} print "Name\tUID,GID\tHomeDirectory" }{print $1"\t"$3,$4"\t"$6}'

The output as below:
Name UID,GID HomeDirectory
root 0,0 /root
bin 1,1 /bin
daemon 2,2 /sbin
adm 3,4 /var/adm
lp 4,7 /var/spool/lpd
sync 5,0 /sbin
shutdown 6,0 /sbin
halt 7,0 /sbin
mail 8,12 /var/spool/mail
operator 11,0 /root
games 12,100 /usr/games
ftp 14,50 /var/ftp
nobody 99,99 /
systemd-bus-proxy 999,998 /
systemd-network 192,192 /
dbus 81,81 /
polkitd 998,997 /
tss 59,59 /dev/null
sshd 74,74 /var/empty/sshd
postfix 89,89 /var/spool/postfix
dockerroot 997,994 /var/lib/docker
huupv 1000,1000 /home/huupv

Awk NF NR Example: Number of Fields in a record

Awk NF gives you the total number of fields in a record
Awk NR gives you the total number of records being processed or line number

$ cat huuphan-awk
The output as below:
huu 213 78 84 77
phan 221 56 58 45 111
huuphan 222 138 37 31
huuphan.com 257 178 67 45 333

$ awk '{print "line "NR,"->",NF"(Fields)"}' huuphan-awk
The output as below:
line 1 -> 5(Fields)
line 2 -> 6(Fields)
line 3 -> 5(Fields)
line 4 -> 6(Fields)

To print Non-system users and ignoring "nobody"

$ getent passwd |awk -F : '$3 >= 1000 && $3 < 65534' | awk -F':' 'BEGIN{{OFS=","} print "Name\tUID,GID\tHomeDirectory" }{print $1"\t"$3,$4"\t"$6}'
The output as below:
Name UID,GID HomeDirectory
huupv 1000,1000 /home/huupv

post new :

Huu Phan | Blog Linux operating system | Huu Phan ~ Zimbra Mail Server,linux,bash script,centos,linux command | www.huuphan.com

This Blog is protected by DMCA.com