当前位置:网站首页>awk从入门到入土(11)awk getline函数详解
awk从入门到入土(11)awk getline函数详解
2022-07-04 08:38:00 【奇妙之二进制】
**getline命令是我个人认为awk最强大的一个命令。因为它彻底改变了awk的运行逻辑。**awk本质上就是一个for循环,它每次对输入文件的一行进行处理,然后转而执行下一行,直到整个文件的每一行都被执行完毕。整个过程是自动的,你无需做什么。但是,getline命令却可以让你去控制循环。当然,getline命令执行后,awk会设置NF,NR,FNR和$0等这些内部变量。
我们先看一个简单的例子,打印出从1到10之间的偶数:
$ seq 10 | awk '{getline; print $0}'
2
4
6
8
10
那么getline究竟是实现什么功能呢?正如getline的翻译,得到行,但是注意,得到的并不是当前行,而是当前行的下一行。以上面的例子来分析,awk首先读取到了第一行,就是1,然后getline,就得到了1下面的第二行,就是2,因为getline之后,awk会改变对应的NF,NR,FNR和$0等内部变量,所以此时的$0的值就不再是1,而是2了,然后将它打印出来。以此类推,就可以得到上面的结果。同样,我们可以利用getline只打印出奇数行。
$ seq 10 | awk '{print $0; getline}'
1
3
5
7
9
与打印偶数行的唯一区别就是print $0和getline的顺序不一样。因为getline在print $0之后,此时的$0仍然是第一行。然后getline,$0变成了下一行2。依次类推,就打印出了奇数行。
下一个我们换一个难一些的,就是奇偶行对调打印,原来在奇数行的内容将其打印在偶数行,原来在偶数行的内容将其打印在奇数行。
$ seq 10 | awk '{getline tmp; print tmp; print $0}'
2
1
4
3
6
5
8
7
10
9
上面例子将getline得到的下一行的内容放在了tmp这个变量里,因此NF,NR,FNR和$0等内部变量并不会被改变。
另外getline也可以从另外一个文件中读取内容。下面例子实现将两个文件的每一行都打印在一行上。
[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文件的内容为上面打印出来的第一列,b.txt文件的内容为上面打印出来的第二列。
此外,getline支持从管道读取。下面例子通过getline得到系统的当前时间。
[email protected]:~/bash/awk$ awk 'BEGIN {"date" | getline; close("date"); print $0}'
Tue May 10 07:50:51 PDT 2016
getline的使用方法汇总如下:
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.
返回值:
-1 失败
0 读到文件末尾
1 成功
边栏推荐
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- The upper layer route cannot Ping the lower layer route
- Collections in Scala
- FRP intranet penetration, reverse proxy
- C#实现一个万物皆可排序的队列
- Laravel page load problem connection reset - PHP
- Démarrage des microservices: passerelle
- Scanf read in data type symbol table
- Xcode 6 swift code completion does not work properly - Xcode 6 swift code completion not working
- 运动【跑步 01】一个程序员的半马挑战:跑前准备+跑中调整+跑后恢复(经验分享)
猜你喜欢

What if I forget the router password

C, Numerical Recipes in C, solution of linear algebraic equations, Gauss Jordan elimination method, source code

Do you know about autorl in intensive learning? A summary of articles written by more than ten scholars including Oxford University and Google

Guanghetong's high-performance 4g/5g wireless module solution comprehensively promotes an efficient and low-carbon smart grid

FOC控制

小程序容器技术与物联网 IoT 可以碰撞出什么样的火花

es6总结

How to solve the problem of computer jam and slow down

运动【跑步 01】一个程序员的半马挑战:跑前准备+跑中调整+跑后恢复(经验分享)

Bishi blog (13) -- oral arithmetic test app
随机推荐
DM8 database recovery based on point in time
埃氏筛+欧拉筛+区间筛
System disk expansion in virtual machine
Newh3c - network address translation (NAT)
Call Baidu map to display the current position
Sort by item from the list within the list - C #
Unity-Text上标平方表示形式+text判断文本是否为空
Mouse over to change the transparency of web page image
Wechat has new functions, and the test is started again
Collections in Scala
ctfshow web255 web 256 web257
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
猜数字游戏
ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
Difference between static method and non static method (advantages / disadvantages)
What should I do if there is a problem with the graphics card screen on the computer
C#实现一个万物皆可排序的队列
Basic operations of databases and tables ----- view data tables
C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,Gauss-Jordan消去法,源代码
snipaste 方便的截图软件,可以复制在屏幕上