awk - Return number of records per file after processing multiple files -


i have following code runs on multiple tab delimited text files. sums amount particular field , counts number of records per file (processing multiple files). output filename,sum of field, count of records - per file. works well. issue instead of getting count of records per file, getting cumulative count of whole batch of files processed. how can fix this? tried replacing 'nr' 'fnr'. didn't work.

i calling awk through .bat file

awk -f sumcolumnrecordcount.awk *.txt 

this code in awk file

begin { fs="\t" } { sum[filename] += $42 } end {     (i=1;i<argc;i++)     printf "%s %15d %d\n",argv[i],sum[argv[i]],nr >>"output.txt" } 

running .bat file in windows 7 gawk (gnu awk?)

try quick adaptation of code (should work gnu<4 , non-gnu awks):

begin { fs="\t" } { sum[filename] += $42   last[filename] = fnr } end {     (i=1;i<argc;i++)     printf "%s %15d %d\n",argv[i],sum[argv[i]],last[argv[i]] >>"output.txt" } 

awk version without arrays (should work gnu < 4 , non-gnu awks):

begin {    fs="\t"  } function pr() {   printf "%s %15d %d\n", f, sum, last >>"output.txt" } fnr==1 {    if(nr>1) pr()   sum=last=0   f=filename } {    sum+=$42   last++ } end {   pr() } 

--edit-- if 1 or more input files empty, second version not print 0 filename.. (thanks @edmorton)


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -