search
Homephp教程PHP开发AWK command usage summary

AWK command usage summary

Dec 15, 2016 am 10:21 AM
awk command

awk's most basic function is to browse and extract information based on specified rules in files or strings. It is used to divide a line into several "fields" for processing. Suitable for processing small data.

Example:

Cat /etc/passwd | awk '{FS=":"}$3


execution method, such as:

awk [-Field-separator] 'commands' input-file(s)

here commands Is the real awk command, [-F domain separator] is optional, awk uses spaces to separate by default, so if you want to browse text with spaces between fields, you do not need to specify this option, but if you browse a file such as passwd, this file will If the domain uses a colon as a separator, you must use the -F option: awk -F : 'commands' input-file


When awk is executed, its browsing marks are $1, $2...$n, this method is called Label the domain. Use $1 and $3 to refer to the 1st and 3rd fields. Note that commas are used to separate the fields, and $0 means to use all fields. For example:

awk '{print $0}' temp.txt > sav.txt

means to print all fields and redirect the results to sav.txt

awk '{print $0}' temp.txt | tee sav.txt

Similar to the above example, the difference is that it will be displayed on the screen

awk '{print $1,$4}' temp.txt

Only print out the first 1 and 4th domain


awk 'BEGIN {print "NAME GRADEn----"} {print $1"t"$4}' temp.txt


means to enter the header, that is, input Add "NAME GRADEn-------------" before the first line of the content, and separate the content with tabs

awk 'BEGIN {print "being"} {print $1} END {print "end"}' temp

Print the information header and information tail at the same time



Conditional operator:


 =, ~ matches regular expressions, !~ does not match regular expressions

Matching: awk '{if ($4~/ASIMA/) print $0}' temp means that if the fourth field contains ASIMA, then Print the entire

Exact match: awk '$3=="48" {print $0}' temp Only print the record where the 3rd field is equal to "48"

Mismatch: awk '$0 !~ /ASIMA/ ' temp Print the entire record excluding ASIMA

Not equal to: awk '$1 != "asima"' temp

Less than: awk '{if ($1

Set case: awk '/[Gg]reen/' temp Print the entire record containing Green, or green

Any character: awk '$1 ~/^...a/' temp Print the record in which the fourth character in the first field is a. The symbol '^' represents the beginning of the line, and '.' represents any character

or relational matching: awk '$0~/(abc)|(efg)/ ' temp When using |, the statement needs to be enclosed

AND and relationship: awk '{if ( $1=="a" && $2=="b" ) print $0}' temp

OR or relationship: awk '{if ($1=="a" || $1=="b") print $0}' temp

awk built-in variables:

AWK command usage summary

Example: awk 'END {print NR}' temp in Finally print the number of read records

awk '{print NF, NR, $0} END {print FILENAME}' temp

awk '{if (NR>0 && $4~/Brown/) print $0} ' temp There is at least one record and contains Brown

Another usage of NF: echo $PWD | awk -F/ '{print $NF}' Display the current directory name



Running mode: awk 'Condition type 1 {action 1} condition type 2 {action 2} ...' filename

# last | awk '{print $1 "t" $3}'

NF The total number of fields in each row ($0)

NR What row of data is currently processed by awk

FS specifies the separator, the default is a space bar


More AWK command usage summary For related articles, please pay attention to the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor