当前位置:网站首页>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
边栏推荐
- 09 softmax regression + loss function
- How to re enable local connection when the network of laptop is disabled
- Three paradigms of database design
- Démarrage des microservices: passerelle
- 地平线 旭日X3 PI (一)首次开机细节
- [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-
- How can we make a monthly income of more than 10000? We media people with low income come and have a look
- C#实现一个万物皆可排序的队列
- 埃氏筛+欧拉筛+区间筛
- How to choose solid state hard disk and mechanical hard disk in computer
猜你喜欢
What if the wireless network connection of the laptop is unavailable
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
Openfeign service interface call
Industry depression has the advantages of industry depression
manjaro安装微信
System disk expansion in virtual machine
Snipaste convenient screenshot software, which can be copied on the screen
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
std::is_ union,std::is_ class,std::integral_ constant
随机事件的关系与运算
随机推荐
[CV] Wu Enda machine learning course notes | Chapter 9
Awk from getting started to digging in (9) circular statement
Awk from entry to penetration (6) regular matching
Educational Codeforces Round 119 (Rated for Div. 2)
[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-
Call Baidu map to display the current position
AcWing 244. Enigmatic cow (tree array + binary search)
deno debugger
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
Conversion of yolov5 XML dataset to VOC dataset
Leetcode topic [array] - 121 - the best time to buy and sell stocks
@Role of requestparam annotation
Technology sharing | MySQL parallel DDL
How to send pictures to the server in the form of file stream through the upload control of antd
广和通高性能4G/5G无线模组解决方案全面推动高效、低碳智能电网
2022 electrician (intermediate) examination question bank and electrician (intermediate) examination questions and analysis
awk从入门到入土(14)awk输出重定向
ctfshow web255 web 256 web257
How to re enable local connection when the network of laptop is disabled
A method for detecting outliers of data