简体   繁体   English

如何用同一列中前一个单元格的数据填充CSV中的空白单元格?

[英]How to fill empty cells in a CSV with data from a previous cell in the same column?

I have a large tab-seperated CSV file. 我有一个很大的制表符分隔的CSV文件。 It is missing some data, however: 但是,它缺少一些数据:

1      cat    The cat ate the fish.
       dog    The dog played in the yard.
       fish   The fish went to the river.
2      eagle  The eagle flew in the sky.
              The eagle stopped in the mountains.
       bear   The bear ate the honey.

I need to fill all of the empty cells with whatever data appears in the previous rows. 我需要用上一行中显示的任何数据填充所有空单元格。 The output would look like this: 输出如下所示:

1      cat    The cat ate the fish.
1      dog    The dog played in the yard.
1      fish   The fish went to the river.
2      eagle  The eagle flew in the sky.
2      eagle  The eagle stopped in the mountains.
2      bear   The bear ate the honey.
  • Preferably, the method only edits one specified column at a time, and must be run multiple times with different columns specified, to get the entire CSV fully filled. 优选地,该方法一次仅编辑一个指定的列,并且必须在指定的不同列下运行多次,以完全填充整个CSV。

Is there any way to fill the empty cells in a CSV with the contents of a previous cell in the same column that does have data? 有没有办法用同一列中有数据的前一个单元格的内容填充CSV中的空白单元格?

awk solution to do the whole file: awk解决方案来做整个文件:

awk -F\\t '
    {
      for (i=1;i<=NF;++i) if ($i != "") a[i] = $i;
      if (na < NF) na = NF;
      for (i=1;i<na;++i) printf "%s\t", a[i]
      printf "%s\n", a[na];
    }
    ' file.tsv

To just do a specified column: 只需执行指定的列:

awk -F\\t -v COL=2 '
    $COL=="" {$COL = saved}
    {saved = $COL; print}
    ' file.tsv

This should work for 1st and 2nd columns: 这应该适用于第一列和第二列:

awk -F '\t' '$1 != ""{p1=$1} NF==3{p2=$2} p1 && $1 == ""{$1=p1} p2 && NF==2{$0=$1 OFS p2 OFS $2} 1' OFS='\t' file
1   cat     The cat ate the fish.
1   dog     The dog played in the yard.
1   fish    The fish went to the river.
2   eagle   The eagle flew in the sky.
2   eagle   The eagle stopped in the mountains.
2   bear    The bear ate the honey.

Works for any missing columns 适用于任何缺少的列

awk -F\\t '
{ for (i=1;i<=NF;++i) 
    { if ($i != "") a[i] = $i;
      printf "%s\t", a[i]
    }
  printf RS
}' file

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

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