Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of awk Multi-line merging

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about the example analysis of awk multi-line merger, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

The awknext statement uses: match line by line in a loop. If next is encountered, the current line will be skipped and the following statement will be ignored. And match the next line.

The code is as follows:

The content of text.txt is:

A

B

C

D

E

[chengmo@centos5 shell] $awk 'NR%2==1 {next} {print NR,$0;}' text.txt

2 b

4 d

When the record line number is divided by 2 or more than 1, the current line is skipped. The following print NR,$0 will not be executed either. At the beginning of the next line, the program starts to judge the NR%2 value. When the record line number is: 2, the following statement block will be executed: 'print NR,$0'

Examples of awk next usage:

The code is as follows:

Request:

File: text.txt format:

Web01 [192.168.2.100]

Httpd ok

Tomcat ok

Sendmail ok

Web02 [192.168.2.101]

Httpd ok

Postfix ok

Web03 [192.168.2.102]

Mysqld ok

Httpd ok

You need to change the output format to:

Web01 [192.168.2.100]: httpd ok

Web01 [192.168.2.100]: tomcat ok

Web01 [192.168.2.100]: sendmail ok

Web02 [192.168.2.101]: httpd ok

Web02 [192.168.2.101]: postfix ok

Web03 [192.168.2.102]: mysqld ok

Web03 [192.168.2.102]: httpd ok

Analysis:

The analysis found that the line containing "web" needs to be skipped, and then the content needs to be merged into one line with the following line.

[chengmo@centos5 shell] $awk'/ ^ web/ {Thirty test.txt;} {print T ":\ t" $0;} 'test.txt

Web01 [192.168.2.100]: httpd ok

Web01 [192.168.2.100]: tomcat ok

Web01 [192.168.2.100]: sendmail ok

Web02 [192.168.2.101]: httpd ok

Web02 [192.168.2.101]: postfix ok

Web03 [192.168.2.102]: mysqld ok

Web03 [192.168.2.102]: httpd ok

Next is very convenient in terms of multi-line merging and selective output. You might as well try it when you use it.

After reading the above, do you have any further understanding of the example analysis of awk multi-line merging? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 237

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report