0

Challenge: add up the values in column 6 of a spreadsheet starting at row 3 and going to row X (X because the number of rows in each file are variable).

I have ~1000 files in a directory that I would like to do this for. I managed to come up with the following:

awk 'BEGIN {FS = ","} FNR>2 {sum+=$6} END {print sum}' SR* >output.txt

This works, but I want the sum to be printed for each input file separately, all into one output file. What do I need to change in my script to accomplish that?

Any help would be greatly appreciated - and please keep in mind I really don't have a firm understanding for using the command line.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • tripleee thank you for the help and re-direct, and I have to apologize because I'm VERY new here and don't really understand how to apply/combine: cmd [option] [filename] > results.out with my info? What does [option] mean? is that where I put in my awk script? and what does > results.out mean? – David Salzman Feb 08 '18 at 05:51
  • It's just a placeholder for your command. The redirect creates one output file for each input file. So `for file in SR*; do awk '... things ... ' "$file" >"$file.out"; done` will create SR1.out for SR1, SR2.out for SR2, etc. If that's not what you want, please [edit] your question to clarify what you are looking for. – tripleee Feb 08 '18 at 05:59
  • It would be more efficient to refactor your script to have Awk notice when the file name changes and print the result for the previous file and start over from zero; but baby steps... – tripleee Feb 08 '18 at 06:01
  • tripleee - thank you for the time and help! that's exactly what I'm trying to do: 1 output file for each input file - you described the objective perfectly...I'm trying to figure out how to execute based on the suggestion: here's where I'm at: $ find /path to directory -exec cmd awk 'BEGIN {FS = ","} FNR>2 {sum+=$6} END {print sum}' {SRR*} \; > results.out I'm def doing something wrong b/c terminal tells me find: cmd: No such file or directory so I'm definitely doing some thing wrong. however a (blank) text file called results.out was generated in the directory which I find amazing! – David Salzman Feb 08 '18 at 06:24
  • actually I should specify more clearly: I'd prefer 1 output file that has the answer from all the input files (not 1 output per input) – David Salzman Feb 08 '18 at 06:26
  • `cmd` indicates you're on Windows, not U*x. Awk is a standard Unix tool so if you're on a completely different platform, that should be made very clear in your question. It's not hard to write the same loop in Windows but the syntax is different. Look for a duplicate tagged [tag:batch] and/or [tag:cmd] and/or [tag:windows] – tripleee Feb 08 '18 at 07:49
  • Possible duplicate of https://stackoverflow.com/questions/14237548/batch-script-run-command-on-each-file-in-directory – tripleee Feb 08 '18 at 07:50
  • I refactored your question and nominated it for reopening. With a downvote and a history of closing, it is unfortunately hard to predict whether this will garner enough reopen votes to actually be reopened. I'll see if I can find a duplicate for the updated question. – tripleee Feb 08 '18 at 08:00

0 Answers0