Shell Command _awk command

Source: Internet
Author: User
Tags gopher



# awk ' Condition 1{action 1} Condition 2{action 2} ... ' File name condition (Pattern):Generally use relational expressions as conditions x > 10 to determine if a variable x is greater than 10x>=10 greater than or equal to x<=10 less than or equal to actions (Action):Formatting output Flow control statements
text message: Vim score.txt
1234 ID Name PHP Linux MySQL Average1 Liming 82 95 86 87.662 Sc 74 96 87 85.663 Gao 99 83 93 91.66


1. Output score. txt per line content, line 2nd +\t+ 6th line +\n

printf line end does not output line break; Print behavior output line break

12345678910 [[email protected] tmp]# cat score.txt D Name PHP Linux MySQL Average1 Liming 82 95 86 87.662 Sc 74 96 87 85.663 Gao 99 83 93 91.66[[email protected] tmp]# awk ‘{printf $2 "\t" $6 "\n"}‘ score.txt  Name    AverageLiming  87.66Sc      85.66Gao     91.66

2, Output DF 1th, 3 lines of content

12345678910 [[email protected] tmp]# df -hFilesystem                 Size  Used Avail Use% Mounted on/dev/mapper/vg_v2-lv_root36G  5.0G   29G  15% /tmpfs                      931M   72K  931M   1% /dev/shm/dev/sda1 485M   40M  421M   9% /boot[[email protected] tmp]# df -h | awk ‘{print $1 "\t" $3}‘Filesystem      Used/dev/mapper/vg_v2-lv_root5.0Gtmpfs   72K/dev/sda140M


3, output below 1 of the people in


1 df-h|grep tmpfs|awk ‘{print $5}‘|cut -d "%"-f 1


4. BEGIN

123456 [[email protected] tmp]# awk ‘BEGIN{print "这里是开始前输出"}{printf $2 "\t" $6 "\n"}‘ score.txt这里是开始前输出Name    AverageLiming  87.66Sc      85.66Gao     91.66


5. END

1234567 [[email protected] tmp]# awk ‘BEGIN{print "这里是开始前输出"}END{print "这里是结束输出"}{printf $2 "\t" $6 "\n"}‘ score.txt    这里是开始前输出Name    AverageLiming  87.66Sc      85.66Gao     91.66这里是结束输出


6. FS Set delimiter

1234567891011121314151617181920212223242526272829303132333435363738394041424344 [[email protected] tmp]# cat /etc/passwd | grep "/bin/bash" | awk ‘BEGIN {FS=":"} {printf $1 "\t" $3 "\n"}‘root    0chenzhi 500mysql   27[[email protected] tmp]# cat /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinusbmuxd:x:113:113:usbmuxd user:/:/sbin/nologinvcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologinrpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologinrtkit:x:499:497:RealtimeKit:/proc:/sbin/nologinavahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologinabrt:x:173:173::/etc/abrt:/sbin/nologinrpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologinnfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologinhaldaemon:x:68:68:HAL daemon:/:/sbin/nologingdm:x:42:42::/var/lib/gdm:/sbin/nologinntp:x:38:38::/etc/ntp:/sbin/nologinapache:x:48:48:Apache:/var/www:/sbin/nologinsaslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologinpulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologintcpdump:x:72:72::/:/sbin/nologinchenzhi:x:500:500:chenzhi:/home/chenzhi:/bin/bash

mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash


[[email protected] tmp]# cat /etc/passwd | grep "/bin/bash" | awk ‘BEGIN {FS=":"} {printf $1 "\t" $3 "\n"}‘root    0chenzhi 500mysql   27


7. Logical operation

Grep-v name to delete the row containing the content "Name"

awk ' $6 >= {printf $ "\ n"} calculates the 6th row greater than 87

12345678 [[email protected] tmp]# cat score.txt D Name PHP Linux MySQL Average1 Liming 82 95 86 87.662 Sc 74 96 87 85.663 Gao 99 83 93 91.66[[email protected] tmp]# cat score.txt | grep -v Name | awk ‘$6 >= 87 {printf $2 "\n" }‘LimingGao




From for notes (Wiz)

Shell Command _awk command

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.