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 convert a multiline file to one line by awk

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how awk converts multi-line files into one line, which is very detailed and has a certain reference value. Friends who are interested must read it!

1. Demand:

Convert these database files to one line, and then use scp-r to copy them all at once.

II. Examples

[root@mysql5 ~] # cat / tmp/1ga_10122ga_10589ga_11053ga_11238ga_11559ga_11944ga_12193ga_13542ga_35179ga_36013ga_36063ga_36575ga_36737ga_36926ga_37962ga_39596ga_39599ga_39603ga_39604ga_39605ga_39608ga_39609ga_39610ga_39614ga_39618ga_39619ga_39622ga_39625ga_39626ga_39630ga_39633ga_39636ga_39639ga_39640ga_39642ga_39645ga_39648ga_39652ga_39654ga_39657ga_39668ga_39675ga_39687ga_39690ga_39691ga_39693ga_39695ga_39697ga_39702ga_39703ga_39704ga_39708ga_39715ga_39717ga _ 39724ga_39725ga_39727ga_39728ga_39730ga_39732ga_39738ga_39741ga_39743ga_39753ga_39757ga_39761ga_39765

There are three kinds of solutions

1. Adopt awk

[root@mysql5 ~] # awk BEGIN {RS=EOF}'{gsub (/\ nmax, ") Print}'/ tmp/1ga_10122 ga_10589 ga_11053 ga_11238 ga_11559 ga_11944 ga_12193 ga_13542 ga_35179 ga_36013 ga_36063 ga_36575 ga_36737 ga_36926 ga_37962 ga_39596 ga_39599 ga_39603 ga_39604 ga_39605 ga_39608 ga_39609 ga_39610 ga_39614 ga_39618 ga_39619 ga_39622 ga_39625 ga_39626 ga_39630 ga_39633 ga_39636 ga_39639 ga_39640 ga_39642 ga_39645 ga_39648 ga_ 39652 ga_39654 ga_39657 ga_39668 ga_39675 ga_39687 ga_39690 ga_39691 ga_39693 ga_39695 ga_39697 ga_39702 ga_39703 ga_39704 ga_39708 ga_39715 ga_39717 ga_39724 ga_39725 ga_39727 ga_39728 ga_39730 ga_39732 ga_39738 ga_39741 ga_39743 ga_39753 ga_39757 ga_39761 ga_39765

By default, awk sets the record separator (record separator, that is, RS) to\ n.This line of code sets RS to EOF (end of file), that is, treats the file as a record, then replaces\ nwith spaces through the gsub function, and finally outputs.

2. Adopt sed

[root@mysql5] # sed': a; Nutter /\ n / /; t a '/ tmp/1ga_10122 ga_10589 ga_11053 ga_11238 ga_11559 ga_11944 ga_12193 ga_13542 ga_35179 ga_36013 ga_36063 ga_36575 ga_36737 ga_36926 ga_37962 ga_39596 ga_39599 ga_39603 ga_39604 ga_39605 ga_39608 ga_39609 ga_39610 ga_39614 ga_39618 ga_39619 ga_39622 ga_39625 ga_39626 ga_39630 ga_39633 ga_39636 ga_39639 ga_39640 ga_39642 ga_39645 ga_39648 ga_39652 Ga_39654 ga_39657 ga_39668 ga_39675 ga_39687 ga_39690 ga_39691 ga_39693 ga_39695 ga_39697 ga_39702 ga_39703 ga_39704 ga_39708 ga_39715 ga_39717 ga_39724 ga_39725 ga_39727 ga_39728 ga_39730 ga_39732 ga_39738 ga_39741 ga_39743 ga_39753 ga_39757 ga_39761 ga_39765

Note: sed is processed by default only by line, N can read it into the next line, and then replace\ n, so that you can combine two lines with one line. But how do you put all the lines together? The jump function of sed can be used. : a set a tag an at the beginning of the code, use the jump command t a to jump back to the label an at the end of the code execution, and re-execute the code, so that all lines can be merged into one line recursively.

3. Cat file | xargs

[root@mysql5 ~] # cat / tmp/1 | xargsga_10122 ga_10589 ga_11053 ga_11238 ga_11559 ga_11944 ga_12193 ga_13542 ga_35179 ga_36013 ga_36063 ga_36575 ga_36737 ga_36926 ga_37962 ga_39596 ga_39599 ga_39603 ga_39604 ga_39605 ga_39608 ga_39609 ga_39610 ga_39614 ga_39618 ga_39619 ga_39622 ga_39625 ga_39626 ga_39630 ga_39633 ga_39636 ga_39639 ga_39640 ga_39642 ga_ 39645 ga_39648 ga_39652 ga_39654 ga_39657 ga_39668 ga_39675 ga_39687 ga_39690 ga_39691 ga_39693 ga_39695 ga_39697 ga_39702 ga_39703 ga_39704 ga_39708 ga_39715 ga_39717 ga_39724 ga_39725 ga_39727 ga_39728 ga_39730 ga_39732 ga_39738 ga_39741 ga_39743 ga_39753 ga_39757 ga_39761 ga_39765 and above are all the contents of the article "how awk converts multiple-line files to one line" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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

Development

Wechat

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

12
Report