Awk BASICS (Marco video), awk basics Marco

Source: Internet
Author: User
Tags file separator print format

Awk BASICS (Marco video), awk basics Marco

Awk Basics

1.1 print

Print format:

Print item1, item2 ,....

Key points:

Example:

# Awk 'in in {print "line one \ nline two \ nline three "}'

Awk-F: '{print $1, $2}'/etc/passwd

Built-in variables:

ORS (output record sepatator) (output line break)

OFS (output field separator) (output separator)

FS: field separator, which defaults to blank characters (input file separator)

RS: Record separator. The default character is a line break)

NR: The number of input records, number of records processed by The awk command: if there are multiple files, this number will count The rows of The processed files in a unified manner;

NF: Number of Field, Number of fields in the current record;

FNR: Unlike NR, FNR is used to record the rows being processed as the total number of rows being processed in the current file;

ARGV: array. Save the command line string. For example, in the awk '{print $0}' a.txt B .txt' command, the argv1_01_save awk, And the argv1_11_save a.txt;

ARGC: number of parameters of the awk command;

1.2 printf

Format of the print command:

Printf format, item1, item2 ,....

Key points:


The format indicators start with %, followed by a character:

% C: ASCII code of the displayed characters:

% D, % I: decimal INTEGER:

% E, % E: Numeric value displayed in scientific notation;

% F: displays floating point numbers;

% G, % G: The value is displayed in scientific notation or floating point format;

% S: Display string

% U: unsigned integer

%: Display % itself


Modifier:

N: display width;

-: Align

+: Displays numeric symbols.


Example:

# Awk-F: '{printf "%-15 s % \ n", $1, $3}'/ect/passwd

1.3 output redirection

Printitems> output-file

Printitems> output-file

Special file descriptor:

/Dev/stdin: Standard Input

/Dev/stdout: standard output

/Dev/stderr: Error output

/Dev/df/N: a specific file descriptor. For example,/dev/stdin is equivalent to/dev/fd/0;

Example

Awk-F: '{printf "%-15 s % I \ n", $1, $3> "/dev/stderr"}'/etc/passwd

1.4 awk

Awk format:

Awk [options] 'script' file1, file2 ,....

Awk [options] 'pattern' {action} 'file1, file2 ,....

-F:


1. arithmetic operators:

-X: negative value

+ X: convert to a value

X ^ y:

X ** y: Power

X * y:

X + y:

X-y:

X % y:


2. String operators:

There is only one, and you do not need to write it out to implement string connection:

3. Value assignment operator:

=, + =,-=, * =,/=, % =, ^ =, ** =

++ ,--

It should be noted that if a mode is equal to =,/=/may cause a syntax error, which should be replaced by/[= ]/.

4. Boolean Value

In awk, any non-0 value or non-null string is true, and vice versa, it is false;

5. Comparison operators:

X <y, x <= y, x> y, x> = y, x = y, x! = Y, x ~ Y, x !~ Y

6. Logical Relationship Between Expressions:

&, |

7. conditional expressions

Selector? If-true-exp: if-false-exp

$ A> $ B? Echo $ A: echo $ B

8. Number of lines called

Function_name (para1, para2)


9. awk mode:

Awk 'program 'input-file1 input-file2 ....

The program is:

Pattern {action}

Pattern {action}

....

Common Mode types:


Common actions include:


/Regular expression/: Extension Set with wildcard characters

Relational Expression: You can use the Relational operators in the following operator table to perform operations. It can be a string or a large number of values, for example, $2> $1, select a row whose second field is longer than the first field.

Awk-F: '$3 >= 500 {print $1}'/ect/passwd

Awk-F: '$3 >= 500 {print $1, $3}'/ect/passwd

Awk-F: '$3 >=500 {printf "%-15 s % s \ n", $1, $3}'/ect/passwd

Awk-F: '$3 >= 500 {print "Username UID"; printf "%-15 s % s", $1, $3}'/ect/passwd


Expression for pattern matching:

Mode: Specifies the range of rows. This syntax cannot include the BEGIN and END modes.

BEGIN: Specifies the action that occurs before the first input record is processed. You can set global variables here.

END: The action that occurs after the last input record is read.

Awk-F: 'In in {print "Username UID"} {printf "%-15 s % s", $1, $3} '/ect/passwd

Awk-F: 'In in {print "Username UID"} {printf "%-15 s % s", $1, $3} END {print "Over} '/ect/passwd

Awk 'in in {print "a" "B "}'

Awk-v FS =: '{print $1}'/etc/passwd

Awk 'in in {FS = ":"} {print $1} '/ect/passwd



Example:

Vima.txt

Welcometo redhat linux.

Howare you?

Awk '{print $1}' a.txt

Df-h

Df-h | awk '{print $1 }'

Df-hP | awk '{print $1 }'

Awk '{print $1}'/etc/passwd

Awk-F: '{print $1}'/etc/passwd

Awk-F: '{print $1, $7}'/etc/passwd

Awk-F: '{print $ NF}'/etc/passwd

Awk '{print $ NF}' a.txt

Awk-v FS =: '{print $ NF}'/etc/passwd

Awk '{print $1 $2}' a.txt

Awk '{print $1, $2}' a.txt

Awk-v OFS =: '{print $1, $2}' a.txt

Awk '{printf "%-10 s, % s \ n", $1, $2}' a.txt

Awk '{printf "%-10 s % s \ n", $1, $2}' a.txt

Awk 'in in {print "a" "B "}'

Awk-F: '$1 ~ /^ Root/{print $3, $4, $ NF} '/ect/passwd

