当前位置:网站首页>Awk getting started to proficient series - awk quick start
Awk getting started to proficient series - awk quick start
2022-07-03 21:58:00 【Educatable】
brief introduction
AWK Is an excellent text processing tool ,Linux And Unix One of the most powerful data processing engines available in the environment . This programming and data manipulation language ( Its name comes from its founder Alfred · AI Hou 、 Peter · Weinberg and Brian · The first letter of colingham's surname ) The greatest function of depends on one's knowledge .awk A new version of the improved version nawk,gawk, Now default linux The daily use of the system is gawk, You can use the command to view the awk The source of the (ls -l /bin/awk )
A simple example
# Create a file
vim awk.txt
Beth 4.00 0
Dan 3.75 0
Kathy 4.00 10
Mark 5.00 20
Mary 5.50 22
Susie 4.25 18
Field explanation : full name Hourly wage Working hours
# Print each employee's name and compensation
awk '$3>0 {print $1,$2*$3}' awk.txt
$3>0 Is the mode
print $1,$2*$3 It's action
# Want to know which employees are lazy
awk '$3==0 {print $1}' awk.txt
Execution result diagram :
AWK Program structure
- AWK Program execution flow
awk The basic operation of is in a sequence of input lines , Scan each line in succession , Search can be " Pattern " matching (match) The line of . If it matches, execute " action ", Continue until all inputs are read
- Pattern - Action analysis
(1) Pattern - Actions exist awk '$3==0 {print $1}' awk.txt
(2) Mode of existence , Action doesn't exist awk '$3==0' awk.txt
(3) Mode does not exist , Action exists awk '{print $1}' awk.txt
(4) Neither can exist ( Can't run )
AWK The running format of the command
# Followed by documents
awk '$3==0 {print $1}' awk.txt It's followed by a file
awk '$3==0 {print $1}' awk.txt awk02.txt Followed by two files
# Waiting for input
awk '$3==0 {print $1}' There is no file after it , Wait for input before judging
# take awk Put the program into the file
cat program
$3==0 {print $1}
Carry out orders : awk -f program awk.txt
AWK The output format of
- data type
Numbers and strings
- Rows and fields
awk Read one line at a time from its input , Break the rows into fields ( By default, the field is regarded as a sequence of non blank characters ).
The first field of the current input line is called $1, The second is $2, By analogy , A whole line is written as $0, The number of fields in each row may be different .
- Case study
# Print every line
awk '{print}' awk.txt or awk '{print $0}' awk.txt
# Print some fields
awk '{print $1,$3}' awk.txt
# Print the number of fields per line ( Built in variables NF)
awk '{print NF}' awk.txt
# Print the first and last fields
awk '{print $1,$NF}' awk.txt
# Calculate and print
awk '{print $1,$2 * $3}' awk.txt
# Print line number (NR)
awk '{print NR,$0}' awk.txt
# Splice strings and fields
awk '{print $1," Today's income is ",$2 * $3}' awk.txt
# Format output
awk '{ printf("%s Today's income is $%.2f\n",$1,$2*$3) }' awk.txt
Fixed width output
awk '{ printf("%-8s Today's income is $%6.2f\n",$1,$2*$3) }' awk.txt
Output sort
awk '{ printf("%6.2f,%-8s Today's income is $%6.2f\n",$2*$3,$1,$2*$3) }' awk.txt |sort -nk3 -t,
AWK Pattern matching
- Single mode
# The hourly wage is greater than 5 The record of
awk '$2>5 {print $0}' awk.txt
# Pay more than 50 The employees'
awk '$2*$3>50 {print $1,$2*$3}' awk.txt
# The query name is Mark The record of
awk '$1=="Mark" {print $0}' awk.txt
# Regular expression matching names with Mar The record of
awk '/Mar/ {print $0}' awk.txt
- Pattern combination
# Print those $2 At least for 4, perhaps $3 At least for 20 The line of
awk '$2>=4||$3>=20 {print $0}' awk.txt
awk '!($2<4&&$3<20) {print $0}' awk.txt
# Print those $2 At least for 4, also $3 At least for 20 The line of
awk '$2>=4 && $3>=20 {print $0}' awk.txt
- BEGIN And END
Special mode BEGIN Matched before the first line of the first input file , END Match after the last line of the last input file is processed .
awk 'BEGIN {*********} END{***********}'
awk 'BEGIN {print "NAME RATE HOURS"} {print} END{print "END"}' awk.txt
awk 'BEGIN {print "NAME RATE HOURS";print "------"} {print} END{print "------";print "END"}' awk.txt
use AWK Calculation
- Calculate the sum
# The working hours exceed 15 Total number of employees per hour
awk '$3>15 {emp=emp+1} END{print emp," The working hours exceed 15 Number of employees per hour "}' awk.txt
- Calculate the average
# Calculate the average salary of employees
awk '{pay=pay+$2*$3} END{print NR," The total number of employees ";print " Total wage ",pay; print " Average wage ",pay/NR}' awk.txt
- Find the maximum
# Find the employee with the highest hourly wage
awk '$2 >maxrate {maxrate=$2;maxemp=$1} END{print " The employee with the highest hourly wage is :",maxemp," Wages are :",maxrate}' awk.txt
- Print the last line
awk '{last=$0} END{print last}' awk.txt
String splicing
#name Add a space between
awk '{names=names $1 " "} END{print names}' awk.txt
Built-in functions
#length Find the string length
Calculate the length of the name
awk '{print $1,length($1)}' awk.txt
# Count the lines of text , Total number of fields , Total bytes
awk '{nc=nc+length($0)+1;nw=nw+NF} END{print NR,"lines,",nw,"words,",nc,"characters"}' awk.txt
Flow control statement
- if-else sentence
# Find hourly wages more than $6.00 Total and average remuneration of employees
awk '$2>6 {n=n+1;pay=pay+$2*$3} END{if(n>0) print n,"employees,total pay is",pay,"average pay is",pay/n;else print "not exit"}' awk.txt
- while sentence
Calculation 1 To 100 The sum of the
awk 'BEGIN{ test=100; total=0; while(i<=test) { total+=i; i++; } print total; }' 5050
#shell Script
#!/bin/bash
total=0
i=0
while [ $i -le 100 ]
do
let total+=$i
let i++
done
echo $total
- for sentence
# Calculation 1 To 100 The sum of
awk 'BEGIN{ total=0; for(i=0;i<=100;i++) { total+=i; } print total; }'
- Array
# Print each line upside down
awk '{line[NR] = $0} END {i=NR; while (i>0){ print line[i];i=i-1}}' awk.txt
awk '{line[NR] = $0} END{for(i=NR;i>0;i--){print line[i]}}' awk.txt
AWK Production cases
# The total number of rows entered
awk 'END{print NR}' awk.txt
# Print page 2 That's ok
awk 'NR==2 {print $0}' awk.txt
# Print the last field of each line
awk '{print $NF}' awk.txt
# Print the last field of the last line
awk '{field=$NF} END{print field}' awk.txt
# The number of print fields is more than 2 Input lines of
awk 'NF>2 {print $0}' awk.txt
# Print the last field with a value greater than 4 Input line for
awk '$NF>4{print $0}' awk.txt
边栏推荐
- Memory analyzer (MAT)
- 使用dnSpy對無源碼EXE或DLL進行反編譯並且修改
- Yyds dry inventory hcie security Day12: concept of supplementary package filtering and security policy
- 大神们,我想发两个广播流1 从mysql加载基础数据,广播出去2 从kafka加载基础数据的变更
- 90 后,辞职创业,说要卷死云数据库
- Mysql - - Index
- Teach you how to install aidlux (1 installation)
- English topic assignment (28)
- Après 90 ans, j'ai démissionné pour démarrer une entreprise et j'ai dit que j'allais détruire la base de données Cloud.
- Remember the experience of automatically jumping to spinach station when the home page was tampered with
猜你喜欢
Station B, dark horse programmer, employee management system, access conflict related (there is an unhandled exception at 0x00007ff633a4c54d (in employee management system.Exe): 0xc0000005: read locat
Bluebridge cup Guoxin Changtian single chip microcomputer -- hardware environment (I)
Exclusive interview with the person in charge of openkruise: to what extent has cloud native application automation developed now?
Farmersworld farmers world, no faith, how to talk about success?
Implementation principle of inheritance, encapsulation and polymorphism
Collection | pytoch common loss function disassembly
17 websites for practicing automated testing. I'm sure you'll like them
Yyds dry inventory hcie security Day12: concept of supplementary package filtering and security policy
Compréhension de la technologie gslb (Global Server load balance)
Yiwen teaches you how to choose your own NFT trading market
随机推荐
Goodbye 2021, how do programmers go to the top of the disdain chain?
Luogu deep foundation part 1 Introduction to language Chapter 6 string and file operation
treevalue——Master Nested Data Like Tensor
Global and Chinese market of recycled yarn 2022-2028: Research Report on technology, participants, trends, market size and share
JS demo calculate how many days are left in this year
No matter how hot the metauniverse is, it cannot be separated from data
Covariance
技术管理进阶——如何在面试中考察候选人并增大入职概率
The post-90s resigned and started a business, saying they would kill cloud database
Exclusive interview with the person in charge of openkruise: to what extent has cloud native application automation developed now?
4. Data splitting of Flink real-time project
Mysql - - Index
Selenium has three waiting methods (forced waiting, implicit waiting, and display waiting)
Cognitive fallacy: what is Fredkin's paradox
2022 G3 boiler water treatment registration examination and G3 boiler water treatment examination papers
Blue Bridge Cup Guoxin Changtian single chip microcomputer -- software environment (II)
How PHP drives mongodb
Tkinter Huarong Road 4x4 tutorial III
Global and Chinese market of telematics boxes 2022-2028: Research Report on technology, participants, trends, market size and share
Control loop of program (while loop)