Linux awk Command Summary

Source: Internet
Author: User
Tags logical operators

Linux awk Command Summary

Brief introduction:

awk is a powerful text analysis tool, with the search for grep and the editing of SED, which is especially powerful when it comes to analyzing data and generating reports. to put it simply, awk reads the file line-by-row, using spaces as the default delimiter to slice each row, and then perform various analytical processing of the cut.

1. Command line mode awk [-f field-separator] ' commands ' input-file (s) where commands is the true awk command, [-F domain delimiter] is optional. Input-file (s) is the file to be processed. In awk, each line in a file, separated by a domain delimiter, is called a domain. In general, the default field delimiter is a space without naming the-F domain delimiter. The 2.shell script inserts all of the awk commands into a file and makes the awk program executable, and then the awk command interpreter is invoked as the first line of the script, again by typing the script name. Equivalent to the first line of the shell script: #!/bin/sh can be replaced by: #!/bin/awk3. Inserts all awk commands into a single file and then calls: Awk-f awk-script-file input-file (s) where,- The f option loads the awk script in Awk-script-file, and Input-file (s) is the same as above.

If you just show/etc/passwd's account

#cat/etc/passwd |awk-f ': ' {print $} ' Rootdaemonbinsys

This is an example of awk+action, where each line executes action{print $.

-f Specifies the domain delimiter as ': '.

If you only display the/ETC/PASSWD account and the shell of the account, and the account and the shell are split by tab


#cat/etc/passwd |awk-f ': ' {print ' \ t ' $7} ' Root/bin/bashdaemon/bin/shbin bin/bin/shsys/bin/sh

If you just show/etc/passwd's account and the shell of the account, and the account is separated by a comma from the shell, and the column name Name,shell is added to all rows, add "Blue,/bin/nosh" to the last line.

CAT/ETC/PASSWD |awk-f ': ' BEGIN {print ' Name,shell '} {print $ ', ' $7} END {print ' Blue,/bin/nosh '} '

Name,shell

Root,/bin/bash

Daemon,/bin/sh

Bin,/bin/sh

Sys,/bin/sh

....

Blue,/bin/nosh

the awk workflow is done by first executing the beging, then reading the file, reading a record with the/n line break, and then dividing the record by the specified field delimiter, populating the field, $ A represents all fields, representing the first field, $n representing the nth field , and then the action action corresponding to the execution mode is started. Then start reading the second record ... Until all the records have been read, the end operation is performed.

Search all rows with the root keyword/etc/passwd


#awk-F: '/root/'/etc/passwdroot:x:0:0:root:/root:/bin/bash

Search support for the regular, for example, root start: awk-f: '/^root/'/etc/passwd

Search all lines that have the root keyword/etc/passwd and display the corresponding shell


# awk-f: '/root/{print $7} '/etc/passwd/bin/bash

Special points:

The whole thing is going forward

The first field of each row

$1-$n the nth field of the current record

NF Field number Variable

NR record number per row, multi-file record increment

FNR is similar to NR, although multiple file records are not incremented, each file starts from 1

\ t tab

\ n line break

Defining delimiters when FS begin

The record delimiter entered by the RS, which defaults to a newline character (that is, the text is entered as one line)

~ match, not accurate compared to = =

!~ mismatch, inaccurate comparisons

= = equals, must be all equal, exact comparison

! = does not equal, exact comparison

&& Logic and

|| Logical OR

+ matches 1 or more than 1

/[0-9][0-9]+/two or two or more digits

/[0-9][0-9]*/one or more numbers

FileName File name

OFS output field delimiter, default is also a space, you can change to a tab, etc.

The record delimiter for the ORS output, which defaults to a newline character, that is, the processing result is a line of output to the screen

-F ' [: #/] ' defines three separators

-F Specify delimiter

$ = After the specified delimiter, the first field, the third field, \ t is a tab

One or more contiguous spaces or tabs are considered a delimiter, meaning that multiple spaces are considered a single space

Awk-f ":" ' {print $} '/etc/passwd

Awk-f ":" ' {print $ $} '/etc/passwd//$1 output, not delimited

Awk-f ":" ' {print $1,$3} '/etc/passwd//More a comma, $ $ and $ $ use a space-delimited

Awk-f ":" "{print $" "$ $}"/etc/passwd//$1 and $ $ manually add a space separating

Awk-f ":" ' {print ' Username: "$" \t\t Uid: "$ $} '/etc/passwd//Custom output

Awk-f: ' {print NF} '/etc/passwd//show how many fields are in each line

Awk-f: ' {print $NF} '/etc/passwd//print the value of the NF field per line

Awk-f: ' nf==4 {print} '/etc/passwd//show rows with only 4 fields

Awk-f: ' Nf>2{print $} '/etc/passwd//show rows with a number of fields greater than 2 per row

awk ' {print nr,$0} '/etc/passwd//output line number per line

Awk-f: ' {print nr,nf, $NF, ' \ t ', ' $} '/etc/passwd//Print line number, number of fields, last field value, tab, contents per line

Awk-f: ' Nr==5{print} '/etc/passwd//Display line 5th

Awk-f: ' nr==5 | | Nr==6{print} '/etc/passwd//Show lines 5th and 6th

Route-n|awk ' Nr!=1{print} '//Do not display the first line

Matching code blocks

Pure character matching!//pure character mismatch ~//field value match!~//field value mismatch ~/a1|a2/field value matches A1 or A2

awk '/mysql/'/etc/passwd

awk '/mysql/{print} '/etc/passwd

awk '/mysql/{print $} '/etc/passwd//Three instructions as a result

awk '!/mysql/{print $} '/etc/passwd//output does not match MySQL line

awk '/mysql|mail/{print} '/etc/passwd

awk '!/mysql|mail/{print} '/etc/passwd

Awk-f: '/mail/,/mysql/{print} '/etc/passwd//range matching

awk '/[2][7][7]*/{print $} '/etc/passwd//matches the line that contains 27 for the number beginning, such as 27,277,2777 ...

Awk-f: ' $1~/mail/{print $ '/etc/passwd//$1 matches the specified content

Awk-f: ' {if ($1~/mail/) print '} '/etc/passwd//Same as above

Awk-f: ' $1!~/mail/{print $ '/etc/passwd//mismatch

Awk-f: ' $1!~/mail|mysql/{print $ '/etc/passwd

If statement

Must be used in {}, and the comparison content is expanded by ().

Awk-f: ' {if ($1~/mail/) print '} '/etc/passwd//shorthand

Awk-f: ' {if ($1~/mail/) {print '} '/etc/passwd//Full Write

Awk-f: ' {if ($1~/mail/) {print '} ' else {print $}} '/etc/passwd//if...else ...

logical operators

&& | |

Awk-f: ' $1~/mail/&& $3>8 {print} '/etc/passwd//logic with, ' match Mail ', and $3>8

Awk-f: ' {if ($1~/mail/&& $3>8) print} '/etc/passwd

Awk-f: ' $1~/mail/| | $3>1000 {print} '/etc/passwd//Logical OR

Awk-f: ' {if ($1~/mail/| | $3>1000) PRINT} '/etc/passwd


This article is from the "one small step per day" blog, so be sure to keep this source http://fenyuer.blog.51cto.com/11265169/1913110

Summary of Linux awk commands

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.