Shell text filtering programming (8): built-in functions of awk

Source: Internet
Author: User

[Copyright statement: reprinted. Please retain the Source: blog.csdn.net/gentleliu. Mail: shallnew at 163 dot com]

In this section, let's take a look at a w k's many powerful string functions and their usage methods.

1. sub and gsub functions:
Usage: Sub (ERE, REPL, [STR])
Gsub (ERE, REPL, [STR])
Search for the string that meets the extended regular expression specified by the ere parameter in the string given by the third parameter, and replace it with the second parameter. If the in parameter is not specified, the default value is the entire record ($0 record variable ). The sub function replaces the first conforming string, and gsub replaces all conforming strings.
# awk 'BEGIN{buf="Hello,awk! there is awktutorial!";sub(/awk/, "world", buf); print buf}'Hello,world! there is awk tutorial!# echo "Hello,awk! there is awk tutorial" | awk'{sub(/awk/, "world"); print $0}'Hello,world! there is awk tutorial# awk 'BEGIN{buf="Hello,awk! there is awktutorial!";gsub(/awk/, "world", buf); print buf}'Hello,world! there is world tutorial!# echo "Hello,awk! there is awk tutorial" | awk'{gsub(/awk/, "world"); print $0}'Hello,world! there is world tutorial#
2. Index Function
Index (string1, string2 );
Search for the string string2 In the first parameter string1, and return the first occurrence location.
# echo "Hello,awk! there is awk tutorial" | awk '{printindex($0, "awk")}'7#
3. Length function, blength Function
Length [(string)]; return the length of the string specified by the parameter, in characters. If the string parameter is not provided, the length of the entire record is returned ($0 record variable ).
# echo"Hello,awk! there is awk tutorial" | awk '{print length()}'32#
4. substr Function
Substr (string, M, [N]); returns a string of n characters starting from the string at the position of M. The m parameter specifies the first character in the string parameter as the number 1. If n is not specified, the length of the substring is the length from the position specified by the M parameter to the end of the string parameter.
# echo"Hello,awk! there is awk tutorial" | awk '{print substr($0, 5)}'o,awk! there isawk tutorial# echo"Hello,awk! there is awk tutorial" | awk '{print substr($0, 5, 6)}'o,awk!#
5. Spilt Functions
Split (string, A, [ere]); splits the parameter specified by the string parameter into array elements a [1], a [2],..., A [n] and return the value of N variable. This separator can be performed using the extended regular expression specified by the ere parameter or using the current field separator (FS special variable) (if the ere parameter is not given ). Unless the context specifies that a specific element should also have a numeric value, the elements in array a are created using string values.
# echo"Hello,awk! there is awk tutorial" | awk '{split($0, arr); for (i inarr){print i,arr[i]}}'4 awk5 tutorial1 Hello,awk!2 there3 is
Awk... An in loop is an unordered loop. It is not from the array subscript 1... N, so pay attention to it.
6. system functions
System (CMD); run the command specified by the CMD parameter and return the exit status.
# awk 'BEGIN{system("echo $HOME")}'/root
In the original awk, shell commands can also be called.
There are so many articles in the awk series that awk is excellent in Text Processing and has powerful functions. you can master the awk series to do many things, the next section describes the SED command.


Shell text filtering programming (8): built-in functions of awk

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.