将多行文件转换为一行

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将多行文件转换为一行相关的知识,希望对你有一定的参考价值。

一、需求:

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

二、实例

[[email protected] ~]# 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

[[email protected] ~]# 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

[[email protected] ~]# 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

[[email protected] ~]# 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


以上是关于将多行文件转换为一行的主要内容,如果未能解决你的问题,请参考以下文章

将多行转换为一行

ORACLE——将多行文本数据转换成一行

将某一多行的fasta文件转换为单行的fasta文件

使用 Linux 工具将多行 csv 转换为单行

如何将 CSV 文件转换为多行 JSON?

将单行转换为多行 Bigquery SQL