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.
I got:
awk -F"," '{ if(NF > 5) printf("Filename: %d Index: %d Number of commas : %d\n",FILENAME,NR, NF-1); }' dsc* >> filename.csv
The output is:
Filename: 0 Index: 68520 Number of commas : 6
Index and commas seems to work OK, but what about filename?
What am I doing wrong?
It should be eg:
Filename: dscabc.txt Index: 68520 Number of commas : 6
Filename: dscabc1.txt Index: 123 Number of commas : 6
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
From the section of man awk
that discusses printf
:
%d, %i A decimal number (the integer part).
%s A character string.
For strings, use %s
format in printf
:
awk -F"," '{ if(NF > 5) printf("Filename: %s Index: %d Number of commas : %d\n",FILENAME,NR, NF-1); }' dsc* >> filename.csv
FILENAME is an builtin variable in awk, which is always set to name of current file being read. The type of variable is string (represented by null terminated char array in c).
awk -F"," '{ if(NF > 5) printf("Filename: %d Index: %d Number of commas : %d\n",FILENAME,NR, NF-1); }' dsc* >> filename.csv
In this command you have given '%d' as the format specifier for 'FILENAME', which prompts awk to interpret 'FILENAME' as an integer. You can modify your command to use %s
instead to interpret it as string correctly. The modified command should look something like:
awk -F"," '{ if(NF > 5) printf("Filename: %s Index: %d Number of commas : %d\n",FILENAME,NR, NF-1); }' dsc* >> filename.csv