首页 > 代码库 > 将多行文件转换为一行
将多行文件转换为一行
一、需求:
把这些数据库文件转换成一行,然后用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
将多行文件转换为一行
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。