Use of awk in Linux (Absolute Essence Edition)

Source: Internet
Author: User
Tags arithmetic operators

Awk is absolutely the core tool in Linux, especially in the search field, as important as mastering the SED command.

Here are some of the basics of awk that, based on this knowledge, allow you to manipulate a file at will:

In awk: () bracket is a conditional block, {} is a command block for execution

Print command
Print file contents
awk ' {print} ' datafile

-F parameter
Execute an awk script, such as script Hello.awk
Awk-f Hello.awk datafile

$number
field that represents the record, where the whole string

Fs
Represents the field delimiter, as follows a comma as a delimiter
awk ' begin{fs= ', '}{print} ' datafile
When you write a script file, you typically use FS

-F parameter
As with FS, set the delimiter, such as
Awk-f "," ' {print $} ' datafile

Nf
Represents the number of fields in the current record row

Nr
Number that represents the current record.

FNR
Number of records currently browsing files

Fs= "\t+"
Indicates that one or more tabs are used as separators

OFS
character separators, inserting separators between two fields

Rs
Record delimiter, that is, a line separator

ORS
Output record delimiter, between two records (ROW) insert

Output format for OFMT number

CONVFMT internal conversion format for numeric values

Boolean expression
Awk-f "," ' $1== "Hello" {print $} ' datafile
Only the first parameter value is Hello, the second one is printed

Length (string1)
return string1 length

Index (STRING1,STRING2)
Returns the position of string2 in string1, no return 0

ToLower (string1)/toupper (string1)
Returns case

SUBSTR (string1,5,3)
Intercept a field of length 3 starting from the 5th position of string1

Match (string1,/you/), rstart,rlength
Match matches a regular expression
Rstart returns the first matching position
RLENGTH specifies the span of characters he occupies, not 1

Gsub/sub (/my/, "You", string1)
Gsub Global substitution, sub replaces only the first
As above, find my in string1 and replace it with you

Split (String1,strarray, ",")
Print Strarray[1],strarray[numelements]
Divide the string1 into a comma and pass it to the Strarray group
Numbering starting from 1

Delete duplicate rows (must be sorted first)
Sort Datafile|awk ' {if ($0!=line) print;line=$0} '

FILENAME
Special presence in awk, used to represent a file
awk ' End{print FILENAME} ' datafile
Print file name
If more than one input file can be used to determine, such as:
awk ' if (filename== "File1") {arr[$3]=$1}else{($ in arr) {print arr[$1] "|" $ file1}} file2
Note: awk processes the previous file before processing the subsequent file

Argind
Currently processed parameter markers

ARGC
Number of command line arguments

Argv
Command line parameter arrangement

ENVIRON
Support for the use of environment variables in queues

Next
The next command does not execute
awk ' (nr%2=1) {next}{print} ' F1
awk ' Nr==fnr{a[$0]=$0;next}a[$0]{print a[$0],$0}

Nextfile
Interrupt the current file processing and go to the next file processing:
awk ' {print filename;nextfile} f1 f2
Print two filenames, more for finding files

Exit
Stop awk and exit after executing the end statement block

To define a function:
awk ' {print ' sum= ', Sqrtsum ($1,$2)}function sqrtsum (x, y) {return x*x+y*y} ' file
awk ' {print ' sum= ', $1*$1+$2*$2}

-V
Load parameters in an environment or shell
Awk-f \| -V Orahome=${ora_home} ' {print $ ' | Orahome> "Datafile1"} datafile
Or as follows:
Awk-f \| ' {print ' | ' Orahome> "Datafile1"} Orahome=${ora_home} datafile

Getline
Get the line contents of a file, usually in begin
A getline gets a row, or it can be saved to a variable
awk ' Begin{getline var1}end{print var1} ' file

Print the line after 5 lines:
awk ' nr>5 ' file
Print 2 to 6 lines:
awk ' nr==2,nr==6 ' file
Print the first 5 lines:
awk '! (nr>5) ' File
awk ' nr<6 ' file
Print 5 times times the number of lines:
awk ' (nr%10==0) {print} ' file
awk '! (NR%10) {print} ' file

The related operators in awk use

~ operator
Two side matches, can be blurred compared, such as:
awk ' begin{fs= ' | '} ($2~/. my./) {print} ' file

Comparison operators
= = Two sides equal
>
>=
<
<=
! = Both sides are not equal
~ Match Regular expression
!~ does not match regular expressions
‖ Logic or
&& Logic and

Arithmetic operators
+ Addition
-Subtraction
* Multiplication
/Division
^ Exponential algorithm
% modulo algorithm
--Self-1 (back and forth minus)
+ + from +1 (pre-and post-add)
+ = auto-plus algorithm
-= self-reduction algorithm
*= Squared algorithm
/= Auto-removal algorithm
^= Self-exponential algorithm
%= Self-Modeling algorithm

Number of empty lines in statistical text
awk ' Begin{x=0}/^$/{x+=1}end{print x} ' datafile
/^$/represents a blank line

Regular Expressions:
\ escape Character
^ Line break
$ line Tail character
. Match one character
[] matches one of the characters
| or operation
() Judgment statement
* Match 0 or more of the preceding characters
+ Match one or more of the preceding characters
? Frequency of Match pattern occurrence


If statement
if ($1== "foo") {
Print "Foo"
}else if ($1== "bar") {
Print "Bar"
}else{
Print "Other"
}

Do.. While statement
{
Count=6
do{
Print Count
count--
}while (count! =1)
}

While statement
{
while (X<NF) {
Print $x
X + +
}
}


For statement
for (x=1;x<=5;x++) {
Print X
}

Use of awk in Linux (Absolute Essence Edition)

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.