0

I have 68 data files. All of them have the same values in the first column, but different values in the second column.

I need to create a new file containing the first column and ANOTHER COLUMNS who is the sum of all second columns.

How can I do this?


  • 1
    Can you give an example of the data? – Nathan McCoy Sep 13 '16 at 10:24
  • It is a three columns file – Lodvert POATY Sep 13 '16 at 10:33
  • 1
    If you add the sample data to your question instead of a comment, you can use formatting to make it more readable. – Wieland Sep 13 '16 at 10:45
  • Then what's the problem Lodvert ? – SHW Sep 13 '16 at 10:54
  • 2
    Could you please add a sample of two files (say, with three lines each) and what output you expect to your question? It is not entirely clear what you want. – countermode Sep 13 '16 at 11:12
0

Easy done with awk

awk '{sum[$1]+=$2}END{for(i in sum)print i sum[i]}' file{1..68}.data
  • That assumes the values in the first column are unique and it won't preserve the order, it would be better to index on FNR and use two arrays. – Stéphane Chazelas Sep 13 '16 at 11:55
0
awk '{f1[FNR] = $1; f2[FNR] += $2}
     FNR > n {n = FNR}
     END {for (i = 1; i <= n; i++) print f1[i], f2[i]}' ./*.data
  • This just popped up in the review queue. Stephane, you should know how to answer on SE, so please... – countermode Sep 13 '16 at 14:55
  • @countermode, awk code is generally self-explanatory. Any addition to that would be useless paraphrasing. – Stéphane Chazelas Sep 13 '16 at 14:57
  • Lol! - no, not for beginners I'm afraid, and not all of us don't speak awk fluently as you do either... – countermode Sep 13 '16 at 15:01
  • @countermode, I'm not going to teach the basic principles of awk scripting (which IMO is the only prerequisite for understanding that answer) in all the awk answers I give. Please feel free to edit the answer to show me how you would improve it. – Stéphane Chazelas Sep 13 '16 at 15:04
  • awk '{f1[FNR] = $1; f2[FNR] += $2} FNR > n {n = FNR} END {for (i = 1; i <= n; i++) print f1[i], f2[i]}' ./*.data (It's working ... Thank you to all. – Lodvert POATY Sep 14 '16 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.