Awk usage (1)

Source: Internet
Author: User

An awk program is composed of a cycle of the main input. A loop is a routine and will be executed repeatedly until some conditions exist to terminate it. The main input loop is a routine. Number of cycles = number of objects.

Awk can write two special routines, begin and end.

 

Cat name

=>

John Robinson, Koren inc., 978 4th Ave., Boston, MA: 01760,696-0987
Phyllis Chapman, Gve Corp., 34 sea drive, amesbury, MA: 01881,879-0900

Bulent green, wR Corp., 46 win, Alameda, MA: 93253,356-3635

Lily Smith, beyondsoft Corp., 28 win, Beijing, BJ: 2535,010-4546

 

Specify a separator in the awk

 

Use the-F parameter

 

Awk-F "/t" '{print $1}' # use a tab as the Separator

 

Use FS system variables

 

Awk 'in in {FS = ","} {print $1} 'name # fs is a space by default.

 

You can also use regular expressions to specify separators.

 

Awk-F "[,:]" '{print $6}' name

 

 

Determine whether the fields match in the awk


Sed-n'/^ $ /! P' name | awk-F "," '$5 !~ /MA/{print $1 "," $6 }'

=>

Lily Smith, 010-4546

 

Use of variables in awk


Awk '/^ $/{x ++} end {print x}' name # Number of printed Spaces

=> 3

 

Cat scre

=>

John 85 92 78 94 88
Andrea 89 90 75 90 86
Jasper 84 88 80 92 84

 

Awk '{Total = $2 + $3 + $4 + $5 + $6; AVG = total/5; print $1, AVG}' score # print the name and average score

=>

John 87.4
Andrea 86
Jason 85.6

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.