当前位置:网站首页>awk从入门到入土(14)awk输出重定向
awk从入门到入土(14)awk输出重定向
2022-07-04 08:38:00 【奇妙之二进制】
到目前为止,我们的 AWK 程序都是把结果输出到 标准输出 上。
虽然 AWK 程序没有打开文件/读写文件等相应的功能,但 AWK 程序同样支持 重定向 操作,也就是支持把数据输出到 文件 上。
AWK 的重定向操作一般出现在 print 或 printf 语句后面。
AWK中的重定向与 shell 命令中的重定向类似,只是它们写在 AWK 程序中。
重定向操作符 >
重定向操作符 > 就是把 AWK 程序的输出结果重定向到某个文件。
不过要注意的是:重定向操作符 > 会先删除文件的原先内容然后再写入数据,也就是说原先的内容不存在了。
重定向操作符 > 操作符的语法格式如下
print DATA > output-file
重定向操作符 > 会把 print 的输出结果写入到文件 output-file 中
如果 output-file 不存在,重定向操作符 > 会先创建文件。
重定向操作符 > 会首先删除文件中的内容然后再输出,但是,对于同一个 AWK 程序中的后续的 重定向操作符 > 则并不会再删除原先的内容,而是在末尾追加。
也就是说,重定向操作符 > 在打开文件的时候就会先删除内容。后面的 重定向操作符 > 因为文件已经打开了,就不会再执行打开操作,也就不会删除原先的内容
范例 1
首先我们使用 重定向操作符 > 创建一个文件并写入数据
[www.twle.cn]$ echo "旧数据:简单教程欢迎您" > /tmp/message.txt
[www.twle.cn]$ cat /tmp/message.txt
运行上面的 awk 命令,输出结果如下
旧数据:简单教程欢迎您
接着,我们再使用 重定向操作符 > 往文件里写入一些数据,看看结果
范例 2
[www.twle.cn]$ awk 'BEGIN { print "新数据:简单教程欢迎您" > "/tmp/message.txt" }'
[www.twle.cn]$ cat /tmp/message.txt
运行上面的 awk 命令,输出结果如下
新数据:简单教程欢迎您
数据追加操作符 >>
数据追加操作就是把输出输出到文件的末尾,而不是覆盖原先的内容。
数据追加操作符 >> 的语法格式如下
print DATA >> output-file
数据追加操作符 >> 会把 print 的输出结果 追加 到 output-file 文件的末尾,原先的数据还在。
如果 output-file 文件不存在,那么会先创建文件然后再输入数据。
范例
我们首先使用 重定向操作符 > 创建一个文件并写入一些数据
[www.twle.cn]$ echo "旧数据:简单教程欢迎您" > /tmp/hello.txt
[www.twle.cn]$ cat /tmp/hello.txt
运行上面的 awk 命令,输出结果如下
旧数据:简单教程欢迎您
接下来我们使用 数据追加操作符 >> 往刚刚的 hello.txt 文件中追加一些数据
范例 2
[www.twle.cn]$ awk 'BEGIN { print "新数据:简单教程欢迎您" >> "/tmp/hello.txt" }'
[www.twle.cn]$ cat /tmp/hello.txt
运行上面的 awk 命令,输出结果如下
旧数据:简单教程欢迎您
新数据:简单教程欢迎您
管道 |
AWK 还支持管道及管道操作符 |。
也就是说我们可以使用管道代替文件将输出发送到另一个程序。
管道操作符 | 会打开一个管道,AWK 通过此管道将输出的结果写入另一个进程以执行该命令。
管道重定向参数命令实际上是 AWK 表达式。
AWK 中管道 | 的语法格式如下
print items | command
范例
下面,我们使用管道 | 把输出重定向到外部的 tr 命令
[www.twle.cn]$ awk 'BEGIN { print "hello, world !!!" | "tr [a-z] [A-Z]" }'
运行上面的 awk 命令,输出结果如下
HELLO, WORLD !!!
双向交流管道 |&
AWK可以使用 |& 与外部进程通信。
|& 会打开一个双向交流管道,AWK 程序可以往打开的管道进行读写,外部程序也可以往打开的管道进行读写。
AWK 中|& 双向管道的语法格式如下
print items |& command
范例
BEGIN {
cmd = "tr [a-z] [A-Z]"
print "hello, world !!!" |& cmd
close(cmd, "to")
cmd |& getline out
print out;
close(cmd);
}
运行上面的 awk 命令,输出结果如下
HELLO, WORLD !!!
上面这段程序看起来是不是很迷茫 ??
这估计是我们见过的最为复杂的程序了,下面我们就一步一步来拆解吧!!!
第一行
cmd = "tr [a-z] [A-Z]定义了一个系统命令,我们会使用这个命令开启一个 双向通讯管道第二行
print "hello, world !!!" |& cmd。我们先来看
|& cmd,这句话的意思是使用|&输出重定向运算符打开一个 双向通讯管道 连接当前 AWK 程序和刚刚定义的命令。然后
print "hello, world !!!" |& cmd就是把print函数的输出结果重定向到刚刚定义的 双向通讯管道。说的直白一点,就是
print函数为tr命令提供了输入。第三行
close(cmd, "to")用于关闭刚刚打开的 双向通讯管道 的输入进程。第四行
cmd |& getline out把tr命令的执行结果使用管道运算符&|重定向到getline函数。getline 函数会把结果储存到out变量中。最后一行
close(cmd)用于关闭刚刚打开的 双向通讯管道
边栏推荐
- Cancel ctrl+alt+delete when starting up
- [test de performance] lire jmeter
- Moher College webmin unauthenticated remote code execution
- Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
- go-zero微服务实战系列(九、极致优化秒杀性能)
- How to improve your system architecture?
- C#实现一个万物皆可排序的队列
- Leetcode 23. Merge K ascending linked lists
- Educational Codeforces Round 115 (Rated for Div. 2)
- 【Go基础】1 - Go Go Go
猜你喜欢

Fault analysis | MySQL: unique key constraint failure

How can we make a monthly income of more than 10000? We media people with low income come and have a look

AcWing 244. Enigmatic cow (tree array + binary search)

Comparison between sentinel and hystrix

Mouse over to change the transparency of web page image

转:优秀的管理者,关注的不是错误,而是优势

How college students choose suitable computers

DM database password policy and login restriction settings

SSRF vulnerability exploitation - attack redis

How to choose solid state hard disk and mechanical hard disk in computer
随机推荐
Li Kou today's question -1200 Minimum absolute difference
How to improve your system architecture?
@Role of requestparam annotation
Newh3c - routing protocol (RIP, OSPF)
Codeforces Global Round 21(A-E)
How to play dapr without kubernetes?
DM8 tablespace backup and recovery
Openfeign service interface call
Group programming ladder race - exercise set l2-002 linked list de duplication
Display Chinese characters according to numbers
deno debugger
R language ggplot2 visualization: ggplot2 visualization grouping box diagram, place the legend and title of the visualization image on the top left of the image and align them to the left, in which th
[untitled] 2022 polymerization process analysis and polymerization process simulation examination
根据数字显示中文汉字
From scratch, use Jenkins to build and publish pipeline pipeline project
Conversion of yolov5 XML dataset to VOC dataset
Leetcode 146. LRU cache
09 softmax regression + loss function
Sqli labs download, installation and reset of SQL injection test tool one of the solutions to the database error (# 0{main}throw in d:\software\phpstudy_pro\www\sqli labs-...)
How to set multiple selecteditems on a list box- c#