Linux/awk/usage

Source: Internet
Author: User

<title>Linux <i>awk</i> Usage</title> Linux awkUsagetable of Contents
    • 1. Sum a specific column:
    • 2. Sum the columns of the lines with additional conditions (starting with 7):
    • 3. Output the line whose value in the 3rd column is 1:
    • 4. Output the line whose $ < $ > 20:
    • 5. Count the lines starting with 7:
    • 6. Output the line containg number 765
    • 7. Output the line containing number 765 with the line number in front of this line:
    • 8. Output the lines not containing number 765:

In this post, I am going to record the common patterns of using awk, theamazing tool for processing file.
Assume I has a file (BB) with the following contents:
70 23 2757 35 3765 56 3765 56 123 411 3

1Sum a specific column:
awk ' {sum + = ' {} END {print sum} ' bb
2Sum the columns of the lines with additional conditions (starting with 7):
awk '/^7/{print $} ' bb | awk ' {sum + = $ '} END {print sum} '
3Output the line whose value in the 3rd column is 1:
awk ' $ = = 1 ' bb
4Output the line whose $ < $ > 20:
awk ' {$1<80 && $2>20;print} ' bb
5Count the lines starting with 7:
awk '/^7/{x + +;} END {print x} ' bb
6Output the line containg number 765
awk '/765/' bb
7Output the line containing number 765 with the line number in front of this line:
awk '/765/{print NR ': ' $} ' bb

In the above command, the NR are the built-in variable that represents the Linebeing processed, the $ refers to the whole line.9

8Output the lines not containing number 765:
awk '!/765/' bb

Author:wujing

CREATED:2015-01-23 Five 21:22

Emacs 24.3.1 (ORG mode 8.2.10)

Linux/awk/usage

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.