How to merge some files with diffrent sizes into one excel file using shell?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to merge some files with diffrent sizes into one excel file using shell?
# 8  
Old 07-15-2013
Quote:
Originally Posted by krishmaths
If the number of records in each file is same then this approach would work. The problem to tackle here is how to paste files with varying number of records.
exactly. my problem is this
# 9  
Old 07-15-2013
Shell script solution

Posting a shell script approach. It creates 3 extra files that need to be removed at the end.

Code:
FILE1_LINES=$(wc -l file1 | awk '{print $1}')
FILE2_LINES=$(wc -l file2 | awk '{print $1}')
FILE3_LINES=$(wc -l file3 | awk '{print $1}')
MAX=$FILE1_LINES
if [ $MAX -lt $FILE2_LINES ]
then
 MAX=$FILE2_LINES
fi
if [ $MAX -lt $FILE3_LINES ]
then
 MAX=$FILE3_LINES
fi
awk 'BEGIN {OFS=","} {print $1,$2}' file1 >file11
awk 'BEGIN {OFS=","} {print $1,$2}' file2 >file21
awk 'BEGIN {OFS=","} {print $1,$2}' file3 >file31

FILE1_DEFICIT=$(expr $MAX - $FILE1_LINES)
while [ $FILE1_DEFICIT -gt 0 ]
do
 echo "," >>file11
 FILE1_DEFICIT=$(expr $FILE1_DEFICIT - 1)
done

FILE2_DEFICIT=$(expr $MAX - $FILE2_LINES)
while [ $FILE2_DEFICIT -gt 0 ]
do
 echo "," >>file21
 FILE2_DEFICIT=$(expr $FILE2_DEFICIT - 1)
done

FILE3_DEFICIT=$(expr $MAX - $FILE3_LINES)
while [ $FILE3_DEFICIT -gt 0 ]
do
 echo "," >>file31
 FILE3_DEFICIT=$(expr $FILE3_DEFICIT - 1)
done

paste -d, file11 file21 file31 >out.csv

# 10  
Old 07-15-2013
tnx alot. but It doesnt work yet :-(
# 11  
Old 07-15-2013
I tried with the sample data you have provided and it worked fine for me. Are you able to view the csv with correct number of commas on the terminal?

I assumed you do not have the header (title1) in the files.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tabbed multiple csv files into one single excel file with using shell script not perl

Hi Experts, I am querying backup status results for multiple databases and getting each and every database result in one csv file. so i need to combine all csv files in one excel file with separate tabs. I am not familiar with perl script so i am using shell script. Could anyone please... (4 Replies)
Discussion started by: ramakrk2
4 Replies

2. Shell Programming and Scripting

How to list files names and sizes in a directory and output result to the file?

Hi , I'm trying to list the files and output is written to a file. But when I execute the command , the output file is being listed. How to exclude it ? /tmp file1.txt file2.txt ls -ltr |grep -v '-' | awk print {$9, $5} > output.txt cat output.txt file1.txt file2.txt output.txt (8 Replies)
Discussion started by: etldeveloper
8 Replies

3. Shell Programming and Scripting

Merge two files on awk/shell

Hi, i have two files like these: FILE 1 00:0f:35:1b:0c:00 1402691094.750049000 00:0f:35:1b:0c:00 1402691087.474893000 44:d3:ca:fd:a2:08 1402691091.865127000 30:e4:db:c1:df:de 1402691090.192464000 FILE 2_ 00:0F:35 Cisco Systems, Inc 30:E4:DB Cisco Systems, Inc I need a file 3, that... (5 Replies)
Discussion started by: bertiko
5 Replies

4. Shell Programming and Scripting

Merge Two files on the basis of 2 columns at diffrent position

Hello, I am trying to merge two files with multiple records having a common columns but on first file its on 7th column and on 2nd file it on 6th column. First file(file1.txt) - 7th Column is unique H|123|Alex|Ren|W|9856746|2345789|20152202| H|97654|Ray|John||9855678|2345790|20152201|... (6 Replies)
Discussion started by: Mannu2525
6 Replies

5. Shell Programming and Scripting

To merge different sizes txt files

Hi, I have to .txt files that look like "baseMean" "log2FoldChange" "lfcSE" "stat" "pvalue" "padj" "c104215_g2_i4" 202.057864855455 5.74047973414006 1.14052672909697 5.03318299141063 4.8240223910525e-07 0.00234905721174879 "c91544_g1_i1" 373.123487095726 5.62496675850204 1.15060014539303... (2 Replies)
Discussion started by: alisrpp
2 Replies

6. Shell Programming and Scripting

Perl script to Merge contents of 2 different excel files in a single excel file

All, I have an excel sheet Excel1.xls that has some entries. I have one more excel sheet Excel2.xls that has entries only in those cells which are blank in Excel1.xls These may be in different workbooks. They are totally independent made by 2 different users. I have placed them in a... (1 Reply)
Discussion started by: Anamika08
1 Replies

7. Shell Programming and Scripting

Merge two cells in excel via UNIX?

Hi UNIX Gods! Is it possible to merge two cells in .csv file using unix commands? Imagine that this is my present csv file opened via excel: Gate Reports| | fatal alerts | 200 | is is possible to make it look like this using unix? Gate Reports | fatal... (1 Reply)
Discussion started by: 4dirk1
1 Replies

8. Shell Programming and Scripting

combine 3 excel files using shell.

Is there any way to combine 3 excel files into one comma separated file, after removing the header row from all the three files. Is this possible? I have looked in FAQ and I did not find anything. Appreciate any suggestions or links to resources. Radhika. (11 Replies)
Discussion started by: radhika
11 Replies

9. Shell Programming and Scripting

create diffrent files based on other file and parameters list

I would like ot create shell script/ bash to create diffrent files based on a file and parameters list. Here is the detail example: I have a textfile and four static parameter files (having ‘?'). mainfile.txt has below records (this count may be more than 50) A200001 A200101 B200001... (9 Replies)
Discussion started by: raghav525
9 Replies

10. Shell Programming and Scripting

shell script to merge files

Can anybody help me out with this problem " a shell program that takes one or any number of file names as input; sorts the lines of each file in ascending order and displays the non blank lines of each sorted file and merge them as one combined sorted file. The program generates an error... (1 Reply)
Discussion started by: arya
1 Replies
Login or Register to Ask a Question