11 shell command awk, 11shell command awk

Source: Internet
Author: User
Tags processing text

11 shell command awk, 11shell command awk

Awk, as an amazing command, can help us solve seemingly complex and difficult problems in minutes.

Awk, as its name implies (I am so embarrassed ). is a tool for processing text lines. just kidding, this command cannot be the name suggests. it is said to be the first letter of the names of the three developers. foreigners are crazy.

Awk [option] 'pattern{ action} 'filename

Here, pay attention to a detail. option is optional. In addition, when we learn sed, double quotation marks can all be single quotes, but in awk, we can only use single quotes.

Options:

-F specifies the pattern file. That is to say, you can write some pattern to a file and use-f to specify it. This is the so-called awk programming.

-F specifies the delimiter. multiple separators can be specified at a time. If not specified, blank characters are used as the delimiter by default.

-V initializes the awk variable. This should be a method of using external variables, similar to the form parameters in C language. Although variables are introduced, they cannot be transmitted.

Pattern {action}

There are two special pattern, BEGIN and END. we know that awk is a command for processing text lines. Therefore, the number of executions of normal pattern {action} is the number of lines of the specified file currently. BEGIN and END are executed only once, and BEGIN is executed before processing all rows. END is executed after processing is complete.

Pattern: defines a rule. As long as it complies with this rule in a line of text, {action} is executed. If pattern is empty, it is executed by default.

1. All rows whose NR <5 conditions meet NR smaller than 5 will be processed

2. NR = 1, NR = 4 will process the first row to the fourth row

3./awk/process rows containing awk

4 .! /Awk/process rows that do not contain awk

Action: Specifies the operations performed on text lines.

It has powerful functions and supports if, while, and for flow control statements.


So Nervous, such a cool command will be a mess I wrote. Let me learn from the following...

In single quotes, awk defines its own built-in variables and built-in functions.

Built-in variables:

Number of ARGC command line parameters
ARGV command line parameter arrangement
ENVIRON supports the use of system environment variables in the queue
FILENAME awk browsed file name
Number of FNR browsing file records
FS sets the input domain separator, which is equivalent to the command line-F Option
Number of NF browsing records
Number of records read by NR
OFS output domain Separator
ORS output record Separator
RS control record delimiter

The $0 variable indicates the entire record.

$1 indicates the first domain of the current row, $2 indicates the second domain of the current row, and so on.

Built-in functions (Part 1 ):

Length (string) is the length of string.

Substr (string, start-position, end-position) returns the substring between start-position and end-position.

Print records

Index (string, search-string) returns the position where search-string appears in string.

Split (string, array, delimeter) Splits string by delimeter and stores the obtained content in array.

Sub (regex, replacement, string) replaces the substring corresponding to the regex Regular Expression in the string with replacement

Gsub (regex, replacement-str, string) is familiar with g, global meaning, that is, all matched replace.

Match (regex, string) returns 0 if not matched, and returns non-0 if matched.

Next, let's take a look at awk in combination. I will try to use the scenario described above:

1. Specify multiple separators. Match the text lines in the third column of linux and print one or two lines:

2. split text using split:

3. there is a command called tac. It looks awkward, right. that's right. cannot match with words at all. cat. That's right. This command is to print the lines in reverse order. try it with awk.

4. Process 1 or 2 rows of data and print


OK. Although the introduction is not comprehensive, I believe it can help some of my colleagues. This command is too complicated. I am very pleased to write a blog post.


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.