当前位置:网站首页>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
边栏推荐
- swatch
- Awk from entry to penetration (6) regular matching
- 小程序容器技术与物联网 IoT 可以碰撞出什么样的火花
- Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
- OpenFeign 服务接口调用
- Manjaro install wechat
- Const string inside function - C #
- @Role of requestparam annotation
- C, Numerical Recipes in C, solution of linear algebraic equations, Gauss Jordan elimination method, source code
- [untitled] 2022 polymerization process analysis and polymerization process simulation examination
猜你喜欢

Redis 哨兵机制

Fault analysis | MySQL: unique key constraint failure

Awk from entry to earth (12) awk can also write scripts to replace the shell

Industry depression has the advantages of industry depression

Turn: excellent managers focus not on mistakes, but on advantages

System disk expansion in virtual machine

Conversion of yolov5 XML dataset to VOC dataset

How does Xiaobai buy a suitable notebook

Redis sentinel mechanism

Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
随机推荐
FOC control
Horizon sunrise X3 PI (I) first boot details
awk从入门到入土(18)gawk线上手册
2022 gas examination registration and free gas examination questions
The second session of the question swiping and punching activity -- solving the switching problem with recursion as the background (I)
Convert datetime string to datetime - C in the original time zone
Const string inside function - C #
Take you to master the formatter of visual studio code
Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
Redis 哨兵机制
deno debugger
Educational Codeforces Round 119 (Rated for Div. 2)
Show server status on Web page (on or off) - PHP
Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
Laravel page load problem connection reset - PHP
Live in a dream, only do things you don't say
Webapi interview question summary 01
ES6 summary
ctfshow web255 web 256 web257
ctfshow web255 web 256 web257