bash 将索引列添加到 CSV 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30530407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:04:44  来源:igfitidea点击:

Add index column to CSV file

bashcsv

提问by SharkSandwich

I have a large Comma-Separated File (6GB) and would like to add an index column to it. I'm looking at Unix type solutions for efficiency. I'm using a Mac.

我有一个大的逗号分隔文件 (6GB),并想向其中添加一个索引列。我正在寻找 Unix 类型的解决方案以提高效率。我正在使用 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

回答by larsks

This will probably work:

这可能会起作用:

awk -F'\t' -v OFS='\t' '
  NR == 1 {print "ID", 
:%s/^/\=line('.').','/
; next} {print (NR-1), ##代码##} ' input.csv > output.csv

In awk, the NRvariable is "the total number of input records seen so far", which in general means "the current line number". So the NR == 1in the first line is how we match the first record and add the "ID" column header, and for the remaining lines we use NR-1as the index.

在 中awkNR变量是“到目前为止看到的输入记录总数”,一般表示“当前行号”。所以NR == 1第一行是我们如何匹配第一条记录并添加“ID”列标题,对于剩余的行,我们NR-1用作索引。

The -F'\t'argument sets the input field separator, and -vOFS='\t'sets the outputfield separator.

-F'\t'参数设置输入字段分隔符,并-vOFS='\t'设置输出字段分隔符。

回答by LondonRob

Since no technology is specified in the original post, I'd be happy here to keep it simple.

由于原始帖子中没有指定技术,我很乐意在这里保持简单。

(all the fancy Vim/bashsolutions 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)
  • insert a column to the left of column A
  • Enter a 1 into cell A2, the first cell under the headers
  • Double-click the blob at the bottom right of the cell as shown in the screenshot:
  • 在您最喜欢的电子表格程序中打开 CSV 文件(我使用的是 LibreOffice,但 Excel 或本机 Mac 等效程序也可以)
  • 在 A 列的左侧插入一列
  • 在单元格 A2(标题下的第一个单元格)中输入 1
  • 双击单元格右下角的 blob,如屏幕截图所示:

LibreOffice wizardry

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 文件。

回答by Luc M

I assume you have a commas delimited file.

我假设你有一个逗号分隔的文件。

Using vim, open the file. In normal mode, type

使用 vim 打开文件。在正常模式下,键入

##代码##

:%s/^/\=line('.')/adds the line number at the beginning of the line. Since you have a commas delimited file (add a column) you need a comma after your line number. so the .','

:%s/^/\=line('.')/在行首添加行号。由于您有一个逗号分隔的文件(添加一列),因此行号后需要一个逗号。所以.','

see this answerfor full explanation about :%s/^/\=line('.')/

请参阅此答案以获取有关:%s/^/\=line('.')/

回答by Hajar Homayouni

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

enter image description here

在此处输入图片说明

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

enter image description here

在此处输入图片说明