当前位置:网站首页>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)用于关闭刚刚打开的 双向通讯管道
边栏推荐
- [attack and defense world | WP] cat
- PHP session variable passed from form - PHP
- MySQL relearn 1-centos install mysql5.7
- How to get bytes containing null terminators from a string- c#
- Difference between static method and non static method (advantages / disadvantages)
- Li Kou today's question -1200 Minimum absolute difference
- Internal learning
- DM8 command line installation and database creation
- 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
- Example analysis of C # read / write lock
猜你喜欢

4 small ways to make your Tiktok video clearer

Moher College webmin unauthenticated remote code execution

Newh3c - network address translation (NAT)

Moher college phpMyAdmin background file contains analysis traceability

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

Codeforces Global Round 21(A-E)

Fault analysis | MySQL: unique key constraint failure

Comparison between sentinel and hystrix

Unity-Text上标平方表示形式+text判断文本是否为空

go-zero微服务实战系列(九、极致优化秒杀性能)
随机推荐
NPM run build error
High order phase difference such as smear caused by myopic surgery
[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-
Leetcode topic [array] -136- numbers that appear only once
How to play dapr without kubernetes?
Use preg_ Match extracts the string into the array between: & | people PHP
Collections in Scala
广和通高性能4G/5G无线模组解决方案全面推动高效、低碳智能电网
Figure guessing game
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Leetcode 23. Merge K ascending linked lists
Li Kou today's question -1200 Minimum absolute difference
埃氏筛+欧拉筛+区间筛
std::is_ union,std::is_ class,std::integral_ constant
4 small ways to make your Tiktok video clearer
Technology sharing | MySQL parallel DDL
2022 gas examination registration and free gas examination questions
转:优秀的管理者,关注的不是错误,而是优势
How to send pictures to the server in the form of file stream through the upload control of antd
Show server status on Web page (on or off) - PHP