Shell script, how awk handles two rows in a file that are associated with the upper and lower lines.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.