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

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

how to split a column values into separate columns?

问题

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

  1. name class school
  2. a 1 sec
  3. b 2 pri
  4. c 3 hrsec

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

  1. name
  2. a
  3. b
  4. c

第2列应该是

  1. class
  2. 1
  3. 2
  4. 3

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

我使用了以下命令:

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

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

英文:

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

  1. name class school
  2. a 1 sec
  3. b 2 pri
  4. c 3 hrsec

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

  1. name
  2. a
  3. b
  4. c

column 2 should be

  1. class
  2. 1
  3. 2
  4. 3

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

I used

  1. 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):

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

Excel将使用这些列。

英文:

Use , (for the CSV(

  1. 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:

确定