当前位置:网站首页>Awk from getting started to digging in (11) detailed explanation of awk getline function
Awk from getting started to digging in (11) detailed explanation of awk getline function
2022-07-04 08:46:00 【Wonderful binary】
**getline The order is what I personally think awk The most powerful command . Because it completely changed awk The operation logic of .**awk It's essentially one for loop , It processes one line of the input file at a time , Then turn to the next line , Until every line of the entire file is executed . The whole process is automatic , You don't have to do anything . however ,getline Commands can let you control the cycle . Of course ,getline After the execution of the command ,awk Will be set NF,NR,FNR and $0 Wait for these internal variables .
Let's start with a simple example , Print out from 1 To 10 Even number between :
$ seq 10 | awk '{getline; print $0}'
2
4
6
8
10
that getline What function does it realize ? just as getline Translation , Get the line , But notice , What you get is not the current line , It is the next line of the current line . Take the above example to analyze ,awk First read the first line , Namely 1, then getline, Got it. 1 The second line below , Namely 2, because getline after ,awk Will change the corresponding NF,NR,FNR and $0 And so on , So at this time $0 The value of is no longer 1, It is 2 了 , Then print it out . And so on , You can get the above results . Again , We can use getline Only odd lines are printed .
$ seq 10 | awk '{print $0; getline}'
1
3
5
7
9
The only difference from printing even lines is print $0 and getline The order is different . because getline stay print $0 after , At this time $0 Still the first line . then getline,$0 It becomes the next line 2. By analogy , It prints out odd lines .
Next, let's change to a harder one , That is, parity line swap printing , The original contents in odd lines will be printed in even lines , The original contents in even lines will be printed in odd lines .
$ seq 10 | awk '{getline tmp; print tmp; print $0}'
2
1
4
3
6
5
8
7
10
9
The above example will getline The content of the next line obtained is placed in tmp In this variable , therefore NF,NR,FNR and $0 And other internal variables will not be changed .
in addition getline You can also read from another file . The following example implements printing each line of two files on one line .
[email protected]:~/bash/awk$ awk '{printf "%s ", $0; getline < "b.txt"; print $0}' a.txt
1 6
2 7
3 8
4 9
5 10
a.txt The contents of the document are the first column printed above ,b.txt The content of the file is the second column printed above .
Besides ,getline Support reading from pipeline . The following example passes getline Get the current time of the system .
[email protected]:~/bash/awk$ awk 'BEGIN {"date" | getline; close("date"); print $0}'
Tue May 10 07:50:51 PDT 2016
getline The usage of is summarized as follows :
getline
reads into $0, updates the fields, NF, NR and FNR.
getline < file
reads into $0 from file, updates the fields and NF.
getline var
reads the next record into var, updates NR and FNR.
getline var < file
reads the next record of file into var.
command | getline
pipes a record from command into $0 and updates the fields and NF.
command | getline var
pipes a record from command into var.
Return value :
-1 Failure
0 Read to end of file
1 success
边栏推荐
- 没有Kubernetes怎么玩Dapr?
- 根据数字显示中文汉字
- ArcGIS application (XXII) ArcMap loading lidar Las format data
- Redis sentinel mechanism
- [attack and defense world | WP] cat
- 2022 electrician (intermediate) examination question bank and electrician (intermediate) examination questions and analysis
- C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,Gauss-Jordan消去法,源代码
- DM8 command line installation and database creation
- Codeforces Round #803 (Div. 2)(A-D)
- What sparks can applet container technology collide with IOT
猜你喜欢
随机事件的关系与运算
4 small ways to make your Tiktok video clearer
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
User login function: simple but difficult
What if I forget the router password
What if the wireless network connection of the laptop is unavailable
A method for detecting outliers of data
Fault analysis | MySQL: unique key constraint failure
AI Winter Olympics | is the future coming? Enter the entrance of the meta universe - virtual digital human
随机推荐
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
deno debugger
09 softmax regression + loss function
awk从入门到入土(7)条件语句
How to re enable local connection when the network of laptop is disabled
Use preg_ Match extracts the string into the array between: & | people PHP
Leetcode topic [array] -136- numbers that appear only once
Developers really review CSDN question and answer function, and there are many improvements~
go-zero微服务实战系列(九、极致优化秒杀性能)
awk从入门到入土(4)用户自定义变量
学习Nuxt.js
Internal learning
std::is_ union,std::is_ class,std::integral_ constant
Industry depression has the advantages of industry depression
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
DM database password policy and login restriction settings
FOC控制
@Role of requestparam annotation
Flutter integrated amap_ flutter_ location