我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

awk如何将多行文件转换为一行-创新互联

这篇文章主要介绍awk如何将多行文件转换为一行,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

黎川ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

一、需求:

把这些数据库文件转换成一行,然后用scp -r 一次性作拷贝。

二、实例

[root@mysql5 ~]# cat /tmp/1
ga_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

三、解决有三种

1、采用awk

[root@mysql5 ~]# awk BEGIN{RS=EOF}'{gsub(/\n/," ");print}' /tmp/1
ga_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

awk默认将记录分隔符(record separator即RS)设置为\n,此行代码将RS设置为EOF(文件结束),也就是把文件视为一个记录,然后通过gsub函数将\n替换成空格,最后输出。

2、采用sed

[root@mysql5 ~]# sed ':a ; N;s/\n/ / ; t a ; ' /tmp/1
ga_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

说明:sed默认只按行处理,N可以让其读入下一行,再对\n进行替换,这样就可以将两行并做一行。但是怎么将所有行并作一行呢?可以采用sed的跳转功能。:a 在代码开始处设置一个标记a,在代码执行到结尾处时利用跳转命令t a重新跳转到标号a处,重新执行代码,这样就可以递归的将所有行合并成一行。

3、cat file | xargs

[root@mysql5 ~]# cat /tmp/1 | xargs
ga_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

以上是“awk如何将多行文件转换为一行”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻名称:awk如何将多行文件转换为一行-创新互联
链接分享:http://mswzjz.cn/article/ccppjh.html

其他资讯