Awk-F: '$1 !~ /^ Root/{print $3, $4, $ NF} '/ect/passwd

Awk-F: '/bash/{print $0}'/etc/passwd

Awk-F: '/bash/{print $1}'/etc/passwd


Control statement

Syntax: if (condition) (then-body) else {[else-body]}

Example:

Awk-F: '{if ($1 = "root") print $1, "admin"; else print $1, "Common User"}'/ect/passwd

Awk-F: '{if ($1 = "root") printf "%-15 s: % s \ n", $1, "Admin "; elseprintf "%-15 s: % s \ n", $1, "Common User"} '/ect/passwd

Awk-F:-v sum = 0' {if ($3> = 500) sum ++} END {print sum} '/ect/passwd


  1. While

Syntax: while (condition) {statements1; statements2 ;......}

Awk-F: '{I = 1; while (I <= 3) {print $1; I ++}'/ect/passwd

Awk-F: '$1 !~ /Root/{I = 1; while (I <= 4) {print $1; I ++} '/ect/passwd

Awk-F: '$1 !~ /Root/{I = 1; while (I <= NF) {print $1; I + = 2} '/ect/passwd

Awk-F: '$1 !~ /Root/{I = 2; while (I <= NF) {print $1; I + = 2} '/ect/passwd


  1. Do-while

Syntax: do {statements1; statements2 ;......} While (condition)

Awk-F: '{I = 1; do {print $ I; I ++} while (I <= 3)}'/etc/passwd

  1. For

Syntax: for (variable assignment; condition; iteration process) {statements1; statements2 ;...}

Awk-F: '{for (I = 1; I <= 3; I ++) print $1}'/ect/passwd

Awk-F: '{for (I = 1; I <= NF; I + = 2) print $ I}'/ect/passwd

The for loop can also be used to traverse array elements:

Syntax: for (I in array) {statements1; statements2 ;......}

Awk-F: '$ NF !~ /^ $/{BASH [$ NF] ++} END {for (A in BASH) {printf "% 15 s: % I \ n",, BASH [A]} '/ect/passwd

  1. Case

Syntax: seitch (expression) {case VALUE or/ERGEXP/: statements1; statements2 ;... Default statements0}

  1. Break and continue

It is often used in loop or case statements.

  1. Next

Process the text of this line ahead of schedule, and then process the next line.


Awk built-in variables:

FS: field separator. By default, it is a blank character;

RS: Recordseparator. By default, it is a line break character;

NR: Thenumber of input records, number of records processed by the awk command: if there are multiple files, this number will count the rows of the processed files in a unified manner;

NF: Numberof Field, number of fields in the current record;

FNR: Unlike NR, FNR is used to record the rows being processed as the total number of rows being processed in the current file;

ARGV: array. Save the command line string. For example, in the awk '{print $0}' a.txt B .txt' command, the argv1_01_save awk, And the argv1_11_save a.txt;

ARGC: number of parameters of the awk command;

ForI in {0 .. 10}

PrintA [$ I]


For (A in ARRAY) {print ARRAY [A]}

Awk 'in in {A ["m"] = "hello"; A ["n"] = "world"; for (B in A) print A [B]}'

Use arrays in awk

Array [index-expression]

Index-expression can use any string. Note that if a data group element does not exist, awk will automatically create this element and initialize it as an empty string during the time of application. Therefore, to determine whether an element exists in a data group, you must use the indexin array method.


To traverse every element in the array, use the following special structure:

For (var in array) {statement1 ,...}

Var is used to reference the array subscript.

Example:

Netstat-n | awk '/^ tcp/{++ S [$ NF]} END {for (a in S) print a, S [a]}'

Netstat-ant | grep "LISTEN" | wc-l

Netstat-ant | awk '$1 ~ /Tcp/{S [$ NF] ++} END {for (A in S) print A, S [A]}'

Netstat-ant | awk '$1 ~ /Tcp/{S [$ NF] ++} END {for (A in S) printf "% 10 s: % s \ n", A, S [A]}'

Awk-F: '$ NF !~ /^ $/{SHELL [$ NF] ++} END {for (A in SHELL) printA, SHELL [A]} '/etc/passwd

Awk '{IP [$1] ++} END {for (A in IP) print A, IP [A]}'/usr/local/apach/logs/access_log

For each row that is matched by the/^ tcp/mode, the array S [$ NF] is added. NF is the last field of the currently matched row, here, its value is used as the element index of array S;


Awk '{count [$1] ++}; END {for (url in counts) print counts [url], url}'/var/log/httpd/access_log

The usage is the same as that in the previous example. It is used to count the access volume of IP addresses in log files.


Awk 'in in {A [x] = "hello"; A [y] = "world"; print A [x], A [y]}'


Awk built-in functions

Split (string, array [, fieldsep [, seps])

Function: splits string with filedsep as the separator, and saves the results to the array named array.

Netstat-ant | awk '/: 80/{split ($5, clients ,":"); IP [client [1] ++} END {for (I in IP) {print IP [I], I} '| sort-rn | head-50

Cut the fifth column of the netstat query result with the separator, save the cut result in the client array, and accumulate.


Length ([string])

Function: returns the number of characters in a string.


Substr (string, start [, length])

Function: Take the substring in the string, start from start, Take length, start from 1 to count;


System (command)

Function: Execute the system command and access the result to the awk command.

Systime ()

Function: obtains the current system time.


Awk [options] 'pattern' {cation} 'input_file


How can we learn JAVA better? What books and videos do you read?

Practice
 
The teaching video of Linux kernel programming shows that there is no basis for learning to compile some c and

Learn the basics first and then look at the kernel stuff.
 

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.