简体   繁体   English

将列或行添加到csv文件(matlab或cmd)

[英]Adding a column or row to a csv file (matlab or cmd)

This is a very common question I believe but I could not find an accurate enough answer. 我相信这是一个非常常见的问题,但我找不到足够准确的答案。 I need an answer either using MATLAB or using a simple enough command line prompt. 我需要使用MATLAB或使用足够简单的命令行提示符的答案。 I have (many) .csv files in a directory which all are similar. 我的目录中有(很多).csv文件,它们都很相似。 Say the files look like, 说文件看起来像,

1,2
3,4

I now want them to all look like 我现在希望他们都看起来像

,c1,c2
t1,1,2
t2,3,4

I am not a 100% sure on how to format the entries correctly on StackExchange. 我不确定如何在StackExchange上正确格式化条目。 There is no blank line between the first and secnd row. 第一行和第二行之间没有空白行。 What I am doing is - I add a column "t1 t2..." and row "c1 c2..." to all the files. 我正在做的是-向所有文件添加一列“ t1 t2 ...”和行“ c1 c2 ...”。 The very first cell in the file is blank. 文件中的第一个单元格为空白。 I need this done to all files in my directory. 我需要对目录中的所有文件执行此操作。 Of course the t1 t2 values and c1 c2 values should run for the entire size of the file. 当然,t1 t2值和c1 c2值应在文件的整个大小上运行。

How do I accomplish this? 我该如何完成?

Here's another awk script: 这是另一个awk脚本:

#!/usr/bin/awk -f

BEGIN { FS=OFS="," }
{
    if( FNR==1 ) {
        close(fn); fn=FILENAME ".new"; printf OFS > fn

        for(i=1; i<=NF; i++) {
            printf "c%d%s", i, (i!=NF) ? OFS:"\n" > fn
        }
    }
    print "t"FNR OFS $0 > fn
}

which does the following: 它执行以下操作:

  • BEGIN { FS=OFS="," } - sets the field separators to ,` BEGIN { FS=OFS="," } - sets the field separators to
  • FNR==1 - In the first line of each input file, close(fn) in case it's open, set fn equal to "your filename.new", then print a single leading , . FNR==1在每个输入文件的第一行中,如果打开则close(fn) ,将fn设置为“ your filename.new”,然后打印单个前导, FNR is the line number in the current file. FNR是当前文件中的行号。
  • Also create the c# headers, based on the number of fields in the first data row and print those to fn . 还根据第一个数据行中的字段数创建c#标头,并将其打印到fn

Then, treat all lines like they are valid data lines( no comments ): 然后,将所有行都视为有效数据行(无注释):

  • Prepend a t# OFS to each data row and then print it to fn 在每个数据行前添加一个t# OFS ,然后将其打印到fn

I placed the awk script into a file script.awk and then chmod +x script.awk . 我将awk脚本放入文件script.awk ,然后将chmod +x script.awk放入文件中。

Given the following two input data files ( data1 and data2 ) in the same directory: 在同一目录中给定以下两个输入数据文件( data1data2 ):

cat data1 data2
1,2 
3,4 
1,2,3
4,5,6
7,8,9

and running the script like script.awk data* gives the following two .new data files: 并运行脚本(如script.awk data*可提供以下两个.new数据文件:

cat data1.new data2.new
,c1,c2
t1,1,2 
t2,3,4 
,c1,c2,c3
t1,1,2,3
t2,4,5,6
t3,7,8,9

As long as the file names are similar to each other and don't match the script's name, it should be easy to pass them all to the script. 只要文件名彼此相似且与脚本名称不匹配,就应该很容易将它们全部传递给脚本。 I didn't anything to align the columns as the numbers increase in length, just enough to give one leading space to the c# row. 随着数字长度的增加,我没有什么办法使列对齐,仅够给c#行留一个前导空格。

You should show some code you have tired and if it doesn't work, you can ask the people on stack overflow, Most people here won't even help you without code. 您应该向您展示一些您已经厌倦的代码,如果它不起作用,您可以询问堆栈溢出的人,如果没有代码,这里的大多数人甚至都不会帮助您。 Here is an outline of what to do, you can implment it in your favorite programming language/environment 这是做什么的概述,您可以在自己喜欢的编程语言/环境中实现它

  1. create a new output file 创建一个新的输出文件
  2. Open your existing csv file 打开您现有的csv文件
  3. Add your new rows to the top of the output file (c1 c2 c3..) 将新行添加到输出文件的顶部(c1 c2 c3 ..)
  4. for each line in csv 对于csv中的每一行

    now we are trying to create each line in the output file 现在我们正在尝试在输出文件中创建每一行

    a. 一种。 split line using spaces (or some other delimiter) 使用空格(或其他定界符)分隔线

    b. write t1 to output and insert delimiter 将t1写入输出并插入定界符

    c. C。 write the 1st and 2nd "tokens" from your split 从拆分中写下第一和第二“令牌”

    d. d。 write t2 to output and insert delimter 写入t2以输出并插入除杂

    e. write the remain columns to the line 将其余的列写入行

    f. F。 insert newline character in output file 输出文件中插入换行符

  5. close both csv and output file 关闭csv输出文件

optional "overwriting" the original csv 可选的“覆盖”原始CSV

  1. delete original csv 删除原始的CSV
  2. rename output file to your old csv file name 输出文件重命名为您的旧csv文件名
awk '{
      FS=" "
      if (NR == 1) {
         printf ("%s", FS)
         for (col=1;col < NF;col++) printf( "c%d%s", col, FS)
         printf( "c%d\n", NF)
         }
     if ( $0 !~ /^[[:space:]]*$/ ) {
         printf ( "t%d%s%s\n", NR, FS, $0)
         }
     }' YourFile
  • assume here (strange for a csv) that space is the separator, for any more traditionnal separator, just adapt the FS=" " with your separator value 假设这里(对于csv来说很奇怪),空格是分隔符,对于任何其他传统的分隔符,只需将FS=" "为您的分隔符值
  • add your header column on first line 在第一行添加标题列
  • add the tX start of line 添加tX行首
  • take 1st line as reference for number of column in header 以第一行作为标题中列数的参考

add a find ... or batch loop to change it in every csv you have in your folder 添加find ...或批处理循环以在文件夹中的每个csv中对其进行更改

Using GNU awk 4.* for -i inplace : -i inplace使用GNU awk 4. *:

$ cat tst.awk
BEGIN { FS=OFS="," }
FNR==1 {
    for (i=1;i<=NF;i++)
        printf "%sc%d%s",(i>1?"":OFS),i,(i<NF?OFS:ORS)
}
{ print "t" FNR, $0 }

$ cat file
1,2
3,4

$ awk -i inplace -f tst.awk file

$ cat file
,c1,c2
t1,1,2
t2,3,4

Just change file to * and it'll update every file in your directory. 只需将file更改为* ,它将更新目录中的每个文件。

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

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