如何使用Awk从列显示文件名? [英] How to display filename from a column using Awk?

查看:492
本文介绍了如何使用Awk从列显示文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行添加到文件中的命令: 当前输入文件的名称+逗号数小于5的行的索引+行中逗号的数.

I'm trying to do a command which add to my file: Name of the current input file + Index of line where numbers of commas are less than 5 + numbers of commas across the line.

我得到了:

awk -F"," '{ if(NF > 5) printf("Filename: %d  Index: %d Number of commas : %d\n",FILENAME,NR, NF-1); }' dsc* >> filename.csv

输出为:

Filename: 0  Index: 68520 Number of commas : 6

索引和逗号似乎可以正常工作,但是文件名呢? 我在做什么错了?

Index and commas seems to work OK, but what about filename? What am I doing wrong?

应为例如

 Filename: dscabc.txt   Index: 68520 Number of commas : 6
 Filename: dscabc1.txt  Index: 123 Number of commas : 6

推荐答案

FILENAME是字符串,而不是数字.使用%s:

FILENAME is a string, not a number. Use %s:

awk -F"," '{ if(NF > 5) printf("Filename: %s  Index: %d Number of commas : %d\n",FILENAME,NR, NF-1); }' dsc* >> filename.csv

man awk讨论printf的部分中:

  %d, %i  A decimal number (the integer part).

  %s      A character string.

这篇关于如何使用Awk从列显示文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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