当前位置:网站首页>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
边栏推荐
- Relationship and operation of random events
- Fault analysis | MySQL: unique key constraint failure
- Const string inside function - C #
- How to solve the problem of computer jam and slow down
- If the array values match each other, shuffle again - PHP
- awk从入门到入土(12)awk也可以写脚本,替代shell
- How to re enable local connection when the network of laptop is disabled
- 1211 or chicken and rabbit in the same cage
- Redis sentinel mechanism
- FRP intranet penetration, reverse proxy
猜你喜欢
Bishi blog (13) -- oral arithmetic test app
What exactly is DAAS data as a service? Don't be misled by other DAAS concepts
Fault analysis | MySQL: unique key constraint failure
Redis sentinel mechanism
Comprendre la méthode de détection des valeurs aberrantes des données
随机事件的关系与运算
Mouse over to change the transparency of web page image
Codeforces Round #803 (Div. 2)(A-D)
Manjaro install wechat
Call Baidu map to display the current position
随机推荐
Three paradigms of database design
没有Kubernetes怎么玩Dapr?
ctfshow web255 web 256 web257
ES6 summary
yolov5 xml数据集转换为VOC数据集
随机事件的关系与运算
PHP converts seconds to timestamps - PHP
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
Newh3c - routing protocol (RIP, OSPF)
Use Alibaba cloud NPM image acceleration
Manjaro install wechat
Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
AI Winter Olympics | is the future coming? Enter the entrance of the meta universe - virtual digital human
地平线 旭日X3 PI (一)首次开机细节
2022 gas examination registration and free gas examination questions
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
From scratch, use Jenkins to build and publish pipeline pipeline project
埃氏筛+欧拉筛+区间筛
awk从入门到入土(12)awk也可以写脚本,替代shell