Use of awk built-in functions under Linux (Split/substr/length)

Source: Internet
Author: User

first, split initialization and type coercion

Awk's built-in function split allows you to separate a string into words and store them in an array. You can define the domain delimiter yourself or use the current FS (domain delimiter) value.
Format:

Split (String, array, field separator)
Split (string, array) If the third parameter is not provided, AWK uses the current FS value by default.


Example 1: Replacing separators

time ="12:34:56"out = 'echo $timeawk'  {split ($0,a, ":");p rint a[1],a[2],a[3]}'echo $out

Example 2: Calculation of sum within a specified range (sum of wages calculated for each person in January)

$Cattest.txt Tom -- A- OneCar5        theJohn -- on- -Bike4        +Vivi -- on- -Car4       2800Tom -- on- -Car3       2500John -- on- -Bike6       3500$ awk '{split ($2,a, "-"); if (a[2]==01) {b[$1]+=$5}}end{for (i in B) print I,b[i]}'test.txt Vivi2800Tom2500John4500

Example 3:

$HeadindustryList.txt itit, Internet IT Services (SystemDatamaintenance) IT Services (System/data/maintenance), professional services/Consulting (Accounting/Legal/Human resources, etc.) IT Services (System/data/maintenance), professional services/Consulting (accounting/legal/human resources, etc.), academic/scientific research, computer software $HeadIndustryList.txt |awk '{Split ($, arr, ","); for (i in ARR) print Arr[i]}'itit Internet IT Services (SystemDatamaintenance) IT Services (SystemDatamaintenance) Professional Services/Consulting (Accounting/Law/Human resources, etc.) IT Services (SystemDatamaintenance) Professional Services/Consulting (Accounting/Law/Human Resources, etc.) academic/Scientific research computer software $CatIndustryList.txt |awk '{Split ($, arr, ","); for (i in ARR) print Arr[i]}'|Sort|Uniq> Industrylist_split.txt

Second, substr intercept string
Returns a substring of the specified length from the starting position, or a substring from the starting position to the end of the string if no length is specified.
Format:
SUBSTR (s,p) returns the suffix part of the string s starting from P
SUBSTR (s,p,n) returns the suffix part of the string s starting from p in length n
Example:

Echo " 123 " awk ' {print substr ($0,1,1)} ' 1

Explain:
Awk-f ', ' {print substr ($3,6)} ' ---> indicates that it starts with the 6th character in the 3rd field and continues to the end of the set delimiter ",".
substr ($3,10,8) ---> indicates that the 10th character in the 3rd field starts with a 8-character end.
substr ($3,6) ---> representation starts from the 6th character in the 3rd field, until the end

Length of string
The length function returns the lengths of strings without arguments. The length function returns the number of characters in the entire record.

Echo " 123 " awk ' {print length} ' 3

Cat Info awk ' " ' ' {print $} ' awk ' {if (length > 0) print $} ' Sort Uniq > IndustryList.txt
Four, gsub function

The Gsub function makes it possible to replace all regular expressions when they are matched. gsub (Regular expression, subsitution string, target string); Gsub (r,s,t).

Example: Replace ABC in a file with all ABC-containing lines into Def, then output the first and third columns

awk ' $ ~/abc/{gsub ("abc", "Def", $ $); print $, $ $} ' abc.txt

Reference:

http://gdcsy.blog.163.com/blog/static/12734360920130241521280/

Http://www.cnblogs.com/sunada2005/p/3493941.html

Use of awk built-in functions under Linux (Split/substr/length)

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.