1

I am trying to create excel sheet based on multiple files on a root. I read files line by line and append in the final excel sheet.

I am trying this shell script on small files and it worked 100%, but when I try it on the needed files (85MB per each file) I get this error:

(dsadm@DEVDS) /EDWH/XML/Must # XML.sh csv excel_outputfilename
./XML.sh: line 41: fallocate: command not found
./XML.sh: xmalloc: cannot allocate 172035663 bytes (0 bytes allocated)
./XML.sh: xrealloc: cannot reallocate 86013568 bytes (0 bytes allocated)
./XML.sh: xrealloc: cannot reallocate 86021888 bytes (0 bytes allocated)

Note:

  • The csv parameter is the file extension

  • My OS and version: Unix AIX 7.1

Here's the script:

#!/usr/bin/bash  

#Files Extension#
Ext=$1

#OutPut File Name without extension ex: TEST#
OutPutFileName=$2.xls

function XMLHeader ()
{
     echo "<?xml version=\"1.0\"?>
    <Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"
     xmlns:o=\"urn:schemas-microsoft-com:office:office\"
     xmlns:x=\"urn:schemas-microsoft-com:office:excel\"
     xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"
     xmlns:html=\"http://www.w3.org/TR/REC-html40\">"
}

function SheetHeader ()
{
    echo "<Worksheet ss:Name=\"Sheet1\">
    <Table ss:ExpandedColumnCount=\"2\" ss:ExpandedRowCount=\"2\" x:FullColumns=\"1\" x:FullRows=\"1\">
    <Row><Cell><Data ss:Type=\"String\">"
}

function SheetFooter ()
{
    echo "</Data></Cell></Row></Table>
    </Worksheet>"
}

function XMLFooter ()
{
    echo "</Workbook>"
}

####################################################################################

cd /EDWH/Samir/XML/Must;

fallocate -l 1G $OutPutFileName

XMLHeader > $OutPutFileName;

# loop on the exists files to build Worksheet per each file 
for Vfile in $(ls | grep .$Ext); 
do
    echo "<Worksheet ss:Name=\"$Vfile\"><Table>" >> $OutPutFileName

    ### loop to write the Row 
        VarRow=`cat $Vfile`
        for Row in $(echo $VarRow )
        do

            echo "<Row>" >> $OutPutFileName

                ### loop to write the cells 
                VarCell=`echo $VarRow`
                for Cell in $(echo $VarCell | sed "s/,/ /g")
                do
                    echo "<Cell><Data ss:Type=\"String\">$Cell</Data></Cell>" >> $OutPutFileName
                done

            echo "</Row>" >> $OutPutFileName

        done

    echo "</Table></Worksheet>" >> $OutPutFileName

done    


echo "</Workbook>" >> $OutPutFileName   

####################################################################################

exit;
janos
  • 11,341
  • "Am trying to create excel sheet base on multifiles on a root, I read files line by line append in the final excel sheet." -- so that means you want to concatenate several Excel files in one? – schaiba Nov 19 '15 at 10:05
  • ya, you can say that – Ahmed Samir Nov 19 '15 at 10:21
  • it seems you exhausted the available memory. How big are that files? – Rui F Ribeiro Nov 19 '15 at 10:28
  • Using bash to parse XML and parsing it by hand isn't the best option, you know. If you have some background in basics of programming, I'd suggest using Python + cElementTree(which works like ElementTree, except it's faster) - I'm currently using it, and it allows me to handle files bigger than my ram, so memory is no issue here. – MatthewRock Nov 19 '15 at 10:29
  • btw. "Cannot reallocate" refers to reallocation of memory in RAM, which means that program can't request memory you want from kernel, and it's the reason for failure. It has nothing to do with reallocation of physical files. – MatthewRock Nov 19 '15 at 10:31
  • Do you have this fix installed? http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 – Jeff Schaller Nov 19 '15 at 10:31
  • 85 Mb per each file
  • can u show me and tutorial in Python + cElementTreeto handle this case
  • how to fix this IBM issue ?
  • thanks for all

    – Ahmed Samir Nov 19 '15 at 10:57
  • What's the output of instfix -ik IV56170? – Jeff Schaller Nov 19 '15 at 11:55
  • Where this word ???? – Ahmed Samir Nov 19 '15 at 13:40