Sponsored Content
Top Forums Shell Programming and Scripting Perl syntax for formatting columns and text Post 302892891 by sagar_1986 on Saturday 15th of March 2014 11:05:45 AM
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);

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
dp(8mh) 																   dp(8mh)

Name
       dp - parse dates RFC 822-style

Syntax
       /usr/lib/mh/dp [ -form file ] [ -format string ] [ -help ] [ -width columns ] dates

Description
       The command parses dates according to the ARPA Internet standard.  It also understands many non-standard formats, such as those produced by
       TOPS-20 sites and some UNIX sites using It is useful for seeing how MH will interpret a date.

       The program treats each argument as a single date, and prints the date out in the official RFC 822 format.  Hence, it is  usually  best	to
       enclose each argument in double quotes (") for the shell.

Options
       -form file
		 Overrides the default output format of The output is formatted according to the instructions in the specified file.  This must be
		 a format file; see

       -format string
		 Overrides the default output format of The output is formatted according to the instructions in the specified string.	This  must
		 be a format string; see

		 The  argument to the -format option must be interpreted as a single token by the shell that invokes Therefore, you should usually
		 place the argument to this option inside double quotes (").

       -help	 Prints a list of the valid options to this command.

       -width columns
		 Specifies the width of the screen in columns.

       The defaults for this command are as follows:

	      -width defaults to the width of the terminal

Examples
       This is the default format string used by
       %<(nodate{text})error: %{text}%|%(pretty{text})%>

       When an error is detected, this prints error: and the date that is in error.  Otherwise, it outputs the proper RFC 822 format of the date.

Files
       User profile.

See Also
       ap(8mh)
       Standard for the Format of ARPA Internet Text Messages (RFC 822)

																	   dp(8mh)
All times are GMT -4. The time now is 03:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy