Need help combining large number of text files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help combining large number of text files
# 1  
Old 03-06-2010
Need help combining large number of text files

Hi,
i have more than 1000 data files(.txt) like this

first file format:
178.83 554.545
179.21 80.392


second file:
178.83 990.909
179.21 90.196


etc.

I want to combine them to the following format:

178.83,554.545,990.909,...
179.21,80.392,90.196,...

such that the first column(which is identical across all the files) is preserved, but the other data fields are added onto the same line by comma(or space/tab) separation.

I am very new to shell scripting, hope to receive some kind advice on my question.

Cheers!
Daniel
# 2  
Old 03-06-2010
Actually do you want to compare only 2 files or 1000 files at a same time?
# 3  
Old 03-06-2010
Whether your all 1000 files have same no of lines?
# 4  
Old 03-06-2010
thanks for the reply, everyone.

Yes, all my files have exactly the same number of lines (2048 lines).

each file consist of 2 columns. a 'field' and a 'data', if u like, as per my earlier post

what i want to achieve is to get a big final 'table' file which shows

field data(from file 1) data(from file 2) data(from file 3).... data(from file 1000)

hope this clarifies. thanks again for the help!
# 5  
Old 03-06-2010
the join command could probably do the best for that purpose.
# 6  
Old 03-06-2010
Try:

Code:
awk '{ A[$1]=A[$1]" "$2;}  END { for (i in A) print i,A[i];}' *.txt >outfile

# 7  
Old 03-06-2010
Hello frans,we can't use the join command directly to solve this problem.Because,mr_monocyte wants to deal with 1000 files not only two files which is accepted by join command.But,its a good idea if we have less number of files.mr_monocyte,use the following script to do your requirement.

Code:
k=0
for i in $* #reading the command line arguments which are all files given
do
k=0
while read line  #reading line by line from the file
do
if [[ $i == $1 ]];then #checking whether the file received is first file
arr[$k]=$line
let k+=1
else
val=`echo $line | cut -d ' ' -f 1` #getting the first id to match
if [[ $val == `echo ${arr[$k]} | cut -d ' ' -f 1` ]];then #checking whether both the values are equal
result[$k]=${result[$k]},`echo $line | cut -d ' ' -f 2` #storing it in an array
let k+=1 #incrementing the array index
fi
fi
done < $i
done

for((l=0;l<${#result[@]};l++)) #traversing the list stored in an array
do
echo `echo ${arr[$l]} | cut -d ' ' -f 1`,`echo ${arr[$l]} | cut -d ' ' -f 2`${result[$l]} #printing the resulting output
#If you want the output to be in file,use the following command
echo `echo ${arr[$l]} | cut -d ' ' -f 1`,`echo ${arr[$l]} | cut -d ' ' -f 2`${result[$l]} >> result #printing the resulting output
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing large number of temp files

Hi All, I am having a situation now to delete a huge number of temp files created during run times approx. 16700+ files. We have never imagined that we will get this this much big list of files during run time. It worked fine for lesser no of files in the list. But when list is huge we are... (7 Replies)
Discussion started by: mad man
7 Replies

2. Shell Programming and Scripting

Sftp large number of files

Want to sftp large number of files ... approx 150 files will come to server every minute. (AIX box) Also need make sure file has been sftped successfully... Please let me know : 1. What is the best / faster way to transfer files? 2. should I use batch option -b so that connectivity will be... (3 Replies)
Discussion started by: vegasluxor
3 Replies

3. UNIX for Dummies Questions & Answers

Rename a large number of files in subdirectories

Hi, I have a large number of subdirectories (>200), and in each of these directories there is a file with a name like "opp1234.dat". I'd like to know how I could change the names of these files to say "out.dat" in all these subdirectories in one go. Thanks! (5 Replies)
Discussion started by: lost.identity
5 Replies

4. Shell Programming and Scripting

Using find in a directory containing large number of files

Hi All, I have searched this forum for related posts but could not find one that fits mine. I have a shell script which removes all the XML tags including the text inside the tags from some 4 million XML files. The shell script looks like this (MODIFIED): find . "*.xml" -print | while read... (6 Replies)
Discussion started by: shoaibjameel123
6 Replies

5. UNIX for Dummies Questions & Answers

Combining two text files as columns?

I have one space delimited file with multiple columns and one tab delimited file with multiple columns (They have the same number of rows). I want to basically combine these two text files into a new text file by column. How would I go about doing that? (1 Reply)
Discussion started by: evelibertine
1 Replies

6. UNIX for Dummies Questions & Answers

Delete large number of files

Hi. I need to delete a large number of files listed in a txt file. There are over 90000 files in the list. Some of the directory names and some of the file names do have spaces in them. In the file, each line is a full path to a file: /path/to/the files/file1 /path/to/some other/files/file 2... (4 Replies)
Discussion started by: inakajin
4 Replies

7. Shell Programming and Scripting

Concatenation of a large number of files

Hellow i have a large number of files that i want to concatenate to one. these files start with the word 'VOICE_' for example VOICE_0000000000 VOICE_1223o23u0 VOICE_934934927349 I use the following code: cat /ODS/prepaid/CDR_FLOW/MEDIATION/VOICE_* >> /ODS/prepaid/CDR_FLOW/WORK/VOICE ... (10 Replies)
Discussion started by: chriss_58
10 Replies

8. Shell Programming and Scripting

Script to Compare a large number of files.

I have a large Filesystem on an AIX server and another one on a Red Hat box. I have syncd the two filesystems using rsysnc. What Im looking for is a script that would compare to the two filesystems to make sure the bits match up and the number of files match up. its around 2.8 million... (5 Replies)
Discussion started by: zippdawg2001
5 Replies

9. Shell Programming and Scripting

moving large number of files

I have a task to move more than 35000 files every two hours, from the same directory to another directory based on a file that has the list of filenames I tried the following logics (1) find . -name \*.dat > list for i in `cat list` do mv $i test/ done (2) cat list|xargs -i mv "{}"... (7 Replies)
Discussion started by: bryan
7 Replies

10. AIX

combining two input text files

hi! i would like to process two input text files text1 9835023 20051004F2_011 9835021 20060904FAL0132006 8835099 20051004HOL011 8835044 20051004H1_011 6835023 20061002HAL0132006 4835099 20050721F1_011 4835088 ... (6 Replies)
Discussion started by: d3ck_tm
6 Replies
Login or Register to Ask a Question