如何在制表符分隔的文本文件中追加到具有较少列的行? [英] How to append to the lines with fewer columns in a tab separated text file?

查看:55
本文介绍了如何在制表符分隔的文本文件中追加到具有较少列的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个制表符分隔的文本文件.有些行有10列,有些行有11列.我想在10列的最后一行中添加一个额外的列,其值为0.我该怎么做?

I have a tab separated text file. Some rows have 10 columns and some rows have 11 columns. I want to add an extra column to the last of 10 column rows with the value 0. How can i do this?

推荐答案

由于您已经提到了附加,因此您可以 awk 如下

Since you have mentioned append, you can awk as below

awk -F $'\t' 'BEGIN {OFS = FS} NF==10{$0=$0"0"}1' input-file

-F $'\ t'负责制表符分隔部分, BEGIN {OFS = FS} 用于设置输出字段分隔.

The -F $'\t' takes care of the tab-separation part, BEGIN {OFS = FS} for setting the output field separation.

NF == 10 仅查找只有10条记录的行,而 {$ 0 = $ 0"0"} 1 用于用多余的单词重建该行添加.

The NF==10 looks only for the lines having only 10 records and the {$0=$0"0"}1 for reconstructing that line with the extra word added.

要写入单独的文件,请使用> 重定向运算符

To write to a separate file use the > redirect operator as

awk -F $'\t' 'BEGIN {OFS = FS} NF==10{$0=$0"0"}1' input-file > output-file

要替换原始文件,请使用 mv

To replace the original file use mv

awk -F $'\t' 'BEGIN {OFS = FS} NF==10{$0=$0"0"}1' input-file > output-file ; mv output-file input-file

或者,如果您具有最新的 GNU Awk (自 4.1.0 发布以来),则可以选择就地"文件

Or if you have latest GNU Awk (since 4.1.0 released), it has the option of "inplace" file editing:

gawk -i inplace -F $'\t' 'BEGIN {OFS = FS} NF==10{$0=$0"0"}1' input-file

这篇关于如何在制表符分隔的文本文件中追加到具有较少列的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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