当前位置:网站首页>Awk from entry to earth (14) awk output redirection
Awk from entry to earth (14) awk output redirection
2022-07-04 08:46:00 【Wonderful binary】
up to now , our AWK Programs output results to standard output On .
although AWK The program did not open the file / Read and write files and other corresponding functions , but AWK The program also supports Redirect operation , That is, it supports the output of data to file On .
AWK The redirection operation of usually occurs in print
or printf
Statement behind .
AWK Redirection and shell The redirection in the command is similar , It's just that they're written in AWK In the program .
Redirection operator >
Redirection operator >
Is to put AWK The output of the program is redirected to a file .
But here's the thing : Redirection operator > The original contents of the file will be deleted first, and then the data will be written , That is to say, the original content no longer exists .
Redirection operator > The syntax format of the operator is as follows
print DATA > output-file
Redirection operator > Will be able to print
The output result of is written to a file output-file
in
If output-file
non-existent , Redirection operator > The file will be created first .
Redirection operator > The contents of the file will be deleted first, and then output , however , For the same AWK Subsequent in the program Redirection operator > Then the original content will not be deleted , Instead, add .
in other words , Redirection operator > When you open a file, you will delete the content first . hinder Redirection operator > Because the file has been opened , No more open operations , The original content will not be deleted
Example 1
First we use Redirection operator > Create a file and write data
[www.twle.cn]$ echo " Old data : Welcome to the simple tutorial " > /tmp/message.txt
[www.twle.cn]$ cat /tmp/message.txt
Run the top awk command , The output is as follows
Old data : Welcome to the simple tutorial
next , We'll use it again Redirection operator > Write some data into the file , Look at the results
Example 2
[www.twle.cn]$ awk 'BEGIN { print " The new data : Welcome to the simple tutorial " > "/tmp/message.txt" }'
[www.twle.cn]$ cat /tmp/message.txt
Run the top awk command , The output is as follows
The new data : Welcome to the simple tutorial
Data append operator >>
Data append operation is to output the output to the end of the file , Instead of overwriting the original content .
Data append operator >>
The syntax of is as follows
print DATA >> output-file
Data append operator >>
Will be able to print
Output result of Additional To output-file
End of file , The original data is still .
If output-file
file does not exist , Then the file will be created first, and then the data will be entered .
Example
We use Redirection operator > Create a file and write some data
[www.twle.cn]$ echo " Old data : Welcome to the simple tutorial " > /tmp/hello.txt
[www.twle.cn]$ cat /tmp/hello.txt
Run the top awk command , The output is as follows
Old data : Welcome to the simple tutorial
So let's use Data append operator >> Go to the one just now hello.txt
Add some data to the file
Example 2
[www.twle.cn]$ awk 'BEGIN { print " The new data : Welcome to the simple tutorial " >> "/tmp/hello.txt" }'
[www.twle.cn]$ cat /tmp/hello.txt
Run the top awk command , The output is as follows
Old data : Welcome to the simple tutorial
The new data : Welcome to the simple tutorial
The Conduit |
AWK It also supports pipes and pipe operators |
.
That is, we can use pipes instead of files to send the output to another program .
Pipe operators |
Will open a pipe ,AWK Write the output result to another process through this pipeline to execute the command .
The pipeline redirection parameter command is actually AWK expression .
AWK Middle pipe |
The syntax of is as follows
print items | command
Example
below , We use pipes |
Redirect output to external tr
command
[www.twle.cn]$ awk 'BEGIN { print "hello, world !!!" | "tr [a-z] [A-Z]" }'
Run the top awk command , The output is as follows
HELLO, WORLD !!!
Two way communication pipeline |&
AWK have access to |&
Communicate with external processes .
|&
Will open a two-way communication pipeline ,AWK The program can read and write to the open pipeline , External programs can also read and write to the open pipeline .
AWK in |&
The syntax format of the two-way pipeline is as follows
print items |& command
Example
BEGIN {
cmd = "tr [a-z] [A-Z]"
print "hello, world !!!" |& cmd
close(cmd, "to")
cmd |& getline out
print out;
close(cmd);
}
Run the top awk command , The output is as follows
HELLO, WORLD !!!
Does the above program look confused ??
This is probably the most complicated program we have ever seen , Now let's disassemble it step by step !!!
first line
cmd = "tr [a-z] [A-Z]
Defines a system command , We will use this command to open a Two way communication pipelineThe second line
print "hello, world !!!" |& cmd
.Let's see first
|& cmd
, This sentence means to use|&
The output redirection operator opens a Two way communication pipeline Connect current AWK The program and the command just defined .then
print "hello, world !!!" |& cmd
Is to putprint
The output of the function is redirected to the just defined Two way communication pipeline .To be frank , Namely
print
Function istr
The command provides input .The third line
close(cmd, "to")
Used to close the just opened Two way communication pipeline Input process of .In the fourth row
cmd |& getline out
holdtr
The execution result of the command uses the pipeline operator&|
Redirect togetline
function .getline The function stores the result inout
variable .The last line
close(cmd)
Used to close the just opened Two way communication pipeline
边栏推荐
- Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
- The basic syntax of mermaid in typera
- Flutter integrated amap_ flutter_ location
- OpenFeign 服务接口调用
- Technology sharing | MySQL parallel DDL
- Leetcode topic [array] - 121 - the best time to buy and sell stocks
- How to solve the problem of computer jam and slow down
- PHP session variable passed from form - PHP
- Li Kou today's question -1200 Minimum absolute difference
- High order phase difference such as smear caused by myopic surgery
猜你喜欢
Redis 哨兵机制
How college students choose suitable computers
MySQL relearn 1-centos install mysql5.7
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
yolov5 xml数据集转换为VOC数据集
Codeforces Round #793 (Div. 2)(A-D)
Turn: excellent managers focus not on mistakes, but on advantages
Redis sentinel mechanism
Comparison between sentinel and hystrix
Démarrage des microservices: passerelle
随机推荐
[attack and defense world | WP] cat
Getting started with microservices: gateway gateway
上周热点回顾(6.27-7.3)
ctfshow web255 web 256 web257
A single element in an ordered array
L1 regularization and L2 regularization
awk从入门到入土(14)awk输出重定向
awk从入门到入土(18)gawk线上手册
没有Kubernetes怎么玩Dapr?
swatch
SSRF vulnerability exploitation - attack redis
转:优秀的管理者,关注的不是错误,而是优势
Codeforces Global Round 21(A-E)
学习Nuxt.js
The second session of the question swiping and punching activity -- solving the switching problem with recursion as the background (I)
Horizon sunrise X3 PI (I) first boot details
Awk from getting started to digging in (9) circular statement
Educational Codeforces Round 119 (Rated for Div. 2)
awk从入门到入土(12)awk也可以写脚本,替代shell
09 softmax regression + loss function