填充/填充CSV文件中缺少的列(使用标签) [英] Pad/Fill missing columns in CSV file (using tabs)

查看:164
本文介绍了填充/填充CSV文件中缺少的列(使用标签)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些使用TAB作为分隔符的CSV文件.这些行具有可变的列数,我想对其进行归一化.

I have some CSV files with TAB as separator. The lines have variable amount of columns and I want to normalize that.

我需要准确地说10列,因此我想在第10列之前增加空列,以防列数减少.

I need exactly say 10 columns so effectively I want to add empty column up until 10th column in case it has fewer columns.

我还想循环播放文件夹中的所有文件并更新相应的文件,而不仅仅是输出或写入新文件.

Also I would like to loop all files in a folder and update the corresponding file and not just output or write to a new file.

我可以这样处理逗号:

awk -F, '{$10=""}1' OFS=',' file.txt

但是将其更改为\t时,我中断了并添加了太多列:

But when changing it to \t i breaks and adds too many columns:

awk -F, '{$10=""}1' OFS='\t' file.txt

有输入吗?

推荐答案

如果您具有GNU awk(有时称为gawk),这将确保您有十列,并且如果已经存在,则不会擦除十分之一.在那里:

If you have GNU awk (sometimes called gawk), this will make sure that you have ten columns and it won't erase tenth if it is already there:

awk -F'\t' -v OFS='\t' '{NF=10}1' file >file.tmp && mv file.tmp file

Awk用户非常重视简洁性,并且JID所建议的进一步简化是可能的.由于在awk下,NF=10的计算结果为true,因此我们可以在导致打印行的同时将NF设置为10:

Awk users value brevity and a further simplification, as suggested by JID, is possible. Since, under awk, NF=10 evaluates to true, we can set NF to 10 at the same time that we cause the line to be printed:

awk -F'\t' -v OFS='\t' 'NF=10' file >file.tmp && mv file.tmp file

MacOS::在Mac上,默认awk是BSD,但可以使用brew install gawk安装GNU awk(gawk).

MacOS: On a Mac, the default awk is BSD but GNU awk (gawk) can be installed using brew install gawk.

这篇关于填充/填充CSV文件中缺少的列(使用标签)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