LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   "catting" files in one file side by side, not among each other (https://www.linuxquestions.org/questions/programming-9/catting-files-in-one-file-side-by-side-not-among-each-other-552515/)

indyn00b 05-09-2007 05:03 AM

"catting" files in one file side by side, not among each other
 
Hi!

I want to write a script that takes several files and places the data in these files side by side like a table. Something like "cat *.dat >> target.dat" but side by side for every .dat. Is this somehow possible? With standard tools?

Regards,
n00b

radoulov 05-09-2007 05:21 AM

Code:

paste *.dat>target.dat

pixellany 05-09-2007 08:35 AM

Quote:

Originally Posted by radoulov
Code:

paste *.dat>target.dat

I think OP actually wants the data interleaved---ie each entry in a file is listed next to the correspnding entry in another file.

I would say set up a loop which reads one line from each file and puts the data into an array--which then can be written out to a new file.

PSEUDO-CODE:
Code:

N = 1  (line counter)
array (X,Y)
start loop
  read file1, line N into array (1,N)
  read file2, line N into array (2,N)
  etc.
  increment N
end loop
write array > newfilename


radoulov 05-09-2007 08:43 AM

Quote:

Originally Posted by pixellany
I think OP actually wants the data interleaved---ie each entry in a file is listed next to the correspnding entry in another file.
[...]

Which is what the paste command/utility does:

Code:

$ cat file1
1 a
2 a
$ cat file2
1 b
2 b
$ paste file1 file2
1 a    1 b
2 a    2 b


pixellany 05-09-2007 10:54 PM

Quote:

Originally Posted by radoulov
Which is what the paste command/utility does:

That's not totally obvious from the man page, but I certainly don't doubt what you are saying.

It seems obvious that past expects data in a certain format....

indyn00b 05-10-2007 02:05 AM

Hi!
"paste" did exactly what I wanted, many thanks!

-n00b


All times are GMT -5. The time now is 08:21 AM.