Perl syntax for formatting columns and text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl syntax for formatting columns and text
# 1  
Old 03-15-2014
Perl syntax for formatting columns and text

Dear all,

Pzl let me know what is the syntax for converting the columns format to text as i have lots of values for
Quote:
eg. 001,0235,0002215,0012596,
but when i put these values in xls sheet the values are automatically converted to
Quote:
1,235 etc.
and one more question i have is how to call values from shell script into perl script
eg.
Quote:
if I want to add border and any formatting from cell A1 to A23 and this 23 value is stored/grepped in shell script then how to call it in abc.pl script.
Thanks in advance
# 2  
Old 03-15-2014
Are you using spreadheet::? How (what perl library) are you trying to use?
Spreadsheet::WriteExcel - search.cpan.org
# 3  
Old 03-15-2014
Dear jim mcnamara,

I have just started to use perl script,pl find my code as below,every CSV created here contains text in first column as mentioned before but as soon as i open this excel sheet in windows all zeros which are prefixed to numbers are getting removed.Hence i want to convert this first column as text.
Thanks in advance.

Code:
#!/usr/bin/perl -w

use strict;
use warnings;
use Spreadsheet::Read;
use Spreadsheet::WriteExcel;
#Reading data from csv
my $workbook1 = ReadData("Overview.csv");
my @data1 = Spreadsheet::Read::rows($workbook1->[1]);

my $workbook2 = ReadData("1.csv");
my @data2 = Spreadsheet::Read::rows($workbook2->[1]);

my $workbook3 = ReadData("2.csv");
my @data3 = Spreadsheet::Read::rows($workbook3->[1]);

my $workbook4 = ReadData("3.csv");
my @data4 = Spreadsheet::Read::rows($workbook4->[1]);

my $workbook5 = ReadData("4.csv");
my @data5 = Spreadsheet::Read::rows($workbook5->[1]);

my $workbook6 = ReadData("5.csv");
my @data6 = Spreadsheet::Read::rows($workbook6->[1]);

#Creating new Excel workbook

my $workbook7 = Spreadsheet::WriteExcel->new('sagar.xls');

my $format1 = $workbook7->add_format();
## $format1->set_bold();
  $format1->set_color('black');
   $format1->set_align('center');
   $format1->set_font("Calibri");
   $format1->set_bg_color('orange');

   my $format2 = $workbook7->add_format();
    $format2->set_bold();
     $format2->set_color('green');
      $format2->set_align('right');
       $format2->set_bg_color('red');

       # Create a format for the column headings
       my $header = $workbook7->add_format();
       $header->set_bold();
       $header->set_font("Courier New");
       $header->set_align('center');
       $header->set_align('vcenter');
       $header->set_color('white');
       $header->set_bg_color('black');



       # Adding worksheet to workbook

    my $worksheet1 = $workbook7->add_worksheet("1");
    my $worksheet2 = $workbook7->add_worksheet("2");
    my $worksheet3 = $workbook7->add_worksheet("3");
    my $worksheet4 = $workbook7->add_worksheet("4");
    my $worksheet5 = $workbook7->add_worksheet("5");
    my $worksheet6 = $workbook7->add_worksheet("6");

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

$worksheet2->set_column(1, 1, 10);
$worksheet2->set_column(2, 2, 10);
$worksheet2->set_column(3, 3, 15);
$worksheet2->set_column(4, 4, 55);
$worksheet2->set_column(5, 5, 24);

$worksheet3->set_column(1, 1, 10);
$worksheet3->set_column(2, 2, 10);
$worksheet3->set_column(3, 3, 15);
$worksheet3->set_column(4, 4, 55);
$worksheet3->set_column(5, 5, 24);

$worksheet4->set_column(1, 1, 10);
$worksheet4->set_column(2, 2, 10);
$worksheet4->set_column(3, 3, 15);
$worksheet4->set_column(4, 4, 55);
$worksheet4->set_column(5, 5, 24);

$worksheet5->set_column(1, 1, 10);
$worksheet5->set_column(2, 2, 10);
$worksheet5->set_column(3, 3, 15);
$worksheet5->set_column(4, 4, 55);
$worksheet5->set_column(5, 5, 24);

$worksheet6->set_column(1, 1, 10);
$worksheet6->set_column(2, 2, 10);
$worksheet6->set_column(3, 3, 15);
$worksheet6->set_column(4, 4, 80);
$worksheet6->set_column(5, 5, 24);





       # Write the data to worksheet

       $worksheet1->write( 'B2',[\@data1],$format1);
       $worksheet2->write( 'B2',[\@data2],$format1);
       $worksheet3->write( 'B2',[\@data3],$format1);
       $worksheet4->write( 'B2',[\@data4],$format1);
       $worksheet5->write( 'B2',[\@data5],$format1);
       $worksheet6->write( 'B2',[\@data6],$format1);

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

