LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   add 250 single column files to create one file (https://www.linuxquestions.org/questions/programming-9/add-250-single-column-files-to-create-one-file-4175413657/)

wilelucho 06-27-2012 07:59 AM

add 250 single column files to create one file
 
Hi every one

I have the following problem

I have 250 single column files, I would like to generate a single file with 250 columns. Each file has 18265 rows.

I tried with sed, paste, and awk. I have the impression that awk is powerful enough to do the job, however I'm learnig about it. Also paste should do the job, but it simply doesn't.

Can you help me

firstfire 06-27-2012 08:33 AM

Hi.

Welcome to LQ!

What did you already tried? It looks like `paste' does exactly what you want:
Code:

$ cat f1
1
2
3
4
$ cat f2
a
b
c
d
e
f
$ paste f[12] f2 f1
1      a      a      1
2      b      b      2
3      c      c      3
4      d      d      4
        e      e
        f      f

What is the expected output?

danielbmartin 06-27-2012 08:37 AM

Quote:

Originally Posted by wilelucho (Post 4713290)
Also paste should do the job, but it simply doesn't.

paste should do the job. Why do you say it doesn't? What happens when you try?

Daniel B. Martin

wilelucho 06-27-2012 08:51 AM

That's right
Paste should do it

I tried

ls *.txt > lista.rtf
paste `less lista.rft` > complete_file.txt

Also I tried "manually"

paste file1.txt file2.txt file3.txt > newfile.txt
All I got is a two column file where data in files 2 and 3 are mixed up in the same column

What I would like to have is

Stn ALALAY YOTALA ... 250 files
lat -19.668 -17.258
lon -65.324 -66.147
1961 1.6000 9.8540
.
.
.
18265 rows

Thanks for your help

firstfire 06-27-2012 09:05 AM

Please use the [CODE] .. [/CODE] tags around your code and data to preserve formatting.
Your command
Code:

$ paste file1.txt file2.txt file3.txt > newfile.txt
should work. Please try it with smaller files and show us what you get, that is contents of file1.txt etc and newfile.txt.
BTW you can supply all 250 files using glob pattern:
Code:

$ paste file*.txt > newfile.txt

wilelucho 06-27-2012 11:21 AM

All right,
I'm pretty new to this things,

I have
Code:

$ more AGUIRRE.txt
Aguirre
-17.366667
-65.816666
-999.900
-999.900
-999.900
-999.900
-999.900
-999.900
-999.900
-999.900

$ more ALAMOS.txt
Alamos
-18.708334
-65.391663
-999.900
-999.900
-999.900
-999.900
-999.900
-999.900
-999.900
-999.900
-999.900

$more YACUIBA.txt
Yacuiba
-22.016666
-63.700001
0.000
32.000
0.000
45.000
0.000
3.500
0.000
0.000
0.000


Whenever I try
Code:

$ paste AGUIRRE.txt ALAMOS.txt YACUIBA.txt > newfile.txt
I have
Code:

$ more newfile.txt
Aguirre Yacuiba
-17.3666-22.016666
-65.8166-63.700001
-999.9000.000900
-999.90032.00000
-999.9000.000900
-999.90045.00000
-999.9000.000900
-999.9003.500900
-999.9000.000900
-999.9000.000900
-999.9000.000900

Actually I have 250 archives to be added

pixellany 06-27-2012 12:04 PM

I works correctly here (using cut and paste data from your post).

My only GUESS is that the actual files have some extra characters that somehow confuse paste. If you can attach the actual files, we can dig deeper.

wilelucho 06-27-2012 12:12 PM

3 Attachment(s)
All right pixellany,

Attached you will find the three (of 250) files

pixellany 06-27-2012 12:49 PM

bingo!! Those files are in DOS text format---you can use "dos2unix" to convert them.

(I found this by using hexdump (found the DOS 0d+0a sequence (carriage return + newline)---there is probably an easier way)

wilelucho 06-27-2012 12:58 PM

You nailed it! (I think) I was looking at the same thing, I'm converting my files, I'll yell if something arises

Thanks a lot

danielbmartin 06-27-2012 02:05 PM

Quote:

Originally Posted by firstfire (Post 4713333)
BTW you can supply all 250 files using glob pattern:
Code:

$ paste file*.txt > newfile.txt

For testing I created four single-column input files:
Code:

InFile1='/home/daniel/Desktop/LQfiles/dbm408inp1.txt'
InFile2='/home/daniel/Desktop/LQfiles/dbm408inp2.txt'
InFile3='/home/daniel/Desktop/LQfiles/dbm408inp3.txt'
InFile4='/home/daniel/Desktop/LQfiles/dbm408inp4.txt'

This code catenated them side-by-side.
Code:

InFiles='/home/daniel/Desktop/LQfiles/dbm408inp*.txt'
OutFile='/home/daniel/Desktop/LQfiles/dbm408out.txt'
paste -d'\0' $InFiles > $OutFile

Daniel B. Martin


All times are GMT -5. The time now is 06:28 AM.