Sponsored Content
Top Forums Programming Perl script to merge cells in column1 which has same strings, for all sheets in a excel workbook Post 302952720 by derekludwig on Friday 21st of August 2015 07:57:16 AM
Old 08-21-2015
Something like:
Code:
#! /usr/bin/perl

use strict;
use warnings;
use Spreadsheet::WriteExcel;

my $file = shift @ARGV;
my $workbook = Spreadsheet::WriteExcel->new($file) or die "can't create worksheet: $!";

my $dest = shift @ARGV;
chdir $dest or die "no such directory: $!";
opendir my $dh, '.' or die "can't open directory: $!";
my @files = sort grep { m{^[^.]} } readdir($dh);
close $dh; 

foreach my $file (@files) {
    open my $in, '<', $file or die "Could not open file: $!";

    $file =~ s{\..*$}{};
    my $worksheet   = $workbook->add_worksheet($file);

    my @prevRow = ();
    my $row = 0;

    while(<$in>) {
        my @currRow  = split(',', $_);
        my $col = 0;

        while ($col < @currRow) {
            last if @prevRow < $col || $currRow[$col] ne $prevRow[$col];
            $worksheet->write($row, $col, '');
            $col++;
        }

        while ($col < @currRow) {
            $worksheet->write($row, $col, $currRow[$col]);
            $col++;
        }

        @prevRow = @currRow;
        $row++;
    }
}

Invoked with:
Code:
scriptname workbookfile sourcedir...

