如何在Linux中将空格分隔的文件转换为Excel文件?

如何在Linux中将空格分隔的文件转换为Excel文件?

我有一个如下所示的文件:

ID avedebv debv2019 ebv2018 gebv2018 number avecvlean
7063993 1.06109 0.5325 0.7420 1.5084 206 66.5962
7076247 1.67947 2.1066 2.8630 3.0629 460 67.6856
7076881 -0.236859 -2.6794 0.8820 1.9994 282 66.1557
7126254 -0.409945 -2.2293 0.5860 1.2695 133 66.0323
7178892 0.464492 -0.0859 1.2640 1.3655 327 66.3985
7179256 1.33735 2.2111 1.4730 1.0873 48 67.9671
7247048 0.875797 0.8434 1.5680 2.3221 86 67.8639
7247721 2.44818 3.8560 2.2590 3.7478 306 69.2416
7271572 2.54853 4.1963 2.5310 2.4604 268 68.9649
7271958 1.48163 2.1094 2.6480 2.4154 396 67.8631
7310883 2.07762 2.6693 0.5610 0.8019 164 69.8769
7339810 1.93014 2.7640 2.4130 2.0771 112 70.453
7362729 1.79878 2.5744 1.5900 2.6763 247 69.8597
7383443 1.20869 1.0631 0.6140 0.7587 229 69.4386
7384385 1.95598 1.2635 3.1930 2.1413 32 71.9848
7385021 3.01675 3.7090 1.3690 1.4910 22 71.2533
7385509 0.686986 -0.1679 0.1500 0.2087 81 69.8795
7392631 2.07126 4.0996 0.8400 1.6370 103 70.6173 

我想将其格式更改为 Excel 文件,这样我就可以在 Excel 中处理这些数据。如何在 Linux 上转换它?

答案1

将每个空格替换为逗号,转为逗号分隔值文件,并以XLS后缀保存;然后您可以将其读入 Excel(它将读取逗号分隔的值数据并自动将其转换为 XLS 格式),使用它并保存它。这是一个单行代码,它使用 sed 进行搜索和替换,然后将数据保存为带有 XLS 后缀的 CSV 格式,以及 Bob 的你叔叔。

sed -e 's/\s\+/,/g' filename > filename.xls

答案2

尝试使用以下命令,效果很好

命令

awk '{gsub(" ",",",$0);print $0}' filename| sed "s/,$//g" >file.xls

输出

awk '{gsub(" ",",",$0);print $0}' filename| sed "s/,$//g"

    ID,avedebv,debv2019,ebv2018,gebv2018,number,avecvlean
    7063993,1.06109,0.5325,0.7420,1.5084,206,66.5962
    7076247,1.67947,2.1066,2.8630,3.0629,460,67.6856
    7076881,-0.236859,-2.6794,0.8820,1.9994,282,66.1557
    7126254,-0.409945,-2.2293,0.5860,1.2695,133,66.0323
    7178892,0.464492,-0.0859,1.2640,1.3655,327,66.3985
    7179256,1.33735,2.2111,1.4730,1.0873,48,67.9671
    7247048,0.875797,0.8434,1.5680,2.3221,86,67.8639
    7247721,2.44818,3.8560,2.2590,3.7478,306,69.2416
    7271572,2.54853,4.1963,2.5310,2.4604,268,68.9649
    7271958,1.48163,2.1094,2.6480,2.4154,396,67.8631
    7310883,2.07762,2.6693,0.5610,0.8019,164,69.8769
    7339810,1.93014,2.7640,2.4130,2.0771,112,70.453
    7362729,1.79878,2.5744,1.5900,2.6763,247,69.8597
    7383443,1.20869,1.0631,0.6140,0.7587,229,69.4386
    7384385,1.95598,1.2635,3.1930,2.1413,32,71.9848
    7385021,3.01675,3.7090,1.3690,1.4910,22,71.2533
    7385509,0.686986,-0.1679,0.1500,0.2087,81,69.8795
    7392631,2.07126,4.0996,0.8400,1.6370,103,70.6173

相关内容