Awk string function (2), awk string Function

Source: Internet
Author: User

Awk string function (2), awk string Function

The substr function returns a substring starting from the specified position of the string. If the length of the substring is specified, the corresponding part of the string is returned. If the specified length exceeds the actual range of the string, the actual content is returned.
Format

Substr (string, starting position)
Substr (string, starting position, substring length)

Example

$ Awk '{print substr ("Santa Claus", 7,6)}' filename
Claus

Note: In the string "Santa Claus", the Child string with a length of 6 characters starting from position 7 is printed.

Match function: the match function returns the position where the regular expression appears in the string. If the regular expression does not appear, 0 is returned. The match function sets the built-in variable RSTART as the starting position of the substring in the string, and RLENGTH as the length of the substring. These variables can be used by the substr function to extract the substrings in the corresponding mode.
Format

Match (string, regular expression)

Example

$ Awk 'end {start = match ("Good ole USA",/[A-Z] + $/); print start} 'filename
10

(Regular Expression/[A-Z] + $/means to find the continuous occurrence of uppercase letters at the end of the string. the substring USA is found to start with 10th characters of the string "Good ole USA. If the string does not match the regular expression, 0 is returned.
Example

$ Awk 'end {start = match ("Good ole USA",/[A-Z] + $ /);\
Print RSTART, RLENGTH} 'filename
10 3
$ Awk 'in in {line = "Good ole USA "};\
END {match (line,/[A-Z] + $ /);\
Print substr (line, RSTART, RLENGTH)} 'filename'
USA

Note:
1. The variable RSTART is set by the match function to the starting position of the matched Regular Expression in the string. The variable RLENGTH is set to the length of the substring.
2. The substr function searches for substrings in the Variable line and uses the values of RSTART and RLENGTH (set by the match function) as the starting position and length of the substrings.

The split function uses the field separator specified by the 3rd parameter to split the character into an array. If no 3rd parameters are provided, awk uses the current value of FS as the field separator.
Format

Split (string, array, field separator)
Split (string, array)

Example

$ Awk 'in in {split ("12/25/2001", date, "/"); print date [2]} 'filename

Note: The split function splits the string 12/25/2001 into an array date. Use a forward slash as the field separator. The subscript of the array date starts from 1. Awk prints 2nd elements of the array date.

The sprinf function sprintf returns an expression in the specified format. You can use the format specification of the printf function in the sprintf function.
Format

Variable = sprintf ("string containing format description", expression 1, expression 2,..., expression n)

Example

$ Awk '{line = sprintf ("%-15 s % 6.2f", $1, $3); print line}' filename

Note: According to the printf specification, set the format of 1st and 3rd fields (a left-aligned string with a length of 15 and a right-aligned floating point number with a length of 6 characters ). The result is assigned to the User-Defined variable line. See printf functions.

References: http://www.linuxawk.com/jiaocheng/298.html

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.