awk Command Personal Summary (in CentOS 6.3 environment shell for example)

Source: Internet
Author: User
Tags regular expression centos
awk Command Personal summary (in CentOS 6.3 environment shell for example) recently learned a little bit about the basics of the awk command in the shell. Below, according to what you learn to do some summary, only for the future. If there is any mistake, please correct me. what awk is.

Awk is actually a kind of programming language, carefully expand open can have a lot of content, and I learned a little fur, may not even get started. With Linux, awk can be used to filter and scan some text data. how awk works.

Awk can typically be used with pipe characters, which will be scanned for text files sent to awk. The name of the file is given to $ A, which is the first positional variable. Awk scans a line of text at a time, from the first line to the last line, looking for rows that match the requirements, for user action. The default delimiter for awk is a space or tab, each line is separated by a delimiter, divided into columns, or, more accurately, data segments, each given $, $ ...
The basic structure is as follows

       ' awk ' condition 1{operation 1} condition 2{operation 2} ' file name

Condition: Typically a relational expression
Action: The default processing is two special conditions for the print printout to process the line awk

Begin and end, which are placed in a conditional position, enable the instruction to begin and end to be performed before or after other commands are executed. Let me give you some examples below. basic options for awk

-f Specifies the delimiter,-F followed by the specified delimiter-F specified script, the script file is appended to-F, and
the awk command is read from the script instead of the input
examples of awk use

1. The regular expression matches the blank lines in the entire test document, and each blank line prints "blank Lines"

awk '/^$/{print ' blank line '} ' test

2. The regular expression matches the hostname line in the/etc/sysconfig/network, and prints the host name

awk '/hostname/{print} '/etc/sysconfig/network

3. Invoke an awk script with the-f option to operate on the Sample.txt, and awk scripts are test.sh

Awk-f test.sh Sample.txt

4. Output a,b,c,$1 $ a,b,c for each output

echo a b C | awk ' {print $1,$2,$3} '

5. Read the input rows and output the number of data segments

echo a b C | awk ' {print $} '

6. Specify that the delimiter is ":", output the first column of the passwd text, which is the user name of the system

Awk-f ":" ' {print $} '/etc/passwd//1
awk ' BEGIN {fs= ': '}{print '} '/etc/passwd//notation 2

Variable in 7.awk, default is empty string or 0

awk '/^$/{x+=1} END {print x} ' Test//Count blank row rows and output

8.awk is used with conditional statements, because AWK uses the begin pattern, so the instruction behind begin is executed before the content of the test document is read.

awk ' {if ($1<10) print "a"; else print "B"} ' test  
///If the first parameter of each row in the test file is less than 10, output A, otherwise output B

awk ' I=1 {} begin{while ( i<=10) {++i;print i}} '   
//i initial value is 1, when i<=10, output I value, i++

awk ' BEGIN {do {++x;print x} while (x<=10)} ' 
 //d O-while format

awk ' begin{for (i=1;i<=10;i++) Print i} '   //for loop
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.