使用awk计算多列中的字数

时间:2017-10-31 00:00:18

标签: linux unix awk

输入文件(制表符分隔)

1 . Hello World . 51.4 . This is a text . 200
2 . Another line . 16.4 . Some more words . 600

所需输出(制表符分隔)

Hello World . 2 . This is a text . 4 
Another line . 2 . Some more words . 3

输出是第2列和第4列,它们的字数

我已经

awk '{print $2, "\t", NF}' > output.tsv

但不知道如何在单个命令中为多个列执行此操作

1 个答案:

答案 0 :(得分:1)

awk救援!

awk 'BEGIN {FS=OFS="\t"} 
           {print $2,split($2,x," +"),$4,split($4,x," +")}' file

Hello World     2   This is a text  4
Another line    2   Some more words 3