Recently saw a few good small examples in the forum, for learning awk is still helpful, here in detail
A. A LEFT join query in a similar database
Copy Code code as follows:
[Root@krlcgcms01 mytest]# cat A.txt//a.txt
aaa
222 BBB
333 CCCC
444 DDD
[Root@krlcgcms01 mytest]# cat B.txt//b.txt
111 123 456
2 ABC CBD
444 RTS 786
Required output result is111,aaa,123,456444,ddd,rts,786
Implementation method:
Co
nameIncludeAll records of "zhangsan,
$ awk '{if ($1~/zhangsan/)print $0}' record.txt
Or
$ Awk '$1 ~ /Zhangsan/'record.txt # all fields that meet the conditions are printed by default.
Common operators and regular expressions in conditional operations
+,-, *,/, %, + =,-=, % =, ^ = ++ ,--~ Fuzzy match, that is, ** include **!~ Mismatch = exact match>,
List Regular Expressions
[Gg] reen or (Green | green) ma
Tag: Use the number family font else to manipulate the line-wrap output rangeI. AWK syntaxawk [options] ' commands ' filesOption-F defines the field delimiter, and the default delimiter is a contiguous space or tabDefine the interval symbol using the-f parameter in optionUse the order of $1,$2,$3 to represent the different fields of each row in files separated by the interval symbolNF variable indicates the number of fields in the current record-V Def
"," ' {for (i=1;i3.awk built-in variables:Example: awk ' end {print NR} ' temp Prints the number of read record strips at the endawk ' {print nf,nr,$0} END {print FILENAME} ' tempawk ' {if (nr>0 $4~/brown/) print $} ' temp has at least one record and contains BrownAnother
processedExample:Specifying delimiters using FS[[email protected] ~]# awk ' begin{fs= ":"} {print $1,$7} '/etc/passwdRoot/bin/bashBin/sbin/nologinDaemon/sbin/nologinAdm/sbin/nologinLp/sbin/nologinSync/bin/syncStatistics/etc/passwd: File name, line number per line, number of columns per row, corresponding full line contents[Email protected] ~]# awk-f ":" ' {print ' filename: "filename", linenumber: "
Tags: field action Group font water indent more than a few group elements newLinuxThree Musketeers--Awkdking~ Sharing1.1 awkExecution ProcessAwk reads in the first line of contentdetermine if the conditions in the pattern are met nr>=2(whether to let you enter the gate)if the match executes the corresponding action {Print $}if the condition is not matched, continue reading the next lineContinue reading next linerepeat the process until the last line i
Awk processes multiple rows of records and awk processes multiple rows
The records of input files used in all our examples are composed of a single row. In this section, we will demonstrate how to read a record, and each field in the record is composed of a single row.
We have learned an example of a file that processes the name and address. Let's assume that the same data is saved in a block format file. I
Each awk statement consists of a pattern and corresponding actions. The execution of the pattern control rule in the awk-a rule is executed when its pattern matches the current input record.
Special begin and end modes:
They provide awk
ProgramStart and end actions,
The begin and end rules must have actions. They are executed only once in the program.
For ex
do the delimiter, and then take out the result, that is, a space or a colon can be regarded as the same delimiter.Summary: This method is very convenient, students can use more, select the Special keyword ' inet addr ' filter that contains the IP address line and then use awk with spaces and: Do the delimiter at the same time, and then take out the required content.The biggest drawback of this answer is to calculate the $n time is very laborious, bec
support the use of system environment variables in queues filename awk browses the file name Fnr the number of records to browse files FS- F option NF Browse record number of fields nr Read records ofs output field delimiter ors Output record delimiter Rs control record delimite
/{print $1, $2}/Chet/{S = $3 + $4 + $5; print "Chet contribution:", s}$3 ~ /250/{print}
[Chlaws @ mylinux awk] $ awk-F facts datafile
###############Chapter6:##############Practices:1: print the first and last names of those who contributed over $100 in the first month.2: print the names and phone numbers of those who contributed less than $60 in the first month.3: print those who contributed between $90 an
action (without specifying an action, the default output of the contents of each row).Search support for the regular, for example, root start: awk-f: '/^root/'/etc/passwdSearch all lines that have the root keyword/etc/passwd and display the corresponding shell# awk-f: '/root/{print $7} '/etc/passwd /bin/bashAction{print $7} is specified here.awk built-in variablesAwk has many built-in variables
Command
4. AWK's Environment variables
Table 1. Environment variables for awk
Variable description
$n the nth field in the current record, separated by FS.
$ complete Input record.
The number of ARGC command-line arguments.
The location of the current file in the Argind command line, starting at 0.
ARGV the array that contains the command-line arguments.
CONVFMT numeric conversion format (default value is%.6g)
ENVIRON an associative array of enviro
character group (not matching each character in the character group)^ [^] Rows starting with a character in a non-character group[A-z] lowercase letters[A-Z] capital letters[A-Z] lowercase and uppercase letters[0-9] Number\ \> End of a word
Extended Regular Expressions? Zero or one leading character+ One or more leading charactersAbc | def abc or defA (bc | de) f abcf or adefX \ {m \} x appears m timesX \ {m, \} x appears m times at most (at least m times)X \ {m, n \} x appears m to n times
/'/etc/passwdSearch all lines that have the root keyword/etc/passwd and display the corresponding shellawk ' /root/{print $7} ' /etc/passwd /bin/bashAction{print $7} is specified here.awk built-in variablesAwk has many built-in variables for setting up environment information, which can be changed, and some of the most commonly used variables are given below.ARGC command-line arguments argv command-line parameter arrangement environ support the
each row).Search support for the regular, for example, root start: awk-f: '/^root/'/etc/passwdSearch all lines that have the root keyword/etc/passwd and display the corresponding shell# awk-f: '/root/{print $7} '/etc/passwd/bin/bashAction{print $7} is specified here.Log statisticsFind the IP call address-to-multiple IP in the Tomcat access log, top 100 in descending orderCat access_log.txt |grep '/loan/sho
awk ' condition type 1 {Action 1} condition type 2{action 2} ... ' filenameNF total number of fields per row ($)NR awk is dealing with data from a rowFS input field split character, default is spaceRS input Line delimiterOFS output Field delimiterORS output Line delimiterFNR respective statistic linesBEGIN indicates that there are no files behind , only demos.-F
the input stream is read. Three statement blocks are optional. If the pattern statement block is not provided, each read-to line is printed by default.Special variables for awk:NR: Indicates the number of records that corresponds to the current line number during execution.NF: Indicates the number of fields in the execution process relative to the current row.$NF: Represents the last field of the current row. $ (NF-1) represents the second-to-last field of the current row. In turn$: This variab
fully read;[3] Step three: Execute the end{commands} statement block when reading to the end of the input streamThe BEGIN statement block is executed before awk begins to read rows from the input stream, which is an optional block of statements. For example, the initialization of variables, the table-top statement of the printout table can usually be written in the BEGIN statement block.The end statement block executes after
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.