Get the App
SLTechnology News&Howtos  ›  Servers  › 

Example Analysis of awk Multi-line merging

Shulou Source: shulou.com Published: 2022-06-01 04:07:11 10月03日 Update

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.

Tags: Content analysis multiple lines one line statement example code time format line number output instance file aspect more knowledge program article industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi NVidia vpn Apple Huawei