当前位置:网站首页>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)
用于关闭刚刚打开的 双向通讯管道
边栏推荐
- Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
- [untitled] 2022 polymerization process analysis and polymerization process simulation examination
- Figure guessing game
- Unity-Text上标平方表示形式+text判断文本是否为空
- Convert datetime string to datetime - C in the original time zone
- FRP intranet penetration, reverse proxy
- Educational Codeforces Round 115 (Rated for Div. 2)
- Unity-写入Word
- How to solve the problem that computers often flash
- Parallel shift does not provide any acceleration - C #
猜你喜欢
DM8 database recovery based on point in time
[CV] Wu Enda machine learning course notes | Chapter 9
yolov5 xml数据集转换为VOC数据集
Unity-Text上标平方表示形式+text判断文本是否为空
【Go基础】1 - Go Go Go
Codeforces Round #793 (Div. 2)(A-D)
[performance test] read JMeter
Moher College phpmailer remote command execution vulnerability tracing
What should I do if there is a problem with the graphics card screen on the computer
What does range mean in PHP
随机推荐
Common components of flask
【性能测试】一文读懂Jmeter
1、卡尔曼滤波-最佳的线性滤波器
转:优秀的管理者,关注的不是错误,而是优势
Bishi blog (13) -- oral arithmetic test app
go-zero微服务实战系列(九、极致优化秒杀性能)
Comparison between sentinel and hystrix
[BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
Take you to master the formatter of visual studio code
Codeforces Round #793 (Div. 2)(A-D)
How can we make a monthly income of more than 10000? We media people with low income come and have a look
Leetcode 23. Merge K ascending linked lists
PHP converts seconds to timestamps - PHP
FOC控制
@Role of pathvariable annotation
Unity text superscript square representation +text judge whether the text is empty
[CV] Wu Enda machine learning course notes | Chapter 9
[Chongqing Guangdong education] National Open University spring 2019 455 logistics practice reference questions
[attack and defense world | WP] cat
没有Kubernetes怎么玩Dapr?