当前位置:网站首页>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
边栏推荐
- Turn: excellent managers focus not on mistakes, but on advantages
- Group programming ladder race - exercise set l1-006 continuity factor
- Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
- How does Xiaobai buy a suitable notebook
- awk从入门到入土(9)循环语句
- 团体程序设计天梯赛-练习集 L2-002 链表去重
- From scratch, use Jenkins to build and publish pipeline pipeline project
- Learn nuxt js
- ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
- What if the wireless network connection of the laptop is unavailable
猜你喜欢

DM database password policy and login restriction settings

High order phase difference such as smear caused by myopic surgery

Manjaro install wechat

Educational Codeforces Round 115 (Rated for Div. 2)

L1 regularization and L2 regularization

随机事件的关系与运算

Educational Codeforces Round 119 (Rated for Div. 2)

Horizon sunrise X3 PI (I) first boot details
![[CV] Wu Enda machine learning course notes | Chapter 9](/img/de/41244904c8853b8bb694e05f430156.jpg)
[CV] Wu Enda machine learning course notes | Chapter 9

How to re enable local connection when the network of laptop is disabled
随机推荐
Basic operations of databases and tables ----- view data tables
微服务入门:Gateway网关
Awk from entry to soil (5) simple condition matching
ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
SQL statement view SQL Server 2005 version number
Educational Codeforces Round 119 (Rated for Div. 2)
awk从入门到入土(9)循环语句
DM8 tablespace backup and recovery
Démarrage des microservices: passerelle
From scratch, use Jenkins to build and publish pipeline pipeline project
MySQL relearn 1-centos install mysql5.7
ArcGIS application (XXII) ArcMap loading lidar Las format data
C, Numerical Recipes in C, solution of linear algebraic equations, Gauss Jordan elimination method, source code
Comparison between sentinel and hystrix
Sequence model
Newh3c - routing protocol (RIP, OSPF)
AcWing 244. Enigmatic cow (tree array + binary search)
How to play dapr without kubernetes?
Convert datetime string to datetime - C in the original time zone
Basic discipline formula and unit conversion