awk如何将多行文件转换为一行

发布时间:2022-01-15 14:58:26 作者:小新
来源:亿速云 阅读:178

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

一、需求:

把这些数据库文件转换成一行,然后用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如何将多行文件转换为一行”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. Oracle多行数据怎么转一行
  2. 如何使用sed或awk处理文件最后一行

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

awk

上一篇:MetaMask以太坊钱包插件怎么安装使用

下一篇:springboot整合quartz定时任务框架的方法是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》