如何填充空单元中与来自在同一列中的previous细胞的CSV? [英] How to fill empty cells in a CSV with data from a previous cell in the same column?
本文介绍了如何填充空单元中与来自在同一列中的previous细胞的CSV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个大的标签分隔的CSV文件。它缺少一些数据,但是:
I have a large tab-seperated CSV file. 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.
我需要填写的所有与任何数据出现在previous行空单元格。输出应该是这样的:
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,该方法一次只能编辑一个特定的列,并且必须与运行指定不同的列多次,让整个CSV填满。
有什么办法来填补空单元格用previous细胞中确实有数据同一列的内容的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?
推荐答案
awk的解决方案做整个文件:
awk solution to do the whole file:
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
这篇关于如何填充空单元中与来自在同一列中的previous细胞的CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文