当前位置:网站首页>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
边栏推荐
- Horizon sunrise X3 PI (I) first boot details
- Use preg_ Match extracts the string into the array between: & | people PHP
- awk从入门到入土(11)awk getline函数详解
- es6总结
- How to play dapr without kubernetes?
- Codeforces Round #793 (Div. 2)(A-D)
- Relationship and operation of random events
- 小程序容器技术与物联网 IoT 可以碰撞出什么样的火花
- HMS core helps baby bus show high-quality children's digital content to global developers
- C#实现一个万物皆可排序的队列
猜你喜欢
es6总结
Collections in Scala
Horizon sunrise X3 PI (I) first boot details
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
Relationship and operation of random events
std::is_ union,std::is_ class,std::integral_ constant
Question 49: how to quickly determine the impact of IO latency on MySQL performance
Newh3c - network address translation (NAT)
[CV] Wu Enda machine learning course notes | Chapter 9
Ehrlich sieve + Euler sieve + interval sieve
随机推荐
Convert datetime string to datetime - C in the original time zone
Educational Codeforces Round 119 (Rated for Div. 2)
Codeforces Round #793 (Div. 2)(A-D)
Webapi interview question summary 01
Codeforces Global Round 21(A-E)
09 softmax regression + loss function
4 small ways to make your Tiktok video clearer
deno debugger
How to re enable local connection when the network of laptop is disabled
Awk from entry to earth (7) conditional statements
Educational Codeforces Round 115 (Rated for Div. 2)
WordPress get_ Users() returns all users with comparison queries - PHP
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
Leetcode topic [array] - 121 - the best time to buy and sell stocks
ctfshow web255 web 256 web257
Codeforces Global Round 21(A-E)
How to play dapr without kubernetes?
awk从入门到入土(4)用户自定义变量
NewH3C——ACL
System disk expansion in virtual machine