当前位置:网站首页>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
边栏推荐
- [untitled] 2022 polymerization process analysis and polymerization process simulation examination
- What if I forget the router password
- How to play dapr without kubernetes?
- Awk from entry to earth (12) awk can also write scripts to replace the shell
- Ehrlich sieve + Euler sieve + interval sieve
- awk从入土到入门(10)awk内置函数
- ES6 summary
- System disk expansion in virtual machine
- How does Xiaobai buy a suitable notebook
- Comprendre la méthode de détection des valeurs aberrantes des données
猜你喜欢
广和通高性能4G/5G无线模组解决方案全面推动高效、低碳智能电网
Openfeign service interface call
DM database password policy and login restriction settings
Guanghetong's high-performance 4g/5g wireless module solution comprehensively promotes an efficient and low-carbon smart grid
埃氏筛+欧拉筛+区间筛
Codeforces Round #793 (Div. 2)(A-D)
How to solve the problem that computers often flash
AI Winter Olympics | is the future coming? Enter the entrance of the meta universe - virtual digital human
微服务入门:Gateway网关
ES6 summary
随机推荐
What if I forget the router password
Convert datetime string to datetime - C in the original time zone
awk从入门到入土(11)awk getline函数详解
2022 electrician (intermediate) examination question bank and electrician (intermediate) examination questions and analysis
Codeforces Global Round 21(A-E)
微服務入門:Gateway網關
Need help resetting PHP counters - PHP
SQL statement view SQL Server 2005 version number
Codeforces Round #793 (Div. 2)(A-D)
Codeforces Round #803 (Div. 2)(A-D)
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
1211 or chicken and rabbit in the same cage
Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
Three paradigms of database design
Redis 哨兵机制
Bishi blog (13) -- oral arithmetic test app
snipaste 方便的截图软件,可以复制在屏幕上
PHP session variable passed from form - PHP
How to choose solid state hard disk and mechanical hard disk in computer
ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据