Some notes:
  • if ( -d $dest ) { was not needed, the previous chdir would have failed if $dest was not a directory.
  • chomp $file is not needed, a trailing newline is an acceptable (if not appreciated) character in a directory entry
  • Be reluctant to use system calls in a perlscript. In this case, my $sheetname = `basename $file | cut -d. -f1`; was replaced by $file =~ s{\..*$}{}; as $file is already a "basename" and you were just trimming off everything after the first ".".
 

10 More Discussions You Might Find Interesting

1. Programming

creating more than 2 excel sheets in C

Hi, I have a question about using C to write excel file. There is a way to write into a sheet into a file excel, like this: FILE * File; pMyFile = fopen("test.xls", "w+"); fprintf(File, "\n\n"); /* go to the row 2 of the column A*/ fprintf(File, "%s", "Hi pippo!"); /* write... (7 Replies)
Discussion started by: manceryder
7 Replies

2. Shell Programming and Scripting

Multiple excel work sheets through UNIX

Hi, There is this requirement to create multiple work sheets in an MS Excel file through UNIX. We normally can create one work sheet in unix by either tab or comma delimiting and appending .xls or .csv to the file name, but can we create multiple work sheets. Regards, Puspendu (1 Reply)
Discussion started by: puspendu
1 Replies

3. Shell Programming and Scripting

How to format excel sheets in UNIX??

Hi, I have generated an excel sheet using a shell script. i have converted the output text file to an excel and got the desired output. However, in a particular column in the excel the values of the numbers start with 0. e.g. 078393343, 00342442, etc. But, in the resulting excel I get as... (2 Replies)
Discussion started by: Vijay06
2 Replies

4. Shell Programming and Scripting

PERL: Split Excel Workbook to Indiv Excel files

Hi, I am trying to find a way to read an excel work book with multiple worksheets. And write each worksheet into a new excel file using perl. My environment is Unix. For example: I have an excel workbook TEST.xls and it has Sheet1, Sheet2, Sheet3 worksheets. I would like to create... (2 Replies)
Discussion started by: sandeep78
2 Replies

5. Shell Programming and Scripting

Sending SQL Queries output to different Excel sheets

Hi, I need your help in sedning sql queries output to different excel sheets. My requirement is like this: Query1: Select name from table1 where status = 'Complete' Query2: Select name from table1 where status = 'Failed' Query3: Select name from table1 where status = 'Ignored' ... (4 Replies)
Discussion started by: parvathi_rd
4 Replies

6. Shell Programming and Scripting

modify Existing MS excel workbook in perl

Hi I need to modify an excel file in perl and for which I installed perl in Linux 1. Open a existing excel file 2. delete an unwanted Sheet called "summary" 3. and i want to insert some data into range of cells ( B1:B11) 4. Remove unwanted value called "Sum" repeated in the... (1 Reply)
Discussion started by: luke_devon
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

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

9. Shell Programming and Scripting

Write two csv files into one excel with multiple sheets

I have requirement to write two CSV files to one single excel with multiple sheets. Data present in the two files should sit in excel as different sheets. How can we achieve this using shell script? 1.csv 2. csv 1,2,3,4 5,6,7,8 XXXXX YYYYY Res.excel 1.csv data... (1 Reply)
Discussion started by: duplicate
1 Replies

10. Shell Programming and Scripting

Help with Perl script for identifying dupes in column1

Dear all, I have a large dictionary database which has the following structure source word=target word e.g. book=livre Since the database is very large in spite of all the care taken, it so happens that at times the source word is repeated e.g. book=livre book=tome Since I want to... (7 Replies)
Discussion started by: gimley
7 Replies
Excel(3pm)						User Contributed Perl Documentation						Excel(3pm)

NAME
DBD::Excel - A class for DBI drivers that act on Excel File. This is still alpha version. SYNOPSIS
use DBI; $hDb = DBI->connect("DBI:Excel:file=test.xls") or die "Cannot connect: " . $DBI::errstr; $hSt = $hDb->prepare("CREATE TABLE a (id INTEGER, name CHAR(10))") or die "Cannot prepare: " . $hDb->errstr(); $hSt->execute() or die "Cannot execute: " . $hSt->errstr(); $hSt->finish(); $hDb->disconnect(); DESCRIPTION
This is still alpha version. The DBD::Excel module is a DBI driver. The module is based on these modules: * Spreadsheet::ParseExcel reads Excel files. * Spreadsheet::WriteExcel writes Excel files. * SQL::Statement a simple SQL engine. * DBI Of course. :-) This module assumes TABLE = Worksheet. The contents of first row of each worksheet as column name. Adding that, this module accept temporary table definition at "connect" method with "xl_vtbl". ex. my $hDb = DBI->connect( "DBI:Excel:file=dbdtest.xls", undef, undef, {xl_vtbl => {TESTV => { sheetName => 'TEST_V', ttlRow => 5, startCol => 1, colCnt => 4, datRow => 6, datLmt => 4, } } }); For more information please refer sample/tex.pl included in this distribution. Metadata The following attributes are handled by DBI itself and not by DBD::Excel, thus they all work like expected: Active ActiveKids CachedKids CompatMode (Not used) InactiveDestroy Kids PrintError RaiseError Warn (Not used) The following DBI attributes are handled by DBD::Excel: AutoCommit Always on ChopBlanks Works NUM_OF_FIELDS Valid after "$hSt->execute" NUM_OF_PARAMS Valid after "$hSt->prepare" NAME Valid after "$hSt->execute"; undef for Non-Select statements. NULLABLE Not really working, always returns an array ref of one's. Valid after "$hSt->execute"; undef for Non-Select statements. These attributes and methods are not supported: bind_param_inout CursorName LongReadLen LongTruncOk Additional to the DBI attributes, you can use the following dbh attribute: xl_fmt This attribute is used for setting the formatter class for parsing. xl_dir This attribute is used only with "data_sources" on setting the directory where Excel files ('*.xls') are searched. It defaults to the current directory ("."). xl_vtbl assumes specified area as a table. See sample/tex.pl. xl_skiphidden skip hidden rows(=row height is 0) and hidden columns(=column width is 0). See sample/thidden.pl. xl_ignorecase set casesensitive or not about table name and columns. Default is sensitive (maybe as SQL::Statement). See sample/thidden.pl. Driver private methods data_sources The "data_sources" method returns a list of '*.xls' files of the current directory in the form "DBI:Excel:xl_dir=$dirname". If you want to read the subdirectories of another directory, use my($hDr) = DBI->install_driver("Excel"); my(@list) = $hDr->data_sources( { xl_dir => '/usr/local/xl_data' } ); list_tables This method returns a list of sheet names contained in the $hDb->{file}. Example: my $hDb = DBI->connect("DBI:Excel:file=test.xls"); my @list = $hDb->func('list_tables'); TODO
More tests First of all... Type and Format The current version not support date/time and text formating. Joins The current version of the module works with single table SELECT's only, although the basic design of the SQL::Statement module allows joins and the likes. KNOWN BUGS
o There are too many TODO things. So I can't determind what is BUG. :-) AUTHOR
Kawai Takanori (Hippo2000) kwitknr@cpan.org Homepage: http://member.nifty.ne.jp/hippo2000/ (Japanese) http://member.nifty.ne.jp/hippo2000/index_e.htm (English) Wiki: http://www.hippo2000.net/cgi-bin/KbWiki/KbWiki.pl (Japanese) http://www.hippo2000.net/cgi-bin/KbWikiE/KbWiki.pl (English) SEE ALSO
DBI, Spreadsheet::WriteExcel, Spreadsheet::ParseExcel, SQL::Statement COPYRIGHT
Copyright (c) 2001 KAWAI,Takanori All rights reserved. You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file. perl v5.8.8 2008-03-01 Excel(3pm)
All times are GMT -4. The time now is 05:11 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy