Awk Getting started with a detailed

Source: Internet
Author: User

I. Overview of AWK

"Oz"

awk: report Generator, format print

Format each field of each row read in the file, and then display

Internal support variables, conditional judgments, loops, arrays

awk--New awk--Nawk (fee)

GNU awk---Gawk "Sue"


Ii. Use of awk

awk and default are delimited by any whitespace character, while cut defaults to a tab and can only be specified with a single space character as a delimiter

Note: Use "Do not use" as sed is easy to error

Usage format:

awk [OPTIONS] ' SCRIPT ' FILE ...

awk [OPTIONS] ' pattern/{action} ' File ...

Action

Print, default printing

Pattern

Include Address delimitation:

/pat1/,/pat2/

/pattern/

! /pattern

Expression: Expressions: >,<,=,>=,<=,=,!=,~

BEGIN: preparatory work before execution

END: Finishing work after execution


Four types of separators commonly used by awk:

Input:

Line delimiter: Default line break: "$", cat-a can view all white-space characters of a file

Field delimiter: Default tab

Output:

Line delimiter: Default line break: "$"

Field delimiter: Blank

awk built-in variables:

$1,$2,$3 ...: 2nd field, 2nd field, 3rd field ...

$ A: Indicates the entire row

$NF: Indicates the last field, NF: Indicates the total number of fields

FS: The field delimiter used when reading the entry. Default Blank

OFS: The field delimiter used when outputting rows. Default Blank


[[email protected] shell]# awk -f :  '/^root/ {print $1} '  /etc/ passwdroot[[email protected] shell]# awk -f:  '/^\<root\>/ {print $1} '  /etc/passwd   root[[email protected] shell]# awk -F :  '/^ root/ {print $1,$7} '  /etc/passwd      #逗号只是命令的一部分, not the result of the output root /bin /bash[[email protected] shell]# awk -f :  '/^root/ {print $1$7} '  / etc/passwdroot/bin/bash[[email protected] shell]# awk -f:  '/^\<root\>/ { print username $1,$7} '  /etc/passwdroot /bin/bash[[email protected] shell]#  awk -f:  '/^\<root\>/ {print username sb$1,$7} '  /etc/passwdroot /bin/ bash[[email protected] shell]# awk -f:  '/^\<root\>/ {print username &NBSP;SB&NBSP;$1,$7}'  /etc/passwdroot /bin/bash[[email protected] shell]# awk -F:  '/^\<root\ >/ {print  "USERNAME&NBSP;SB"  $1,$7} '  /etc/passwdusername sbroot /bin/bash[[ email protected] shell]# awk -f:  '/^\<root\>/ {print  ' username  sb  " $1,$7}  /etc/passwdusername sb root /bin/bash[[email protected]  shell]# ^c[[email protected] shell]# awk -f:  '/^\<root\>/ {print   "username sb "  $1,nihao$7} '  /etc/passwdusername sb root /bin/bash[[ email protected] shell]# awk -f:  '/^\<root\>/ {print  ' username  sb  " $1," Nihao "$7} '  /etc/passwdusername sb root nihao/bin/bash[[email  protected] shell]# awk -f:  '/^\<root\>/ {print  "username sb "  $1, "Nihao" $7SB} '  /eTc/passwdusername sb root nihao/bin/bash[[email protected] shell]# awk -f:   '/^\<root\>/ {print  ' username sb  " $1," Nihao "$7" SB "} '  /etc/ Passwdusername sb root nihao/bin/bashsb[[email protected] shell]# awk -f:   '/^\<root\>/ {print  ' username sb  " $1," Nihao "$7" \NSB "} '  /etc/ Passwdusername sb root nihao/bin/bashsb
[[email protected] shell]# df -h|awk   ' {print $1} ' Filesystem/dev/sda2tmpfs/ dev/sda1/dev/sda5[[email protected] shell]# df -h|awk   ' {print $2} ' size29g495m477m68g[[email protected] shell]# df -h|awk   "{print $2}"   filesystem      size  used avail use% mounted on/ Dev/sda2        29g  4.6g   23g  17 % /tmpfs           495m      0  495M   0% /dev/shm/dev/sda1        477m   28m  424m   7% /boot/dev/sda5         68G  4.3G   60G   7% /usr[[email  Protected] shell]# df -h|awk   ' {print} '    Filesystem      Size   Used Avail Use% Mounted on/dev/sda2         29g  4.6g   23g  17% /tmpfs            495m     0  495m   0% /dev/shm/ Dev/sda1       477m   28m  424m   7 % /boot/dev/sda5        68g  4.3g   60g    7% /usr

1, take/etc/passwd the UID is greater than 500 user name

[Email protected] shell]# awk-f: ' $3>=500{print ' Username: ' $ '/etc/passwd Username:testbashUsername:ba SherUsername:nologinUsername:fedoraUsername:user1Username:bash1Username:xjUsername:OracleUsername:bashUsername : XxjUsername:openstackUsername:centos

2. Take/etc/passwd to the end of bash

[[email protected] shell]# awk -f:  ' $7~/bash$/{print} '  /etc/passwdroot:x:0:0: chfn root:/root:/bin/bashtestbash:x:501:501::/home/testbash:/bin/bashbasher:x:502:502::/home/basher:/bin/ Bashfedora:x:504:504::/home/fedora:/bin/bashuser1:x:505:505::/home/user1:/bin/bashbash1:x:506:506::/home/bash1 :/bin/bashxj:x:507:507::/home/xj:/bin/bashoracle:x:3000:3000::/home/database:/bin/bashbash:x:3001:3001::/home/ Bash:/bin/bashxxj:x:3002:3002::/home/xxj:/bin/bashopenstack:x:3003:3003::/home/openstack:/bin/bashcentos:x :3004:3004::/home/centos:/bin/bash[[email protected] shell]# awk -f:  '/bash$/{print   "sbsb: " $} ' &NBSP;/ETC/PASSWD&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SBSB: &NBSP;ROOT:X:0:0:CHFN&NBSP;ROOT:/ROOT:/BIN/BASHSBSB:&NBSP;TESTBASH:X:501:501::/HOME/TESTBASH:/BIN/BASHSBSB:  basher:x:502:502::/home/basher:/bin/bashsbsb: fedora:x:504:504::/home/fedora:/bin/bashsbsb:  User1:x:505:505::/home/user1:/BIN/BASHSBSB:&NBSP;BASH1:X:506:506::/HOME/BASH1:/BIN/BASHSBSB:&NBSP;XJ:X:507:507::/HOME/XJ:/BIN/BASHSBSB:  Oracle:x:3000:3000::/home/database:/bin/bashsbsb: bash:x:3001:3001::/home/bash:/bin/bashsbsb:  xxj:x:3002:3002::/home/xxj:/bin/bashsbsb: openstack:x:3003:3003::/home/openstack:/bin/bashsbsb:  centos:x:3004:3004::/home/centos:/bin/bash[[email protected] shell]# awk -f:  ' $7== '/ Bin/bash " {print $1}  /etc/passwd   Roottestbashbasherfedorauser1bash1xjoraclebashxxjopenstackcentos

Begin,end:

[[email protected] shell]# awk -f:  ' $3>=500 {print  '-------------\ n "$ '  /etc/passwd ------------- Testbash-------------basher-------------nologin-------------Fedora-------------user1-------------bash1-------------XJ---- ---------Oracle-------------bash-------------xxj-------------openstack-------------centos[[email  protected] shell]# awk -f:  ' begin{print  "username\n----------"}$3>=500 {print  $1} '  /etc/ Passwdusername----------testbashbashernologinfedorauser1bash1xjoraclebashxxjopenstackcentos[[email  protected] shell]# awk -f:  ' $3>=500 {print $1}end{print  '-------"} '  /etc/passwdtestbashbashernologinfedorauser1bash1xjoraclebashxxjopenstackcentos-------[[email  protected] shell]# awk  ' begin{fs= ': "} $3>=500 {print $7}  '  /etc/ passwd/bin/bash                            #用BEGIN指定分隔符/ Bin/bash/sbin/nologin/bin/bash/bin/bash/bin/bash/bin/bash/bin/bash/bin/bash/bin/bash/bin/bash/bin/bash


Practice:

1, the group showing GID less than 500

# awk-f: ' $3<500{print $ '

2. Display the default shell for Nologin users

# awk-f: '/\<nologin$/{print '} '

# awk-f: ' $7== '/sbin/nologin ' {print '} '

3. Display the configuration information of the Eh0 network card file, note that only the value after = is displayed

# awk-f= ' {print $} '

4. Display the kernel parameters defined in the/etc/sysctl.conf file, display only the name

5, display eth0 network card IP address, through the ifconfig command result to filter

# ifconfig Eth0|awk '/inet addr/{print} ' |awk-f: ' {print $} ' |awk ' {print '} '

Awk Getting started with a detailed

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.