$worksheet2->write( 'B1','ID',$header);
$worksheet2->write( 'C1','ACTION',$header);
$worksheet2->write( 'D1','ACTION_DATE',$header);
$worksheet2->write( 'E1','MSG',$header);
$worksheet2->write( 'F1','DT',$header);

# 4  
Old 03-18-2014
From Spreadsheet::WriteExcel perldoc page
Quote:
# Format as a string. Doesn't change to a number when edited
my $format1 = $workbook->add_format(num_format => '@');
$worksheet->write_string('A2', '01209', $format1);
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash formatting data into columns

Hi guys, I'm trying to create a table of aggregated data using just bash commands. My data is in three columns, for example: 2014-01-01 testA 64 2014-01-01 testB 27 2014-02-01 testA 31 2014-02-02 testB 29 2014-02-02 testC 12 And the result I am looking for is: ... (4 Replies)
Discussion started by: mccmjc
4 Replies

2. Shell Programming and Scripting

Formatting columns in file

I have a script that creates a file with three column output. The columns look like this: Policy Name Name Volume VLS-EDWARDS-ARCHIVED_FILE-WINDOWS 10 12 XLZ-BASE-CYGWIN-ARCHIVED-FILE-LINUX 2 21 ZLX-GOLD-FILES-JAPANFILERS1-LINUX 20 27 ZLX-GOLD-FILES-JAPANFILERS2-LINUX13 29... (4 Replies)
Discussion started by: newbie2010
4 Replies

3. Shell Programming and Scripting

Aligning columns in a text file using Perl

Hi All, I am new to perl and was trying to write a simple program which will generate a text file as output.. now the output which i am getting is something like this.. ================================================================================================== Col1 ... (8 Replies)
Discussion started by: smarty86
8 Replies

4. Shell Programming and Scripting

formatting columns

Hi I have a file that is several gigs in size and basically I want to change the format of it. Obviously I cannot go through it manually. The file looks like this: 897 2 901 2 905 2 909 2 913 2 917 2 921 2 925 2 929 2 933 2 937 2 941 2 945 2 949 0 (1 Reply)
Discussion started by: kylle345
1 Replies

5. Shell Programming and Scripting

Formatting output in columns

I have a file which contains data in below format: nbkv28s MgmtReporting -> TradingDesk 1 nbkv28s RMBS -> Credits 178 nbkv28s RMBS -> PassThrough 96 nbkv28s RMBS -> Prepayment 111 nbkv28s RMBS -> RMBSHome 370 nbkv28s RMBS -> TradingStrategy 98 nbkvnze RMBS -> RMBSHome 85 nbkvugn GSF... (3 Replies)
Discussion started by: mohsin.quazi
3 Replies

6. UNIX for Dummies Questions & Answers

Formatting a line of data into columns

Hi all, I'm a little stuck with a data file I've been collecting data in. The file contains one field of data running continuously down the file and I can't work out how to format the data into three columns. This is a mock up of the file: Each r# is a random number and varies in length, this... (3 Replies)
Discussion started by: nistleloy
3 Replies

7. Shell Programming and Scripting

Text formatting in Perl.

Hiho, I've got a variable $sth = `du -sh /home/$whoami`; where $whoami is actually logged user. I want to have only the size of this directory eg. 2,7G. I know its lame, but anyway.. how to do it? (2 Replies)
Discussion started by: fenox
2 Replies

8. Shell Programming and Scripting

text file formatting by perl

have a simple text file as input.i have to print that file in paragraph format.whenevr it finds "\n" in the input text it should start printing in next paragraph in output file.also a fixed amount of space should be given before start writing in every paragraph. the input and output file format... (5 Replies)
Discussion started by: avik1983
5 Replies

9. Shell Programming and Scripting

Formatting info into columns

Hi This question has kind of been asked before but I couldn't get any of the solutions to work. I need to format fields being cut from a file into columns so it looks like a table. e.g Full name Address Hiredate Joe Smith London 11.01.01 Bill King ... (8 Replies)
Discussion started by: straight_edge
8 Replies

10. UNIX for Dummies Questions & Answers

Text formatting to 132 columns

I want to take a file that is generated in 132 columns from a unix based report generator and transfer it to a windows environment and preserve the column length. Any ideas how I can do this so that whatever windows based text editor that reads the document will know it should be in 132 columns?... (16 Replies)
Discussion started by: jmossman
16 Replies
Login or Register to Ask a Question