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

How to use awk text processing tool to merge multiple lines in Linux

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces "how Linux uses awk text processing tools to achieve multi-line merging". In daily operations, I believe many people have doubts about how Linux uses awk text processing tools to achieve multi-line merging. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how Linux uses awk text processing tools to achieve multi-line merging". Next, please follow the editor to study!

In the Linux system to use awk text processing tools, sometimes need to merge multiple lines, which requires the use of awknext statements, the following editor will introduce you to Linux in the use of awk to achieve multi-line merging methods, friends who need to know.

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

At this point, the study on "how Linux uses awk text processing tools to achieve multi-line merging" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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: 0

*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