当前位置:网站首页>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

原网站

版权声明
本文为[Wonderful binary]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207040838046971.html