如何将一个列的值拆分成单独的列?

huangapple go评论76阅读模式
英文:

how to split a column values into separate columns?

问题

我有一个名为details.txt的txt文件,内容如下:

name class school

a 1 sec

b 2 pri

c 3 hrsec

我希望用空格分隔的值作为不同的列。所以第1列应该是:

name

a

b

c

第2列应该是

class

1

2

3

最后,我希望将文件保存为.csv文件。

我使用了以下命令:

awk -F " " 'OFS="\t" {print $1,$2,$3 > ("output.csv")}' details.txt

但是当我在Excel中打开输出文件时,所有的值都在同一列中。

英文:

I have a txt file, details.txt, like this:

name class school

a 1 sec

b 2 pri

c 3 hrsec

I want the values separated by space as different columns.
so column 1 should be

name

a

b

c

column 2 should be

class

1

2

3

By the end I want the file to be a .csv file.

I used

awk -F " " 'OFS="\t" {print $1,$2,$3 > ("output.csv")}' details.txt

When I open the output file in excel all the values are in the same column.

答案1

得分: 0

使用,(用于CSV):

awk -vOFS=, '{print $1,$2,$3}' details.txt > output.csv

Excel将使用这些列。

英文:

Use , (for the CSV(

awk -vOFS=, '{print $1,$2,$3}' details.txt > output.csv

and Excel would take the columns

huangapple
  • 本文由 发表于 2023年5月24日 23:32:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定