Awk

如何在 Linux 中將空格分隔的文件轉換為 Excel 文件?

  • May 28, 2019

我有一個看起來像這樣的文件:

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 轉換它?

將每個空格用逗號替換成逗號分隔值文件,並用XLS後綴保存;然後您可以將其讀入 Excel(它將讀取逗號分隔值數據並自動將其轉換為 XLS 格式),使用它並保存它。這是一個使用 sed 進行搜尋和替換的單行程序,然後將數據保存為帶有 XLS 後綴的 CSV 格式,以及 Bob 的你叔叔。

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

嘗試使用以下命令,它工作正常

命令

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

引用自:https://unix.stackexchange.com/questions/520892