shell programming series 15-- Three Musketeers awk text processing format output printf

shell programming series 15-- awk printf formatted output of text processing trio 

printf format specifier 

specifier meanings
 % S print string
 % D print decimal number
 % F print a float
 % X print hexadecimal
 % O Print octal number
 % in the form of printed mathematical e scientific notation
 % C the ASCII code printing a single character 
printf modifiers 
modifier meaning



 -         left
 +         Right alignment 
# preceded displayed in octal 0, displayed in hexadecimal preceded 0x 
the printf awk output format of summary: 
    specifier

     % S print string
     % D 10 print decimal
     % F print float
     % X print hexadecimal
     %o Printing octal
     % E scientific notation numbers printed formats
     % C print a single character ASCII code 

    modifier
     -         Left
     +         Right alignment 
    # preceded displayed in octal 0, 0x, hexadecimal display in front of 

    the format operator example: 
    1 , print string format / etc / passwd in the seventh field to " : " delimiter
         awk  ' the BEGIN {the FS = ":"} {the printf "% S \ n-', $. 7} '  the passwd 
    2 , printed in decimal format / etc / passwd in the third field, to " : " delimiter
         awk  ' the BEGIN {the FS = ":" {} the printf "% D \ n-', $}. 3 ' the passwd 
    . 3 , floating-point format print / etc / passwd in the third field, to " :" As the delimiter
         awk  ' the BEGIN {the FS =": "{} the printf"% 0.2f \ n-', $}. 3 '  the passwd 
    . 4 , the print / etc / passwd in the third field in hexadecimal format to " : " as the separator
         awk  ' the BEGIN {the FS =": "} {the printf"% X \ n-', $. 3} '  the passwd 
    . 5 , the print / etc / passwd in the third field in octal format to " : " as the separator
         awk  ' the BEGIN {the FS =": "} {the printf"% O \ n-', $. 3} '  the passwd 
    . 6 , print in scientific notation format / etc / passwd in the third field, to " : " as a delimiter
         awk  'BEGIN{FS=":"}{printf "%e\n",$3}' passwd

    Example Modifier: 
    1 , left justified format -
     2 , right aligned in the +
     3 , print octal or hexadecimal numbers are preceded by # 

# the printf no default delimiter 
[the root @ localhost 5.11 ] # awk  ' the BEGIN {the FS = ":"} {the printf $. 1} '  the passwd  
rootbindaemonadmlpsyncshutdownhaltmailoperatorgamesftpnobodysystemd - networkdbuspolkitdsshdpostfixajiechronydeploynginx 

# added wrap, formatted output 
[the root @ localhost 5.11 ] # awk  ' the BEGIN {the FS = ":"} {the printf "% S \ n-", $. 1} '  passwd  
root 
bin 
daemon 
adm 
lp 
Sync 
the shutdown 
the HALT 
mail
operator 
games 
FTP 
the nobody 
systemd - Network 
dbus 
polkitd 
the sshd 
postfix 
ajie 
the chrony 
Deploy 
Nginx 

# placeholder landscaping output, the default is right justified 
[the root @ localhost 5.11 ] # awk  ' the BEGIN {the FS = ":" {} the printf "% 20S% 20S \ n-",. 1 $, $}. 7 ' / etc / the passwd 
                the root             / bin / the bash 
                 bin         / sbin / nologin 
              daemon         / sbin / nologin 
                 ADM         / sbin /nologin
                  lp        /sbin/nologin
                sync            /bin/sync
            shutdown       /sbin/shutdown
                halt           /sbin/halt
                mail        /sbin/nologin
            operator        /sbin/nologin
               games        /sbin/nologin
                 ftp        /sbin/nologin
              nobody        /sbin/nologin
     systemd-network        /sbin/nologin
                dbus         / sbin / nologin 
             polkitd         / sbin / nologin 
                the sshd         / sbin / nologin 
             postfix         / sbin / nologin 
                ajie             / bin / the bash 
              the chrony         / sbin / nologin 
              Deploy             / bin / the bash 
               Nginx         / sbin / nologin 

# - minus sign is left-aligned, + plus number is right justified 
[the root @ localhost 5.11 ] # awk  ' the BEGIN {the FS = ":" {} the printf "% -20s% -20s \ n-",. 1 $, $}. 7 ' / etc /passwd
root                 /bin/bash           
bin                  /sbin/nologin       
daemon               /sbin/nologin       
adm                  /sbin/nologin       
lp                   /sbin/nologin       
sync                 /bin/sync           
shutdown             /sbin/shutdown      
halt                 /sbin/halt          
mail                 /sbin/nologin       
operator             /sbin/nologin       
games                /sbin/nologin       
ftp                  /sbin/nologin       
nobody               /sbin/nologin       
systemd-network      /sbin/nologin       
dbus                 /sbin/nologin       
polkitd              /sbin/nologin       
sshd                 /sbin/nologin       
postfix              /sbin/nologin       
ajie                 /bin/bash           
chrony               /sbin/nologin       
deploy               /bin/bash           
nginx                /sbin/nologin 

 

Guess you like

Origin www.cnblogs.com/reblue520/p/10984736.html