File D.txt The following content
GGG 1
portals:192.168.5.41:3260
Werew 2
portals:192.168.5.43:3260
How to change the file D.txt content to the following content
GGG 192.168.5.41:3260
Werew 192.168.5.43:3260
Problem Solving Ideas:
[[email protected] study]# awk '/port/{print a "" $2}{a=$1} ' D.txt
GGG 192.168.5.41:3260
Werew 192.168.5.43:3260
'/port/{print a ' "$2}{a=$1} '
When processing the first line, start with Port? Obviously, do not start with port, so the pattern does not match and the action does not execute. But executed the back of the a=$1
When dealing with the second line, start with port, print a and the bank, and the process is a loop.
Detailed steps:
Because the first line does not have a match to start with port, print a "" is not executed and executes a=$1. A=ggg
Because the second line has a match that starts with Port, the print a "" $ line executes, because of A=GGG, so the output GGG 192.168.5.41:3260
Because the third row does not have a match to start with port, print a "" a=$1 does not execute and executes the. A=werew
Because the line four has a match with Port, the print a "" A=werew executes, so the output werew 192.168.5.43:3260
The results are as follows:
GGG 192.168.5.41:3260
Werew 192.168.5.43:3260
Shell script, how awk handles two rows in a file that are associated with the upper and lower lines.