简体   繁体   English

将索引列添加到 CSV 文件

[英]Add index column to CSV file

I have a large Comma-Separated File (6GB) and would like to add an index column to it.我有一个大的逗号分隔文件 (6GB),并想向其中添加一个索引列。 I'm looking at Unix type solutions for efficiency.我正在寻找 Unix 类型的解决方案以提高效率。 I'm using a Mac.我正在使用 Mac。

I have this:我有这个:

V1  V2  V3
0.4625  0.9179  0.8384
0.9324  0.2486  0.1114 
0.6691  0.7813  0.6705
0.1935  0.3303  0.4336

Would like to get this:想得到这个:

ID  V1  V2  V3
1   0.4625  0.9179  0.8384
2   0.9324  0.2486  0.1114
3   0.6691  0.7813  0.6705
4   0.1935  0.3303  0.4336

This will probably work:这可能会起作用:

awk -F'\t' -v OFS='\t' '
  NR == 1 {print "ID", $0; next}
  {print (NR-1), $0}
' input.csv > output.csv

In awk , the NR variable is "the total number of input records seen so far", which in general means "the current line number".awkNR变量是“迄今为止看到的输入记录总数”,通常表示“当前行号”。 So the NR == 1 in the first line is how we match the first record and add the "ID" column header, and for the remaining lines we use NR-1 as the index.因此,第一行中的NR == 1是我们如何匹配第一条记录并添加“ID”列标题,对于其余行,我们使用NR-1作为索引。

The -F'\\t' argument sets the input field separator, and -vOFS='\\t' sets the output field separator. -F'\\t'参数设置输入字段分隔符, -vOFS='\\t'设置输出字段分隔符。

Since no technology is specified in the original post, I'd be happy here to keep it simple.由于原始帖子中没有指定技术,我很乐意在这里保持简单。

(all the fancy Vim / bash solutions are fine if you know what you're doing) . (如果您知道自己在做什么,那么所有花哨的Vim / bash解决方案都很好)

  • Open the CSV file in your favourite spreadsheet programme (I'm using LibreOffice, but Excel or a native Mac equivalent will do)在您最喜欢的电子表格程序中打开 CSV 文件(我使用的是 LibreOffice,但 Excel 或本机 Mac 等效程序也可以)
  • insert a column to the left of column A在 A 列的左侧插入一列
  • Enter a 1 into cell A2, the first cell under the headers在单元格 A2(标题下的第一个单元格)中输入 1
  • Double-click the blob at the bottom right of the cell as shown in the screenshot:双击单元格右下角的 blob,如屏幕截图所示:

LibreOffice 魔法

This last step will fill the index column with 1,2,3... etc. You can then save the resulting spreadsheet as a CSV file again.最后一步将用1,2,3...等填充索引列。然后您可以再次将生成的电子表格另存为 CSV 文件。

I assume you have a commas delimited file.我假设你有一个逗号分隔的文件。

Using vim, open the file.使用 vim 打开文件。 In normal mode, type在正常模式下,键入

:%s/^/\=line('.').','/

:%s/^/\\=line('.')/ adds the line number at the beginning of the line. :%s/^/\\=line('.')/在行首添加行号。 Since you have a commas delimited file (add a column) you need a comma after your line number.由于您有一个逗号分隔的文件(添加一列),因此行号后需要一个逗号。 so the .','所以.','

see this answer for full explanation about :%s/^/\\=line('.')/有关:%s/^/\\=line('.')/完整解释,请参阅此答案

  1. Open the CSV file in your favorite spreadsheet program, such as Excel在您喜欢的电子表格程序(例如 Excel)中打开 CSV 文件
  2. Insert a column to the left side of first column在第一列的左侧插入一列
  3. Type 1 in the first cell of this column在此列的第一个单元格中键入 1
  4. Type an equation ' =A2+1 ' in the following cell在下面的单元格中键入方程“ =A2+1

在此处输入图片说明

  1. Double-click the blob at the bottom right of the cell as shown in the screenshot双击单元格右下角的 blob,如屏幕截图所示

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM