Linux Text Search-awk article

Source: Internet
Author: User

I. Introduction of AWK

Awk is a bit more unfamiliar than grep, but it's more powerful and more flexible to handle column-structured text data, because it allows for meticulous processing of row data, such as splitting text into multiple fields by specific characters, and then making the next slice of each field. It also supports C syntax, which can be used as a scripting language.

Second, basic grammar

awk usage: awk ' pattern {action} ' files

ARGC Command Line arguments
ARGV command-line array of elements
FileName current Input file name
FNR the record number in the current file
FS input domain delimiter, default is a space
RS Input Record delimiter
NF number of fields in the current record
NR Record Count so far
OFS output Field delimiter
ORS Output Record delimiter

Simple example:

awk '/abcd/{print $1,$2} ' a.txt

Print the first to second column of the file line that contains the ABCD

Third, conditional operator

<, <=, = =,! =, >=, ~ match Regular expressions,!~ mismatched regular expressions
awk ' {if ($4~/asima/) print $} ' temp

Indicates that if the fourth column contains Asima, the entire bar is printed
awk ' $ ~/asima/' temp

It means that as long as the entire article contains Asima, print it
awk ' $3== ' 48″{print $} ' temp

Print only the 3rd column equals "48″" record
awk ' $!~/asima/' temp

Print an entire record that does not contain Asima
awk ' $ = ' Asima ' temp
awk ' {if ($1<$2) print $ "is smaller"} ' temp
awk '/[gg]reen/' temp

Print a whole strip of records containing green, or green
awk ' $ ~/^...a/' temp

Print the fourth character in the 1th field is a record, ^ the beginning of the line,. Any character
awk ' $0~/(ABC) | (EFG)/' temp

When using |, the statement needs to be enclosed.
awk ' {if ($1== "a" && $2== "B") print $} ' temp
awk ' {if ($1== "a" | | $1== "B") print $} ' temp

Four, built-in string processing functions

Gsub (R,s) replaces R with s in the entire $
awk ' gsub (/name/, "xingming") {print $} ' temp
Gsub (R,S,T) replaces R with S in the whole t
Index (S,T) returns the first position of the string T in S
awk ' BEGIN {print index ("Sunny", "NY")} ' temp returned 4
Length (s) returns the lengths of S
Match (S,R) tests if s contains a string that matches R
awk ' $1== ' J.lulu ' {print match ($, "U")} ' temp returns 4
Split (S,A,FS) divides s into sequence a on FS
awk ' BEGIN {print split ("12#345#6789″,myarray," # ")" '
Return 3, while myarray[1]= "12″, myarray[2]=" 345″, myarray[3]= "6789″
Sprint (FMT,EXP) returns the EXP formatted by FMT
Sub (r,s) replaces R with S in the leftmost longest substring of $ A (replaces only the first encountered match string)
SUBSTR (s,p) returns the suffix part of the string s starting from P
SUBSTR (s,p,n) returns the suffix part of the string s starting from p in length n

Linux Text Search-awk 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.