Merging lots of histogram in one .txt file

Dear all,

I have a problem and I don’t know the solution, can you help me?
I have to merge one column data from .txt files in a only file.
For exemple:
0
0
0
1
1
2

I can use Add but I have 2k spectra and I don’t know how to do it.

It is not really clear … you have several small.txt files with one column of number each and you want to have one single big .txt file with one column being the concatenation of all the small files ?

yes, I have one column files of int that I want to merge.
At the end the column line file.txt will be the sum of all the column keeping fixed the position of each int.
I need later to insert it in a TH1F and plot but I know how to do it.

Does this help ?

I tried to use cat command but I can’t find a command to sum the raw.
The biggest problem with cat is that I can’t do it 2000 times awever…
Do you know a way with a macro or a function that I can build in a cpp program?

I am not sure I understand… you want to do the sum of all the ross ? so you get a single value ?

That cab be done in with one command:

cat *.txt > output.dat

I need to do the following
file1 file 2 …(the first column is the column of file1 and the second column is the column of file2)
0 1
0 2
1 3
2 4
… …
at the end I want:
file_sum
1
2
4
6

file1.txt (7 Bytes)
file2.txt (7 Bytes)
filesum.C (551 Bytes)

$ root filesum.C
   ------------------------------------------------------------
  | Welcome to ROOT 6.17/01                  https://root.cern |
  |                               (c) 1995-2019, The ROOT Team |
  | Built for macosx64 on Mar 26 2019, 09:27:08                |
  | From heads/master@v6-16-00-rc1-1362-g1ced1dbf62            |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------

root [0] 
Processing filesum.C...
root [1] .q
$ cat filesum.txt
1
2
4
6

$ 

thank you so much. That perfectly answer my question

$ paste file1.txt file2.txt | awk '{ print $1+$2; }'
1
2
4
6

:expressionless:

1 Like

I gave you the cpp solution you asked but note the @berserker one. It might be suffisant ? … I do not know the exact context of your problem …

I have seen but your .cpp is better as solution to my problem.

1 Like

Note it can be improved. In particular you can compute the length of the buffer needed to do the sum or at least add a protection in case the size is exceeded. I just wrote the basic principles. There is spaces for improvements :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.