Shell Script Programming Summary

Source: Internet
Author: User

Shell Script Programming Summary (i)

Text Processing tool awk

AWK is a column-based text Processing tool. It's powerful and has a wide range of applications in shell programming. Here's an example of how awk's common operations are explained.

To better manipulate awk, create a file abc.txt for this purpose. tab is used as the delimiter between strings. The contents of the file are as follows:

John Male 021-1111111 A

Lucy Female 021-2222222 AB

Jack Male 021-3333333 ABC

Lily Female 021-4444444 ABCD

Each column is called a field, so use $1,$2,$3 ... Said. Where $ A represents all domains, that is, the entire file.

1. Print the specified column

To output the information for the first and second columns, you need to use $ $. The specific commands are as follows:

Cat Abc.txt | awk ' {print $1,$2} '

2. Variable NF

NF is an internal variable. NF stores the number of columns in each row, that is, the total number of fields per row. Specific examples are as follows:

View the total number of fields for each row: Cat Abc.txt | awk ' {print NF} '

With this understanding, it is not difficult to understand $NF. $NF represents the value of the last field in each row. The variable $ (NF-1) represents the value of the second-to-last field in a row. And so the $ (NF-2) stands for what you see! The sample code is as follows:

Cat Abc.txt | awk ' {print $ (NF-4), $ (NF)} '

3. Intercept the specified string

The Intercept string function is substr (Specify the field, start character position, end character position). For example, get "021-" in $4, with the following command:

Cat Abc.txt | awk ' {print substr ($4,1,4)} '

If you want to get the character behind the "-", you need to use SUBSTR ($4,5), which means to intercept all characters starting with the fifth character of the $4 field. The command is as follows:

Cat Abc.txt | awk ' {print substr ($4,5)} '

4. Get the string length

The length of the internal variable can be used to get the lengths of each line string. Get the character length for each line, as shown in the following example:

Cat Abc.txt | awk ' {print length} '

Gets the string length of $ A, as an example: Cat Abc.txt | awk ' {print length ($)} '

5. Summation

For the and, examples are as follows: Cat Abc.txt | awk ' begin{total=0}{total+=$3}end{print Total} '

6. Conditional query

Output all of Jack's information. Examples are: awk ' $1== ' Jack ' {print $} ' Abc.txt

Shell Script Programming Summary

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.